From 3566cabef51d053d365f84edf0cc8fa9eba994da Mon Sep 17 00:00:00 2001 From: Tobias Schramm Date: Tue, 20 Feb 2018 12:03:25 +0100 Subject: [PATCH] autoupdater: add safe allocation functions safe_malloc() and safe_realloc() are wrappers around malloc() and realloc() than abort the process if the memory allocation fails. Signed-off-by: Tobias Schramm [Matthias Schiffer: add safe_realloc()] --- admin/autoupdater/src/util.c | 20 ++++++++++++++++++++ admin/autoupdater/src/util.h | 5 +++++ 2 files changed, 25 insertions(+) diff --git a/admin/autoupdater/src/util.c b/admin/autoupdater/src/util.c index 881523f..ff21d4f 100644 --- a/admin/autoupdater/src/util.c +++ b/admin/autoupdater/src/util.c @@ -100,3 +100,23 @@ float get_uptime(void) { fputs("autoupdater: error: unable to determine uptime\n", stderr); exit(1); } + +void * safe_malloc(size_t size) { + void *ret = malloc(size); + if (!ret) { + fprintf(stderr, "autoupdater: error: failed to allocate memory\n"); + abort(); + } + + return ret; +} + +void * safe_realloc(void *ptr, size_t size) { + void *ret = realloc(ptr, size); + if (!ret) { + fprintf(stderr, "autoupdater: error: failed to allocate memory\n"); + abort(); + } + + return ret; +} diff --git a/admin/autoupdater/src/util.h b/admin/autoupdater/src/util.h index 5c23d79..305cc3a 100644 --- a/admin/autoupdater/src/util.h +++ b/admin/autoupdater/src/util.h @@ -24,7 +24,12 @@ */ #pragma once +#include + void run_dir(const char *dir); void randomize(void); float get_uptime(void); + +void * safe_malloc(size_t size); +void * safe_realloc(void *ptr, size_t size);