firmware-utils/mkfwimage: fix possible memory and resource leak
authorAndrea Dalla Costa <andrea@dallacosta.me>
Sat, 11 Jan 2020 22:27:17 +0000 (23:27 +0100)
committerJo-Philipp Wich <jo@mein.io>
Tue, 14 Jan 2020 16:48:50 +0000 (17:48 +0100)
Add missing calls to `free` for variable `mem`.
Add missing call to `fclose` for variable `f`.

The same changes were made in both `mkfwimage.c` and `mkfwimage2.c`.

Signed-off-by: Andrea Dalla Costa <andrea@dallacosta.me>
tools/firmware-utils/src/mkfwimage.c
tools/firmware-utils/src/mkfwimage2.c

index d0dca040cdaee4d71a39ada82b8242ba08a7c7e8..9e6d8f5b9ab4b62305ba10e6c670a53ee8e7b2ee 100644 (file)
@@ -455,6 +455,7 @@ static int build_image(image_info_t* im)
        if ((f = fopen(im->outputfile, "w")) == NULL)
        {
                ERROR("Can not create output file: '%s'\n", im->outputfile);
+               free(mem);
                return -10;
        }
 
@@ -462,6 +463,8 @@ static int build_image(image_info_t* im)
        {
                ERROR("Could not write %d bytes into file: '%s'\n",
                                mem_size, im->outputfile);
+               free(mem);
+               fclose(f);
                return -11;
        }
 
index 89a98051b430e7077045ef89601f2418e189b920..9b7e1a3cd78ac252d7ce9f338eacb26e14ceab68 100644 (file)
@@ -363,12 +363,15 @@ static int build_image(void)
        /* write in-memory buffer into file */
        if ((f = fopen(im.outputfile, "w")) == NULL) {
                ERROR("Can not create output file: '%s'\n", im.outputfile);
+               free(mem);
                return -10;
        }
 
        if (fwrite(mem, mem_size, 1, f) != 1) {
                ERROR("Could not write %d bytes into file: '%s'\n",
                                mem_size, im.outputfile);
+               free(mem);
+               fclose(f);
                return -11;
        }