util/cbfstool: avoid memleaks and off-by-ones

Change-Id: Iac136a5dfe76f21aa7c0d5ee4e974e50b955403b
Signed-off-by: Patrick Georgi <pgeorgi@chromium.org>
Found-by: scan-build 3.8
Reviewed-on: https://review.coreboot.org/18134
Tested-by: build bot (Jenkins)
Reviewed-by: Nico Huber <nico.h@gmx.de>
This commit is contained in:
Patrick Georgi
2017-01-13 13:30:54 +01:00
committed by Nico Huber
parent 1dfc0a64d4
commit dce629b2f8
3 changed files with 23 additions and 3 deletions

View File

@@ -1150,13 +1150,22 @@ static int cbfs_payload_make_elf(struct buffer *buff, uint32_t arch)
segs[i].len);
} else if (segs[i].type == PAYLOAD_SEGMENT_ENTRY) {
break;
} else {
ERROR("unknown ELF segment type\n");
goto out;
}
if (!name) {
ERROR("out of memory\n");
goto out;
}
if (elf_writer_add_section(ew, &shdr, &tbuff, name)) {
ERROR("Unable to add ELF section: %s\n", name);
free(name);
goto out;
}
free(name);
if (empty_sz != 0) {
struct buffer b;
@@ -1168,10 +1177,16 @@ static int cbfs_payload_make_elf(struct buffer *buff, uint32_t arch)
shdr.sh_addr = segs[i].load_addr + segs[i].len;
shdr.sh_size = empty_sz;
name = strdup(".empty");
if (elf_writer_add_section(ew, &shdr, &b, name)) {
ERROR("Unable to add ELF section: %s\n", name);
if (!name) {
ERROR("out of memory\n");
goto out;
}
if (elf_writer_add_section(ew, &shdr, &b, name)) {
ERROR("Unable to add ELF section: %s\n", name);
free(name);
goto out;
}
free(name);
}
}