From e4637a3ef67b59a0cc40ff7045c9be7629d7b62c Mon Sep 17 00:00:00 2001 From: Adrian Warecki Date: Tue, 14 Nov 2023 12:54:42 +0100 Subject: [PATCH 1/2] 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, From 202693af7dec1dedc85d02496350a94450c45fbe Mon Sep 17 00:00:00 2001 From: Adrian Warecki Date: Tue, 14 Nov 2023 15:04:44 +0100 Subject: [PATCH 2/2] rimage: module: Use file name in error message in module_write_whole_elf By oversight, the error message did not use the file name passed as a parameter to the module_write_whole_elf function. Signed-off-by: Adrian Warecki --- tools/rimage/src/module.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/rimage/src/module.c b/tools/rimage/src/module.c index b150e246ca8c..ffbd4333ed3b 100644 --- a/tools/rimage/src/module.c +++ b/tools/rimage/src/module.c @@ -98,7 +98,7 @@ int module_write_whole_elf(const struct module *module, FILE *out_file, const ch /* write out section data */ count = fwrite(buffer, module->elf.file_size, 1, out_file); if (count != 1) { - ret = file_error("can't write data", "");// TODO: image->out_file); + ret = file_error("can't write data", filename); goto out; }