From c514fa1a29cbf15f6e7a28462dc077e23868731a Mon Sep 17 00:00:00 2001 From: Levi Neely <141506390+lneely@users.noreply.github.com> Date: Tue, 3 Mar 2026 12:03:30 +0100 Subject: [PATCH] Fix MAP_FAILED detection in pmem_mmap (#298) mmap(2) returns MAP_FAILED ((void*)-1) on error, not NULL. The check 'if (likely(ret))' treats MAP_FAILED as truthy, so the emergency retry path is never reached and callers receive MAP_FAILED as a valid pointer. Normalize pmem_mmap() to return NULL on failure (converting MAP_FAILED to NULL in the mmap path). This confines the platform-specific error handling to one place and eliminates the need for preprocessor checks in pmem_mmap_safe() and psync_mmap_anon_emergency(). Fixes GH #226 Co-authored-by: Levi Neely --- pclsync/pmem.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pclsync/pmem.c b/pclsync/pmem.c index 5dec1b1..c62b268 100644 --- a/pclsync/pmem.c +++ b/pclsync/pmem.c @@ -6,8 +6,9 @@ void *pmem_mmap(size_t size) { #if defined(MAP_ANONYMOUS) - return mmap(NULL, size, PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + void *ret = mmap(NULL, size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + return (ret == MAP_FAILED) ? NULL : ret; #else return malloc(size); #endif