Skip to content

Add assertion that php_pcre_replace_impl() returns a valid pointer in SPL RegexIterator() #10520

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion ext/spl/spl_iterators.c
Original file line number Diff line number Diff line change
Expand Up @@ -1866,6 +1866,12 @@ PHP_METHOD(RegexIterator, accept)
}

result = php_pcre_replace_impl(intern->u.regex.pce, subject, ZSTR_VAL(subject), ZSTR_LEN(subject), replacement_str, -1, &count);
zend_string_release(replacement_str);
zend_string_release(subject);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this PR was forgotten, but you cannot release subject here as it would cause a double free at the end of the function. This should only be released in the !result block.
Please test this with a non-interned subject string.


if (!result) {
RETURN_FALSE;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably have to release subject as well.

}

if (intern->u.regex.flags & REGIT_USE_KEY) {
zval_ptr_dtor(&intern->current.key);
Expand All @@ -1875,7 +1881,6 @@ PHP_METHOD(RegexIterator, accept)
ZVAL_STR(&intern->current.data, result);
}

zend_string_release(replacement_str);
RETVAL_BOOL(count > 0);
}
}
Expand Down
37 changes: 37 additions & 0 deletions ext/spl/tests/gh8271.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--TEST--
GH-8271: NULL pointer dereference in RegexIterator::accept()
--EXTENSIONS--
spl
--FILE--
<?php
function words() {
yield "f\xC3oo";
yield "bar";
yield "baz";
}

$it = new RegexIterator(words(), '/a/u', RegexIterator::REPLACE);
var_dump(iterator_to_array($it));

function wordsForEach() {
foreach (["f\xC3oo", "bar", "baz"] as $word) {
yield $word;
}
}
$it = new RegexIterator(wordsForEach(), '/a/u', RegexIterator::REPLACE);
var_dump(iterator_to_array($it));

?>
--EXPECT--
array(2) {
[1]=>
string(2) "br"
[2]=>
string(2) "bz"
}
array(2) {
[1]=>
string(2) "br"
[2]=>
string(2) "bz"
}