Skip to content

Commit 66ce205

Browse files
committed
Fix incorrect zval type_flags in preg_replace_callback_array() for immutable arrays
The ZVAL_ARR macro always set the zval type_info to IS_ARRAY_EX, even if the hash table is immutable. Since in preg_replace_callback_array() we can return the passed array directly, and that passed array can be immutable, we need to reset the type_flags to keep the VM from performing ref-counting on the array. Fixes GH-10968 Closes GH-10970
1 parent 41bbb11 commit 66ce205

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ PHP NEWS
99
. Fixed bug #80602 (Segfault when using DOMChildNode::before()).
1010
(Nathan Freeman)
1111

12+
- PCRE:
13+
. Fixed bug GH-10968 (Segfault in preg_replace_callback_array()). (ilutov)
14+
1215
13 Apr 2023, PHP 8.1.18
1316

1417
- Core:

ext/pcre/php_pcre.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2479,7 +2479,12 @@ PHP_FUNCTION(preg_replace_callback_array)
24792479
}
24802480

24812481
if (subject_ht) {
2482-
RETURN_ARR(subject_ht);
2482+
RETVAL_ARR(subject_ht);
2483+
// Unset the type_flags of immutable arrays to prevent the VM from performing refcounting
2484+
if (GC_FLAGS(subject_ht) & IS_ARRAY_IMMUTABLE) {
2485+
Z_TYPE_FLAGS_P(return_value) = 0;
2486+
}
2487+
return;
24832488
} else {
24842489
RETURN_STR(subject_str);
24852490
}

ext/pcre/tests/gh10968.phpt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
GH-10968: preg_replace_callback_array() segmentation fault
3+
--FILE--
4+
<?php
5+
var_dump(preg_replace_callback_array([], []));
6+
var_dump(preg_replace_callback_array([], ''));
7+
?>
8+
--EXPECT--
9+
array(0) {
10+
}
11+
string(0) ""

0 commit comments

Comments
 (0)