[openwrt/openwrt] firmware-utils: fix coverity zytrx.c resource leak

LEDE Commits lede-commits at lists.infradead.org
Thu Jun 10 08:14:22 PDT 2021


ynezz pushed a commit to openwrt/openwrt.git, branch openwrt-21.02:
https://git.openwrt.org/27f4559281fb9f6a4d8ec5e054cea7f4c6b77820

commit 27f4559281fb9f6a4d8ec5e054cea7f4c6b77820
Author: Kevin Darbyshire-Bryant <ldir at darbyshire-bryant.me.uk>
AuthorDate: Sun May 16 11:46:32 2021 +0100

    firmware-utils: fix coverity zytrx.c resource leak
    
    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 at darbyshire-bryant.me.uk>
    (cherry picked from commit baf2a50ef3cf34574e12d2ab1b23578310f0d527)
---
 tools/firmware-utils/src/zytrx.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/tools/firmware-utils/src/zytrx.c b/tools/firmware-utils/src/zytrx.c
index 302efc6010..7167679206 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;
 }
 



More information about the lede-commits mailing list