Skip to content

Commit 4397306

Browse files
marcioAlmadanikic
authored andcommitted
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 4dd70b6 commit 4397306

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
@@ -2053,13 +2053,14 @@ SPL_METHOD(RegexIterator, accept)
20532053

20542054
if (intern->current.data == NULL) {
20552055
RETURN_FALSE;
2056-
} else if (Z_TYPE_P(intern->current.data) == IS_ARRAY) {
2057-
RETURN_FALSE;
20582056
}
20592057

20602058
if (intern->u.regex.flags & REGIT_USE_KEY) {
20612059
subject_ptr = intern->current.key;
20622060
} else {
2061+
if (Z_TYPE_P(intern->current.data) == IS_ARRAY) {
2062+
RETURN_FALSE;
2063+
}
20632064
subject_ptr = intern->current.data;
20642065
}
20652066

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)