Skip to content

Commit

Permalink
rimage: elf_file: Fix elf_free behavior on unopened elf file
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
softwarecki authored and kv2019i committed Nov 28, 2023
1 parent d42911d commit 2b23146
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions tools/rimage/src/elf_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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,
Expand Down

0 comments on commit 2b23146

Please sign in to comment.