Skip to content

Commit 4bbbe6d

Browse files
nielsdosGirgias
authored andcommitted
Fix substr_replace with slots in repl_ht being UNDEF
The check that was supposed to check whether the array slot was UNDEF was wrong and never triggered. This resulted in a replacement with the empty string or the wrong string instead of the correct one. The correct check pattern can be observed higher up in the function's code. Closes phpGH-10323 Signed-off-by: George Peter Banyard <[email protected]>
1 parent 7d98e3e commit 4bbbe6d

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ PHP NEWS
4848

4949
- Standard:
5050
. Fix GH-10187 (Segfault in stripslashes() with arm64). (nielsdos)
51+
. Fix substr_replace with slots in repl_ht being UNDEF. (nielsdos)
5152

5253
- TSRM:
5354
. Fixed Windows shmget() wrt. IPC_PRIVATE. (Tyson Andre)

ext/standard/string.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2472,7 +2472,7 @@ PHP_FUNCTION(substr_replace)
24722472
if (repl_ht) {
24732473
while (repl_idx < repl_ht->nNumUsed) {
24742474
tmp_repl = &repl_ht->arData[repl_idx].val;
2475-
if (repl_ht != IS_UNDEF) {
2475+
if (Z_TYPE_P(tmp_repl) != IS_UNDEF) {
24762476
break;
24772477
}
24782478
repl_idx++;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
substr_replace() function - array with unset
3+
--FILE--
4+
<?php
5+
6+
$replacement = ['A', 'C', 'B'];
7+
unset($replacement[1]);
8+
$newarr = substr_replace(['1 string', '2 string'], $replacement, 0);
9+
print_r($newarr);
10+
11+
$replacement = ['foo', 42 => 'bar', 'baz'];
12+
unset($replacement[42]);
13+
$newarr = substr_replace(['1 string', '2 string'], $replacement, 0);
14+
print_r($newarr);
15+
16+
?>
17+
--EXPECT--
18+
Array
19+
(
20+
[0] => A
21+
[1] => B
22+
)
23+
Array
24+
(
25+
[0] => foo
26+
[1] => baz
27+
)

0 commit comments

Comments
 (0)