Browse Source

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()]
pull/185/head
Tobias Schramm 4 years ago
committed by Matthias Schiffer
parent
commit
3566cabef5
No known key found for this signature in database GPG Key ID: 16EF3F64CB201D9C
  1. 20
      admin/autoupdater/src/util.c
  2. 5
      admin/autoupdater/src/util.h

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

5
admin/autoupdater/src/util.h

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

Loading…
Cancel
Save