util: introduce and use xrealloc

Just like xzalloc(). Abort when memory allocations fail.

Signed-off-by: Michael Olbrich <m.olbrich@pengutronix.de>
This commit is contained in:
Michael Olbrich 2020-11-27 11:35:05 +01:00
parent 2672a5e604
commit f69064ead3
2 changed files with 14 additions and 1 deletions

View File

@ -129,6 +129,7 @@ extern struct image_handler fit_handler;
(type *)( (char *)__mptr - offsetof(type,member) );})
void *xzalloc(size_t n);
void *xrealloc(void *ptr, size_t size);
unsigned long long strtoul_suffix(const char *str, char **endp,
cfg_bool_t *percent);

14
util.c
View File

@ -253,6 +253,18 @@ void *xzalloc(size_t n)
return m;
}
void *xrealloc(void *ptr, size_t size)
{
void *m = realloc(ptr, size);
if (!m) {
error("out of memory\n");
exit(1);
}
return m;
}
/*
* Like simple_strtoul() but handles an optional G, M, K or k
* suffix for Gigabyte, Megabyte or Kilobyte
@ -360,7 +372,7 @@ static int map_file_extents(struct image *image, const char *filename, int f,
goto err_out;
/* Get extents */
fiemap = realloc(fiemap, sizeof(struct fiemap) + fiemap->fm_mapped_extents * sizeof(struct fiemap_extent));
fiemap = xrealloc(fiemap, sizeof(struct fiemap) + fiemap->fm_mapped_extents * sizeof(struct fiemap_extent));
fiemap->fm_extent_count = fiemap->fm_mapped_extents;
ret = ioctl(f, FS_IOC_FIEMAP, fiemap);
if (ret == -1)