From 2b23146df6b70d2372d1979dd376af37907f4cc4 Mon Sep 17 00:00:00 2001 From: Adrian Warecki Date: Tue, 14 Nov 2023 12:54:42 +0100 Subject: [PATCH] rimage: elf_file: Fix elf_free behavior on unopened elf file If more modules are provided in a command line than defined in a toml file, the program outputs an error message. It frees the module structures even though they have not been opened before. This resulted in an error when trying to close a file that was not previously open. Added a check to ensure that a file has been opened before trying to close it. Error handling in the elf_open function has been simplified. Signed-off-by: Adrian Warecki --- tools/rimage/src/elf_file.c | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/tools/rimage/src/elf_file.c b/tools/rimage/src/elf_file.c index efb9cc912b4e..1891555287e1 100644 --- a/tools/rimage/src/elf_file.c +++ b/tools/rimage/src/elf_file.c @@ -411,18 +411,7 @@ int elf_open(struct elf_file *elf, const char *filename) return 0; err: - free(elf->filename); - free(elf->programs); - - if (elf->file) - fclose(elf->file); - - if (elf->sections) { - for (int i = 0; i < elf->sections_count; i++) - elf_section_header_free(&elf->sections[i]); - - free(elf->sections); - } + elf_free(elf); return ret; } @@ -436,13 +425,17 @@ void elf_free(struct elf_file *elf) int i; free(elf->filename); - fclose(elf->file); + free(elf->programs); - for (i = 0; i < elf->sections_count; i++) - elf_section_header_free(&elf->sections[i]); + if (elf->file) + fclose(elf->file); - free(elf->sections); - free(elf->programs); + if (elf->sections) { + for (i = 0; i < elf->sections_count; i++) + elf_section_header_free(&elf->sections[i]); + + free(elf->sections); + } } int elf_section_read_content(const struct elf_file *elf, const struct elf_section_header *header,