firmware-utils: fix coverity zytrx.c resource leak
authorKevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>
Sun, 16 May 2021 10:46:32 +0000 (11:46 +0100)
committerKevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>
Thu, 20 May 2021 10:56:38 +0000 (11:56 +0100)
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>
tools/firmware-utils/src/zytrx.c

index 302efc601065cdbb79f0a9a0c2271819fe978888..716767920657c342c51ab38b86348aaf8b889234 100644 (file)
@@ -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;
 }