Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rimage/smex: code improvements / fixes #8604

Merged
merged 8 commits into from
Dec 11, 2023
29 changes: 16 additions & 13 deletions smex/elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static int elf_read_sections(struct elf_module *module, bool verbose)
if (count != hdr->shnum) {
fprintf(stderr, "error: failed to read %s section header %d\n",
module->elf_file, -errno);
return count < 0 ? -errno : -ENODATA;
return count > 0 ? -ENODATA : -errno;
}

/* read in strings */
Expand All @@ -64,7 +64,7 @@ static int elf_read_sections(struct elf_module *module, bool verbose)
if (count != section[hdr->shstrndx].size) {
fprintf(stderr, "error: failed to read %s strings %d\n",
module->elf_file, -errno);
return count < 0 ? -errno : -ENODATA;
return count > 0 ? -ENODATA : -errno;
}

module->bss_index = elf_find_section(module, ".bss");
Expand Down Expand Up @@ -151,7 +151,7 @@ static int elf_read_programs(struct elf_module *module, bool verbose)
if (count != hdr->phnum) {
fprintf(stderr, "error: failed to read %s program header %d\n",
module->elf_file, -errno);
return count < 0 ? -errno : -ENODATA;
return count > 0 ? -ENODATA : -errno;
}

/* check each program */
Expand Down Expand Up @@ -191,7 +191,7 @@ static int elf_read_hdr(struct elf_module *module, bool verbose)
if (count != 1) {
fprintf(stderr, "error: failed to read %s elf header %d\n",
module->elf_file, -errno);
return count < 0 ? -errno : -ENODATA;
return count > 0 ? -ENODATA : -errno;
}

if (!verbose)
Expand Down Expand Up @@ -387,6 +387,11 @@ int elf_find_section(const struct elf_module *module, const char *name)
size_t count;
int ret, i;

if (module->section == NULL) {
fprintf(stderr, "error: NULL module section\n");
return -EINVAL;
}

section = &module->section[hdr->shstrndx];

/* alloc data data */
Expand All @@ -406,7 +411,7 @@ int elf_find_section(const struct elf_module *module, const char *name)
if (count != section->size) {
fprintf(stderr, "error: can't read string section %d\n",
-errno);
ret = count < 0 ? -errno : -ENODATA;
ret = count > 0 ? -ENODATA : -errno;
goto out;
}
buffer[section->size - 1] = '\0';
Expand Down Expand Up @@ -497,34 +502,34 @@ int elf_read_module(struct elf_module *module, const char *name, bool verbose)
ret = fseek(module->fd, 0, SEEK_END);
if (ret < 0) {
ret = -errno;
goto hdr_err;
goto err;
}
module->file_size = ftell(module->fd);
ret = fseek(module->fd, 0, SEEK_SET);
if (ret < 0) {
ret = -errno;
goto hdr_err;
goto err;
}

/* read in elf header */
ret = elf_read_hdr(module, verbose);
if (ret < 0)
goto hdr_err;
goto err;

/* read in programs */
ret = elf_read_programs(module, verbose);
if (ret < 0) {
fprintf(stderr, "error: failed to read program sections %d\n",
ret);
goto hdr_err;
goto err;
}

/* read sections */
ret = elf_read_sections(module, verbose);
if (ret < 0) {
fprintf(stderr, "error: failed to read base sections %d\n",
ret);
goto sec_err;
goto err;
}

elf_module_limits(module);
Expand All @@ -543,9 +548,7 @@ int elf_read_module(struct elf_module *module, const char *name, bool verbose)

return 0;

sec_err:
free(module->prg);
hdr_err:
err:
fclose(module->fd);

return ret;
Expand Down
17 changes: 13 additions & 4 deletions smex/ldc.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,13 @@ static int write_logs_dictionary(struct image *image,
header.base_address = section->vaddr;
header.data_length = section->size;

fwrite(&header, sizeof(struct snd_sof_logs_header), 1,
image->ldc_out_fd);
count = fwrite(&header, sizeof(struct snd_sof_logs_header), 1,
image->ldc_out_fd);
if (count != 1) {
fprintf(stderr, "error: can't write header\n");
ret = -errno;
goto out;
}

count = fwrite(buffer, 1, section->size, image->ldc_out_fd);
if (count != section->size) {
Expand Down Expand Up @@ -140,8 +145,12 @@ static int write_uids_dictionary(struct image *image,
header.base_address = section->vaddr;
header.data_length = section->size;

fwrite(&header, sizeof(struct snd_sof_uids_header), 1,
image->ldc_out_fd);
if (fwrite(&header, sizeof(struct snd_sof_uids_header), 1,
image->ldc_out_fd) != 1) {
fprintf(stderr, "error: cant't write header\n");
ret = -errno;
goto out;
}

if (fwrite(buffer, 1, section->size, image->ldc_out_fd) !=
section->size) {
Expand Down
8 changes: 4 additions & 4 deletions tools/rimage/src/elf_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ static int elf_header_read(struct elf_file *elf)
/* read in elf header */
count = fread(&elf->header, sizeof(elf->header), 1, elf->file);
if (count != 1) {
if (count < 0)
if (!count)
return file_error("failed to read elf header", elf->filename);
else
return elf_error(elf, "Corrupted file.", ENODATA);
Expand Down Expand Up @@ -199,7 +199,7 @@ static int elf_section_headers_read(struct elf_file *elf)

count = fread(&elf->sections[i].data, sizeof(Elf32_Shdr), 1, elf->file);
if (count != 1) {
if (count < 0)
if (!count)
return file_error("failed to read section header", elf->filename);
else
return elf_error(elf, "Corrupted file.", ENODATA);
Expand Down Expand Up @@ -264,7 +264,7 @@ static int elf_program_headers_read(struct elf_file *elf)

count = fread(&elf->programs[i], sizeof(Elf32_Phdr), 1, elf->file);
if (count != 1) {
if (count < 0)
if (!count)
return file_error("failed to read program header", elf->filename);
else
return elf_error(elf, "Corrupted file.", ENODATA);
Expand Down Expand Up @@ -460,7 +460,7 @@ int elf_section_read_content(const struct elf_file *elf, const struct elf_sectio

count = fread(buffer, header->data.size, 1, elf->file);
if (count != 1) {
if (count < 0)
if (!count)
return file_error("failed to read section data", elf->filename);
else
return elf_error(elf, "Corrupted file.", ENODATA);
Expand Down
7 changes: 6 additions & 1 deletion tools/rimage/src/ext_manifest.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,12 @@ int ext_man_write_cavs_25(struct image *image)

for (i = 0; i < count; i++)
header.len += mod_ext->ext_mod_config_array[i].header.ext_module_config_length;
fwrite(&header, 1, sizeof(header), image->out_ext_man_fd);
write_ret = fwrite(&header, sizeof(header), 1, image->out_ext_man_fd);
if (write_ret != 1) {
ret = file_error("can't write fw_ext_man_cavs_header",
image->out_ext_man_file);
goto out;
}

for (i = 0; i < count; i++) {
write_ret = fwrite(&mod_ext->ext_mod_config_array[i].header,
Expand Down
5 changes: 1 addition & 4 deletions tools/rimage/src/file_simple.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,7 @@ static int write_block(struct image *image, struct manifest_module *module,
section->header->name);

/* return padding size */
if (ret >= 0)
return padding;

return ret;
return padding;
}

/**
Expand Down
Loading