From f25cc8b4df5e4ac9a13ea8a78595d2ea274d033b Mon Sep 17 00:00:00 2001 From: Levi Neely Date: Tue, 3 Mar 2026 11:58:41 +0100 Subject: [PATCH] Fix MAP_FAILED detection in pmem_mmap 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 --- 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