Skip to content

Commit

Permalink
smex: elf: Added checking of value returned by file operation function
Browse files Browse the repository at this point in the history
Added checking of the value returned by fseek function and added memory
release when an error is detected.

Signed-off-by: Adrian Warecki <adrian.warecki@intel.com>
  • Loading branch information
softwarecki authored and lgirdwood committed Sep 26, 2023
1 parent 877079a commit 7141836
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions smex/elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ int elf_read_section(const struct elf_module *module, const char *section_name,
{
const Elf32_Shdr *section;
int section_index;
int read;
int ret;

section_index = elf_find_section(module, section_name);
if (section_index < 0) {
Expand All @@ -451,17 +451,25 @@ int elf_read_section(const struct elf_module *module, const char *section_name,
return -ENOMEM;

/* fill buffer with section content */
fseek(module->fd, section->off, SEEK_SET);
read = fread(*dst_buff, 1, section->size, module->fd);
if (read != section->size) {
fprintf(stderr,
"error: can't read %s section %d\n", section_name,
-errno);
free(*dst_buff);
return -errno;
ret = fseek(module->fd, section->off, SEEK_SET);
if (ret) {
fprintf(stderr, "error: can't seek to %s section %d\n", section_name, -errno);
ret = -errno;
goto error;
}

ret = fread(*dst_buff, 1, section->size, module->fd);
if (ret != section->size) {
fprintf(stderr, "error: can't read %s section %d\n", section_name, -errno);
ret = ret < 0 ? -errno : -ENODATA;
goto error;
}

return section->size;

error:
free(*dst_buff);
return ret;
}

int elf_read_module(struct elf_module *module, const char *name, bool verbose)
Expand Down

0 comments on commit 7141836

Please sign in to comment.