From cc9f352b669e7d8ef136b286b9ab5b5c93324111 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 17 Aug 2014 14:08:39 -0400 Subject: [PATCH] Common: Correctly set ptr to null if mmap fails in memory_util On POSIX systems mmap will return MAP_FAILED ((void*)-1) instead of a null pointer. --- src/common/memory_util.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/common/memory_util.cpp b/src/common/memory_util.cpp index 71ef159c3..5cc869aea 100644 --- a/src/common/memory_util.cpp +++ b/src/common/memory_util.cpp @@ -51,14 +51,14 @@ void* AllocateExecutableMemory(size_t size, bool low) // printf("Mapped executable memory at %p (size %ld)\n", ptr, // (unsigned long)size); -#if defined(__FreeBSD__) +#ifdef _WIN32 + if (ptr == nullptr) + { +#else if (ptr == MAP_FAILED) { - ptr = NULL; -#else - if (ptr == NULL) - { -#endif + ptr = nullptr; +#endif PanicAlert("Failed to allocate executable memory"); } #if !defined(_WIN32) && defined(__x86_64__) && !defined(MAP_32BIT) @@ -88,6 +88,9 @@ void* AllocateMemoryPages(size_t size) #else void* ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); + + if (ptr == MAP_FAILED) + ptr = nullptr; #endif // printf("Mapped memory at %p (size %ld)\n", ptr,