hardened_malloc/util.c

39 lines
939 B
C
Raw Normal View History

2018-10-04 02:25:25 +05:30
#include <errno.h>
2018-08-22 00:53:22 +05:30
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef __ANDROID__
#include <async_safe/log.h>
#endif
2018-08-22 00:53:22 +05:30
#include "util.h"
2018-10-04 02:25:25 +05:30
static int write_full(int fd, const char *buf, size_t length) {
do {
ssize_t bytes_written = write(fd, buf, length);
if (bytes_written == -1) {
if (errno == EINTR) {
continue;
}
return -1;
}
buf += bytes_written;
length -= bytes_written;
} while (length);
return 0;
}
2018-08-22 00:53:22 +05:30
COLD noreturn void fatal_error(const char *s) {
2018-10-04 01:52:28 +05:30
const char *prefix = "fatal allocator error: ";
2018-10-04 02:25:25 +05:30
(void)(write_full(STDERR_FILENO, prefix, strlen(prefix)) != -1 &&
write_full(STDERR_FILENO, s, strlen(s)) != -1 &&
write_full(STDERR_FILENO, "\n", 1));
#ifdef __ANDROID__
async_safe_format_log(ANDROID_LOG_FATAL, "hardened_malloc", "fatal allocator error: %s", s);
#endif
2018-08-22 00:53:22 +05:30
abort();
}