diff mbox series

[21.02,02/11] firmware-utils: fix coverity zytrx.c resource leak

Message ID 20210608094512.29956-3-ynezz@true.cz
State Accepted
Delegated to: Petr Štetiar
Headers show
Series ramips: mt7621: backport support for 9 devices | expand

Commit Message

Petr Štetiar June 8, 2021, 9:45 a.m. UTC
From: Kevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>

fix coverity resource leak warning:

    	*len = stat.st_size;
    	mapped = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, 0);
    	if (close(fd) < 0)
CID 1484880:  Resource leaks  (RESOURCE_LEAK)
Variable "mapped" going out of scope leaks the storage it points to.
    		return NULL;
    	return mapped;
    }

Signed-off-by: Kevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>
(cherry picked from commit baf2a50ef3cf34574e12d2ab1b23578310f0d527)
---
 tools/firmware-utils/src/zytrx.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/tools/firmware-utils/src/zytrx.c b/tools/firmware-utils/src/zytrx.c
index 302efc601065..716767920657 100644
--- a/tools/firmware-utils/src/zytrx.c
+++ b/tools/firmware-utils/src/zytrx.c
@@ -150,8 +150,10 @@  static void *map_input(const char *name, size_t *len)
 	}
 	*len = stat.st_size;
 	mapped = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, 0);
-	if (close(fd) < 0)
+	if (close(fd) < 0) {
+		(void) munmap(mapped, stat.st_size);
 		return NULL;
+	}
 	return mapped;
 }