Nico Huber has uploaded this change for review. ( https://review.coreboot.org/c/flashrom/+/31015
Change subject: helpers: Add realloc_or_free() ......................................................................
helpers: Add realloc_or_free()
Just like realloc() but frees the original pointer on failure.
Change-Id: Iee0bf09cd7b57d2032efd490e7d542dba80c0401 Signed-off-by: Nico Huber nico.h@gmx.de --- M flash.h M helpers.c 2 files changed, 11 insertions(+), 0 deletions(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/15/31015/1
diff --git a/flash.h b/flash.h index 911214c..3eefd45 100644 --- a/flash.h +++ b/flash.h @@ -308,6 +308,7 @@ #if defined(__DJGPP__) || (!defined(__LIBPAYLOAD__) && !defined(HAVE_STRNLEN)) size_t strnlen(const char *str, size_t n); #endif +void *realloc_or_free(void *ptr, size_t size);
/* flashrom.c */ extern const char flashrom_version[]; diff --git a/helpers.c b/helpers.c index a714908..deff3b0 100644 --- a/helpers.c +++ b/helpers.c @@ -114,3 +114,13 @@ return i; } #endif + +void *realloc_or_free(void *const ptr, const size_t size) +{ + void *const ret = realloc(ptr, size); + if (!ret) { + msg_gerr("Out of memory!\n"); + free(ptr); + } + return ret; +}