firmware-utils/hcsmakeimage: fix possible memory leak and resource leaks
authorAndrea Dalla Costa <andrea@dallacosta.me>
Sat, 11 Jan 2020 21:57:58 +0000 (22:57 +0100)
committerPetr Štetiar <ynezz@true.cz>
Wed, 8 Jul 2020 14:07:05 +0000 (16:07 +0200)
Add missing calls to `free` for variable `filebuffer`.
Add missing calls to `fclose` for variables `fd` and `fd_out`.

Signed-off-by: Andrea Dalla Costa <andrea@dallacosta.me>
src/hcsmakeimage.c

index 7baa7b5845e1ab9f67aa8f3b86446dce2a98326c..2888810cc7e4c1c5f3d6c3a31dae103f64405105 100644 (file)
@@ -183,6 +183,7 @@ int main ( int argc, char** argv )
        char* filebuffer = malloc ( buf.st_size+10 );
        FILE* fd = fopen ( input,"r" );
        fread ( filebuffer, 1, buf.st_size,fd );
+       fclose (fd);
        if (!output)
                {
                output = malloc(strlen(input+5));
@@ -194,10 +195,13 @@ int main ( int argc, char** argv )
        if (!fd_out)
                {
                fprintf(stderr, "Failed to open output file: %s\n", output);
+               free(filebuffer);
                exit(1);
                }
        fwrite ( head,1,sizeof ( ldr_header_t ),fd_out );
        fwrite ( filebuffer,1,buf.st_size,fd_out );
        printf("Firmware image %s is ready\n", output);
+       free(filebuffer);
+       fclose(fd_out);
        return 0;
 }