From f16c80d1a241b813706367627383f74248d4c213 Mon Sep 17 00:00:00 2001 From: Andrea Dalla Costa Date: Sat, 11 Jan 2020 23:27:17 +0100 Subject: [PATCH] firmware-utils/mkfwimage: fix possible memory and resource leak 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 --- src/mkfwimage.c | 3 +++ src/mkfwimage2.c | 3 +++ 2 files changed, 6 insertions(+) diff --git a/src/mkfwimage.c b/src/mkfwimage.c index d0dca04..9e6d8f5 100644 --- a/src/mkfwimage.c +++ b/src/mkfwimage.c @@ -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; } diff --git a/src/mkfwimage2.c b/src/mkfwimage2.c index 89a9805..9b7e1a3 100644 --- a/src/mkfwimage2.c +++ b/src/mkfwimage2.c @@ -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; } -- 2.30.2