Nico Huber has uploaded this change for review.

View Change

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;
+}

To view, visit change 31015. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: flashrom
Gerrit-Branch: master
Gerrit-Change-Id: Iee0bf09cd7b57d2032efd490e7d542dba80c0401
Gerrit-Change-Number: 31015
Gerrit-PatchSet: 1
Gerrit-Owner: Nico Huber <nico.h@gmx.de>
Gerrit-MessageType: newchange