Skip to content

Commit 61a190c

Browse files
authored
Prevents tests from leaking System V IPC objects (#22912)
* Clean up IPC objects in arginfo mismatch test * Fix GH-16592 test leaking a message queue
1 parent 955e2ea commit 61a190c

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

Zend/tests/arginfo_zpp_mismatch.phpt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,36 @@ if (getenv('SKIP_MSAN')) die("skip msan misses interceptors for some functions")
1010

1111
require __DIR__ . "/arginfo_zpp_mismatch.inc";
1212

13+
function testWeakSysvIpcFunction($function): bool {
14+
if (!in_array($function, ['msg_queue_exists', 'msg_get_queue', 'sem_get', 'shm_attach'], true)) {
15+
return false;
16+
}
17+
18+
for ($argumentCount = 0; $argumentCount <= 8; $argumentCount++) {
19+
$arguments = array_fill(0, $argumentCount, null);
20+
if ($function === 'msg_queue_exists' && $argumentCount >= 1) {
21+
$arguments[0] = 1;
22+
}
23+
if (($function === 'sem_get' || $function === 'shm_attach') && $argumentCount >= 3) {
24+
$arguments[2] = 0600;
25+
}
26+
27+
try {
28+
$result = @$function(...$arguments);
29+
if ($result instanceof SysvMessageQueue) {
30+
msg_remove_queue($result);
31+
} elseif ($result instanceof SysvSemaphore) {
32+
sem_remove($result);
33+
} elseif ($result instanceof SysvSharedMemory) {
34+
shm_remove($result);
35+
}
36+
} catch (Throwable) {
37+
}
38+
}
39+
40+
return true;
41+
}
42+
1343
function test($function) {
1444
if (skipFunction($function)) {
1545
return;
@@ -21,6 +51,10 @@ function test($function) {
2151
} else {
2252
echo "Testing " . get_class($function[0]) . "::$function[1]\n";
2353
}
54+
if (testWeakSysvIpcFunction($function)) {
55+
ob_end_clean();
56+
return;
57+
}
2458
try {
2559
@$function();
2660
} catch (Throwable) {

ext/sysvmsg/tests/gh16592.phpt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ class Test {
88
function __serialize() {}
99
}
1010

11-
$q = msg_get_queue(1);
11+
// use a private queue, so we only remove our own.
12+
$q = msg_get_queue(0, 0600);
1213
try {
1314
msg_send($q, 1, new Test, true);
1415
} catch (\TypeError $e) {
1516
echo $e->getMessage();
17+
} finally {
18+
msg_remove_queue($q);
1619
}
1720
?>
1821
--EXPECT--

0 commit comments

Comments
 (0)