From: Pan Xiuli xiuli.pan@linux.intel.com
Handle pointers and memory when error happens.
Signed-off-by: Pan Xiuli xiuli.pan@linux.intel.com --- V2: rebase on the HEAD V3: add ret value to avoid uninitialized use of ret
Signed-off-by: Pan Xiuli xiuli.pan@linux.intel.com --- rimage/rimage.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-)
diff --git a/rimage/rimage.c b/rimage/rimage.c index af57f5a..af4a7d0 100644 --- a/rimage/rimage.c +++ b/rimage/rimage.c @@ -208,10 +208,11 @@ static int write_elf_data(struct image *image) goto out; }
- free(image->prg); - free(image->section); - out: + if (image->prg) + free(image->prg); + if (image->section) + free(image->section); return ret; }
@@ -283,6 +284,8 @@ found: if (image.in_fd == NULL) { fprintf(stderr, "error: unable to open %s for reading %d\n", image.in_file, errno); + ret = -EINVAL; + goto out; }
/* open outfile for writing */ @@ -291,14 +294,19 @@ found: if (image.out_fd == NULL) { fprintf(stderr, "error: unable to open %s for writing %d\n", image.out_file, errno); + ret = -EINVAL; + goto out; }
/* write data */ ret = write_elf_data(&image);
+out: /* close files */ - fclose(image.out_fd); - fclose(image.in_fd); + if (image.in_fd) + fclose(image.in_fd); + if (image.out_fd) + fclose(image.out_fd);
return ret; }