Commit a22f6285 authored by dp-arm's avatar dp-arm
Browse files

fiptool: Provide malloc/strdup wrappers to simplify error checking


Change-Id: Ie3e43e9f7d31df40a5997047b9bddec0a06fd47f
Signed-off-by: default avatardp-arm <dimitris.papastamos@arm.com>
Showing with 33 additions and 30 deletions
+33 -30
...@@ -131,6 +131,26 @@ static void log_errx(const char *msg, ...) ...@@ -131,6 +131,26 @@ static void log_errx(const char *msg, ...)
exit(1); exit(1);
} }
static char *xstrdup(const char *s, const char *msg)
{
char *d;
d = strdup(s);
if (d == NULL)
log_errx("strdup: ", msg);
return d;
}
static void *xmalloc(size_t size, const char *msg)
{
void *d;
d = malloc(size);
if (d == NULL)
log_errx("malloc: ", msg);
return d;
}
static void add_image(image_t *image) static void add_image(image_t *image)
{ {
if (nr_images + 1 > MAX_IMAGES) if (nr_images + 1 > MAX_IMAGES)
...@@ -227,10 +247,7 @@ static int parse_fip(const char *filename, fip_toc_header_t *toc_header_out) ...@@ -227,10 +247,7 @@ static int parse_fip(const char *filename, fip_toc_header_t *toc_header_out)
if (fstat(fileno(fp), &st) == -1) if (fstat(fileno(fp), &st) == -1)
log_err("fstat %s", filename); log_err("fstat %s", filename);
buf = malloc(st.st_size); buf = xmalloc(st.st_size, "failed to load file into memory");
if (buf == NULL)
log_err("malloc");
if (fread(buf, 1, st.st_size, fp) != st.st_size) if (fread(buf, 1, st.st_size, fp) != st.st_size)
log_errx("Failed to read %s", filename); log_errx("Failed to read %s", filename);
bufend = buf + st.st_size; bufend = buf + st.st_size;
...@@ -261,16 +278,11 @@ static int parse_fip(const char *filename, fip_toc_header_t *toc_header_out) ...@@ -261,16 +278,11 @@ static int parse_fip(const char *filename, fip_toc_header_t *toc_header_out)
* Build a new image out of the ToC entry and add it to the * Build a new image out of the ToC entry and add it to the
* table of images. * table of images.
*/ */
image = malloc(sizeof(*image)); image = xmalloc(sizeof(*image),
if (image == NULL) "failed to allocate memory for image");
log_err("malloc");
memcpy(&image->uuid, &toc_entry->uuid, sizeof(uuid_t)); memcpy(&image->uuid, &toc_entry->uuid, sizeof(uuid_t));
image->buffer = xmalloc(toc_entry->size,
image->buffer = malloc(toc_entry->size); "failed to allocate image buffer, is FIP file corrupted?");
if (image->buffer == NULL)
log_err("malloc");
/* Overflow checks before memory copy. */ /* Overflow checks before memory copy. */
if (toc_entry->size > (uint64_t)-1 - toc_entry->offset_address) if (toc_entry->size > (uint64_t)-1 - toc_entry->offset_address)
log_errx("FIP %s is corrupted", filename); log_errx("FIP %s is corrupted", filename);
...@@ -308,15 +320,9 @@ static image_t *read_image_from_file(const uuid_t *uuid, const char *filename) ...@@ -308,15 +320,9 @@ static image_t *read_image_from_file(const uuid_t *uuid, const char *filename)
if (fstat(fileno(fp), &st) == -1) if (fstat(fileno(fp), &st) == -1)
log_errx("fstat %s", filename); log_errx("fstat %s", filename);
image = malloc(sizeof(*image)); image = xmalloc(sizeof(*image), "failed to allocate memory for image");
if (image == NULL)
log_err("malloc");
memcpy(&image->uuid, uuid, sizeof(uuid_t)); memcpy(&image->uuid, uuid, sizeof(uuid_t));
image->buffer = xmalloc(st.st_size, "failed to allocate image buffer");
image->buffer = malloc(st.st_size);
if (image->buffer == NULL)
log_err("malloc");
if (fread(image->buffer, 1, st.st_size, fp) != st.st_size) if (fread(image->buffer, 1, st.st_size, fp) != st.st_size)
log_errx("Failed to read %s", filename); log_errx("Failed to read %s", filename);
image->size = st.st_size; image->size = st.st_size;
...@@ -582,9 +588,8 @@ static int create_cmd(int argc, char *argv[]) ...@@ -582,9 +588,8 @@ static int create_cmd(int argc, char *argv[])
toc_entry = &toc_entries[opt_index]; toc_entry = &toc_entries[opt_index];
toc_entry->action = DO_PACK; toc_entry->action = DO_PACK;
toc_entry->action_arg = strdup(optarg); toc_entry->action_arg = xstrdup(optarg,
if (toc_entry->action_arg == NULL) "failed to allocate memory for argument");
log_err("strdup");
break; break;
} }
case OPT_PLAT_TOC_FLAGS: case OPT_PLAT_TOC_FLAGS:
...@@ -653,9 +658,8 @@ static int update_cmd(int argc, char *argv[]) ...@@ -653,9 +658,8 @@ static int update_cmd(int argc, char *argv[])
toc_entry = &toc_entries[opt_index]; toc_entry = &toc_entries[opt_index];
toc_entry->action = DO_PACK; toc_entry->action = DO_PACK;
toc_entry->action_arg = strdup(optarg); toc_entry->action_arg = xstrdup(optarg,
if (toc_entry->action_arg == NULL) "failed to allocate memory for argument");
log_err("strdup");
break; break;
} }
case OPT_PLAT_TOC_FLAGS: { case OPT_PLAT_TOC_FLAGS: {
...@@ -739,9 +743,8 @@ static int unpack_cmd(int argc, char *argv[]) ...@@ -739,9 +743,8 @@ static int unpack_cmd(int argc, char *argv[])
unpack_all = 0; unpack_all = 0;
toc_entry = &toc_entries[opt_index]; toc_entry = &toc_entries[opt_index];
toc_entry->action = DO_UNPACK; toc_entry->action = DO_UNPACK;
toc_entry->action_arg = strdup(optarg); toc_entry->action_arg = xstrdup(optarg,
if (toc_entry->action_arg == NULL) "failed to allocate memory for argument");
log_err("strdup");
break; break;
case 'f': case 'f':
fflag = 1; fflag = 1;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment