make sure errno is always set on failure

pull/50/head
Daniel Micay 2018-08-28 22:46:20 -04:00
parent 66aa98dbd9
commit 8b42e8c3d6
2 changed files with 10 additions and 6 deletions

View File

@ -59,7 +59,8 @@ static void *allocate_pages(size_t usable_size, size_t guard_size, bool unprotec
usable_size = PAGE_CEILING(usable_size); usable_size = PAGE_CEILING(usable_size);
size_t real_size; size_t real_size;
if (__builtin_add_overflow(usable_size, guard_size * 2, &real_size)) { if (unlikely(__builtin_add_overflow(usable_size, guard_size * 2, &real_size))) {
errno = ENOMEM;
return NULL; return NULL;
} }
void *real = memory_map(real_size); void *real = memory_map(real_size);
@ -84,12 +85,14 @@ static void *allocate_pages_aligned(size_t usable_size, size_t alignment, size_t
usable_size = PAGE_CEILING(usable_size); usable_size = PAGE_CEILING(usable_size);
size_t alloc_size; size_t alloc_size;
if (__builtin_add_overflow(usable_size, alignment - PAGE_SIZE, &alloc_size)) { if (unlikely(__builtin_add_overflow(usable_size, alignment - PAGE_SIZE, &alloc_size))) {
errno = ENOMEM;
return NULL; return NULL;
} }
size_t real_alloc_size; size_t real_alloc_size;
if (__builtin_add_overflow(alloc_size, guard_size * 2, &real_alloc_size)) { if (unlikely(__builtin_add_overflow(alloc_size, guard_size * 2, &real_alloc_size))) {
errno = ENOMEM;
return NULL; return NULL;
} }
@ -219,6 +222,7 @@ static struct slab_metadata *alloc_metadata(struct size_class *c, size_t slab_si
if (unlikely(c->metadata_count == c->metadata_allocated)) { if (unlikely(c->metadata_count == c->metadata_allocated)) {
size_t metadata_max = get_metadata_max(slab_size); size_t metadata_max = get_metadata_max(slab_size);
if (c->metadata_count == metadata_max) { if (c->metadata_count == metadata_max) {
errno = ENOMEM;
return NULL; return NULL;
} }
size_t allocate = c->metadata_allocated * 2; size_t allocate = c->metadata_allocated * 2;
@ -741,7 +745,7 @@ EXPORT void *h_malloc(size_t size) {
EXPORT void *h_calloc(size_t nmemb, size_t size) { EXPORT void *h_calloc(size_t nmemb, size_t size) {
size_t total_size; size_t total_size;
if (__builtin_mul_overflow(nmemb, size, &total_size)) { if (unlikely(__builtin_mul_overflow(nmemb, size, &total_size))) {
errno = ENOMEM; errno = ENOMEM;
return NULL; return NULL;
} }

4
util.h
View File

@ -3,8 +3,8 @@
#include <stdnoreturn.h> #include <stdnoreturn.h>
#define likely(x) __builtin_expect((x), 1) #define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect((x), 0) #define unlikely(x) __builtin_expect(!!(x), 0)
#define COLD __attribute__((cold)) #define COLD __attribute__((cold))
#define UNUSED __attribute__((unused)) #define UNUSED __attribute__((unused))