From adfcdb050c59b2a33b108cec6fc8d94c0c7aff0b Mon Sep 17 00:00:00 2001 From: Tobias Schramm Date: Tue, 20 Feb 2018 12:03:25 +0100 Subject: [PATCH] autoupdater: Add safe_malloc function safe_malloc is a wrapper around malloc that aborts the current process if the memory allocation fails Signed-off-by: Tobias Schramm --- admin/autoupdater/src/util.c | 12 ++++++++++++ admin/autoupdater/src/util.h | 1 + 2 files changed, 13 insertions(+) diff --git a/admin/autoupdater/src/util.c b/admin/autoupdater/src/util.c index 881523f..1221925 100644 --- a/admin/autoupdater/src/util.c +++ b/admin/autoupdater/src/util.c @@ -100,3 +100,15 @@ float get_uptime(void) { fputs("autoupdater: error: unable to determine uptime\n", stderr); exit(1); } + +void *safe_malloc(size_t size, char *errmsg) { + void *ret = malloc(size); + + if (ret) + return ret; + + if (errmsg) + fprintf(stderr, "autoupdater: error: %s\n", errmsg); + + abort(); +} diff --git a/admin/autoupdater/src/util.h b/admin/autoupdater/src/util.h index 5c23d79..65c0061 100644 --- a/admin/autoupdater/src/util.h +++ b/admin/autoupdater/src/util.h @@ -28,3 +28,4 @@ void run_dir(const char *dir); void randomize(void); float get_uptime(void); +void *safe_malloc(size_t size, char *errmsg);