Skip to content

Commit a050565

Browse files
committed
fix bug related to php#865
In case USE_KEY flag is active, RegexIterator->accept() should keep it's old behavior which is to accept keys mapping arrays. This broke after PHP 5.5 but was not noticed due to lack of tests for USE_KEY.
1 parent 5fccf64 commit a050565

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

ext/spl/spl_iterators.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2038,13 +2038,14 @@ SPL_METHOD(RegexIterator, accept)
20382038

20392039
if (Z_TYPE(intern->current.data) == IS_UNDEF) {
20402040
RETURN_FALSE;
2041-
} else if (Z_TYPE(intern->current.data) == IS_ARRAY) {
2042-
RETURN_FALSE;
20432041
}
20442042

20452043
if (intern->u.regex.flags & REGIT_USE_KEY) {
20462044
subject = zval_get_string(&intern->current.key);
20472045
} else {
2046+
if (Z_TYPE(intern->current.data) == IS_ARRAY) {
2047+
RETURN_FALSE;
2048+
}
20482049
subject = zval_get_string(&intern->current.data);
20492050
}
20502051

ext/spl/tests/bug68128-USE_KEY.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
Bug #68128 - RecursiveRegexIterator raises "Array to string conversion" notice
3+
--FILE--
4+
<?php
5+
6+
$arrayIterator = new ArrayIterator(array('key 1' => 'value 1', 'key 2' => ['value 2']));
7+
$regexIterator = new RegexIterator($arrayIterator, '/^key/', RegexIterator::MATCH, RegexIterator::USE_KEY);
8+
9+
foreach ($regexIterator as $key => $value) {
10+
var_dump($key, $value);
11+
}
12+
13+
?>
14+
--EXPECT--
15+
string(5) "key 1"
16+
string(7) "value 1"
17+
string(5) "key 2"
18+
array(1) {
19+
[0]=>
20+
string(7) "value 2"
21+
}

ext/spl/tests/iterator_053.phpt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ bool(true)
5050
bool(true)
5151
bool(true)
5252
bool(true)
53-
bool(false)
53+
bool(true)
5454
bool(true)
5555
bool(true)
5656
bool(true)
@@ -124,7 +124,20 @@ array(2) {
124124
string(1) "4"
125125
}
126126
}
127-
bool(false)
127+
bool(true)
128+
int(5)
129+
array(2) {
130+
[0]=>
131+
array(1) {
132+
[0]=>
133+
string(1) "5"
134+
}
135+
[1]=>
136+
array(1) {
137+
[0]=>
138+
string(1) "5"
139+
}
140+
}
128141
bool(true)
129142
int(6)
130143
array(2) {

0 commit comments

Comments
 (0)