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 <adrian.warecki@intel.com>
This commit is contained in:
Adrian Warecki 2023-11-14 12:54:42 +01:00 committed by Kai Vehmanen
parent d42911d974
commit 2b23146df6
1 changed files with 11 additions and 18 deletions

View File

@ -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);
for (i = 0; i < elf->sections_count; i++)
elf_section_header_free(&elf->sections[i]);
free(elf->sections);
free(elf->programs);
if (elf->file)
fclose(elf->file);
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,