From 66aa98dbd91119a99ed5a71831302477568e3c2d Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Tue, 28 Aug 2018 22:41:18 -0400 Subject: [PATCH] sanity-check mmap errno on failure too --- malloc.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/malloc.c b/malloc.c index 74e402f..def5225 100644 --- a/malloc.c +++ b/malloc.c @@ -30,7 +30,10 @@ static_assert(sizeof(void *) == 8, "64-bit only"); static void *memory_map(size_t size) { void *p = mmap(NULL, size, PROT_NONE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); - if (p == MAP_FAILED) { + if (unlikely(p == MAP_FAILED)) { + if (errno != ENOMEM) { + fatal_error("non-ENOMEM mmap failure"); + } return NULL; } return p; @@ -38,7 +41,7 @@ static void *memory_map(size_t size) { static int memory_unmap(void *ptr, size_t size) { int ret = munmap(ptr, size); - if (ret && errno != ENOMEM) { + if (unlikely(ret) && errno != ENOMEM) { fatal_error("non-ENOMEM munmap failure"); } return ret; @@ -46,7 +49,7 @@ static int memory_unmap(void *ptr, size_t size) { static int memory_protect(void *ptr, size_t size, int prot) { int ret = mprotect(ptr, size, prot); - if (ret && errno != ENOMEM) { + if (unlikely(ret) && errno != ENOMEM) { fatal_error("non-ENOMEM mprotect failure"); } return ret;