Skip to content

Commit 39a3266

Browse files
committed
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.
1 parent 5a32b51 commit 39a3266

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ PHP NEWS
1010
. Fixed bug GH-14596 (crashes with ASAN and ZEND_RC_DEBUG=1).
1111
(David Carlier)
1212

13+
- Shmop:
14+
. Fixed bug GH-14537 (shmop Windows 11 crashes the process). (nielsdos)
1315

1416
04 Jul 2024, PHP 8.2.21
1517

TSRM/tsrm_win32.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,7 @@ TSRM_API int shmget(key_t key, size_t size, int flags)
709709
CloseHandle(shm->segment);
710710
}
711711
UnmapViewOfFile(shm->descriptor);
712+
shm->descriptor = NULL;
712713
return -1;
713714
}
714715

@@ -744,8 +745,8 @@ TSRM_API int shmdt(const void *shmaddr)
744745
shm->descriptor->shm_lpid = getpid();
745746
shm->descriptor->shm_nattch--;
746747

747-
ret = 1;
748-
if (!ret && shm->descriptor->shm_nattch <= 0) {
748+
ret = 0;
749+
if (shm->descriptor->shm_nattch <= 0) {
749750
ret = UnmapViewOfFile(shm->descriptor) ? 0 : -1;
750751
shm->descriptor = NULL;
751752
}

ext/shmop/tests/gh14537.phpt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
GH-14537: shmop Windows 11 crashes the process
3+
--EXTENSIONS--
4+
shmop
5+
--SKIPIF--
6+
<?php
7+
if (PHP_OS_FAMILY !== 'Windows') die('skip only for Windows');
8+
?>
9+
--FILE--
10+
<?php
11+
$str = 'Hello World';
12+
13+
$shm_key = ftok(__FILE__, 'p');
14+
15+
$shm_id1 = shmop_open($shm_key, 'c', 0644, strlen($str));
16+
shmop_delete($shm_id1);
17+
var_dump($shm_id1);
18+
19+
$shm_id2 = shmop_open($shm_key, 'c', 0644, strlen($str) + 10);
20+
var_dump($shm_id2);
21+
?>
22+
--EXPECTF--
23+
object(Shmop)#1 (0) {
24+
}
25+
26+
Warning: shmop_open(): Unable to attach or create shared memory segment "No error" in %s on line %d
27+
bool(false)

0 commit comments

Comments
 (0)