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 <tobleminer@gmail.com>
[Matthias Schiffer: add safe_realloc()]
This commit is contained in:
Tobias Schramm 2018-02-20 12:03:25 +01:00 committed by Matthias Schiffer
parent 0b61fee98e
commit 3566cabef5
No known key found for this signature in database
GPG Key ID: 16EF3F64CB201D9C
2 changed files with 25 additions and 0 deletions

View File

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

View File

@ -24,7 +24,12 @@
*/
#pragma once
#include <stddef.h>
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);