From 39a3266576e0f243af64df0673f352f759749784 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Fri, 28 Jun 2024 18:26:47 +0200 Subject: [PATCH] Fix GH-14537: shmop Windows 11 crashes the process The error handling code isn't entirely right in two places. One of the code blocks is dead because of an always-false condition, and another code block is missing the assignment of a NULL pointer. Getting the exact same behaviour is not entirely possible because you can't extend the size of a shared memory region after it was made with the Windows APIs we use, unless we destroy the region and recreate it, but that has other consequences. However, it certainly shouldn't crash. Closes GH-14707. --- NEWS | 2 ++ TSRM/tsrm_win32.c | 5 +++-- ext/shmop/tests/gh14537.phpt | 27 +++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 ext/shmop/tests/gh14537.phpt diff --git a/NEWS b/NEWS index 34c8d2e873a..ac4460a55e0 100644 --- a/NEWS +++ b/NEWS @@ -10,6 +10,8 @@ PHP NEWS . Fixed bug GH-14596 (crashes with ASAN and ZEND_RC_DEBUG=1). (David Carlier) +- Shmop: + . Fixed bug GH-14537 (shmop Windows 11 crashes the process). (nielsdos) 04 Jul 2024, PHP 8.2.21 diff --git a/TSRM/tsrm_win32.c b/TSRM/tsrm_win32.c index dc8f9fefa3a..0af03b6ed89 100644 --- a/TSRM/tsrm_win32.c +++ b/TSRM/tsrm_win32.c @@ -709,6 +709,7 @@ TSRM_API int shmget(key_t key, size_t size, int flags) CloseHandle(shm->segment); } UnmapViewOfFile(shm->descriptor); + shm->descriptor = NULL; return -1; } @@ -744,8 +745,8 @@ TSRM_API int shmdt(const void *shmaddr) shm->descriptor->shm_lpid = getpid(); shm->descriptor->shm_nattch--; - ret = 1; - if (!ret && shm->descriptor->shm_nattch <= 0) { + ret = 0; + if (shm->descriptor->shm_nattch <= 0) { ret = UnmapViewOfFile(shm->descriptor) ? 0 : -1; shm->descriptor = NULL; } diff --git a/ext/shmop/tests/gh14537.phpt b/ext/shmop/tests/gh14537.phpt new file mode 100644 index 00000000000..05af26a70ff --- /dev/null +++ b/ext/shmop/tests/gh14537.phpt @@ -0,0 +1,27 @@ +--TEST-- +GH-14537: shmop Windows 11 crashes the process +--EXTENSIONS-- +shmop +--SKIPIF-- + +--FILE-- + +--EXPECTF-- +object(Shmop)#1 (0) { +} + +Warning: shmop_open(): Unable to attach or create shared memory segment "No error" in %s on line %d +bool(false)