php-src/ext/pcre/tests/preg_grep_error1.phpt
tobil4sk 5bb3e233db
Implement #77726: Allow null character in regex patterns
In 8b3c1a3, this was disallowed to fix #55856, which was a security
issue caused by the /e modifier. The fix that was made was the
"Easier fix" as described in the original report.

With this fix, pattern strings are no longer treated as null terminated,
so null characters can be placed inside and matched against with regex
patterns without security problems, so there is no longer a reason to
give the error. Allowing this is consistent with the behaviour of many
other languages, including JavaScript, and thanks to PCRE2[0], it does
not require manually escaping null characters. Now that we can avoid the
error here without the cost of escaping characters, there is really no
need anymore to stray here from the conventional behaviour.

Currently, null characters are still disallowed before the first
delimiter and in the options section at the end of a regex string, but
these error messages have been updated.

[0] Since PCRE2, pattern strings no longer have to be null terminated,
and raw null characters match as normal.

Closes GH-8114.
2022-06-17 19:30:44 +02:00

69 lines
1.7 KiB
PHP

--TEST--
Test preg_grep() function : error conditions - bad regular expressions
--FILE--
<?php
/*
* Function is implemented in ext/pcre/php_pcre.c
*/
/*
* Testing how preg_grep reacts to being passed bad regexes
*/
echo "*** Testing preg_grep() : error conditions ***\n";
$values = array('abcdef', //Regex without delimiter
'/[a-zA-Z]', //Regex without closing delimiter
'[a-zA-Z]/', //Regex without opening delimiter
'/[a-zA-Z]/F', array('[a-z]', //Array of Regexes
'[A-Z]', '[0-9]'), '/[a-zA-Z]/', //Regex string
);
$array = array(123, 'abc', 'test');
foreach($values as $value) {
@print "\nArg value is $value\n";
try {
var_dump(preg_grep($value, $array));
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}
}
$value = new stdclass(); //Object
try {
var_dump(preg_grep($value, $array));
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}
echo "Done"
?>
--EXPECTF--
*** Testing preg_grep() : error conditions ***
Arg value is abcdef
Warning: preg_grep(): Delimiter must not be alphanumeric, backslash, or NUL in %spreg_grep_error1.php on line %d
bool(false)
Arg value is /[a-zA-Z]
Warning: preg_grep(): No ending delimiter '/' found in %spreg_grep_error1.php on line %d
bool(false)
Arg value is [a-zA-Z]/
Warning: preg_grep(): Unknown modifier '/' in %spreg_grep_error1.php on line %d
bool(false)
Arg value is /[a-zA-Z]/F
Warning: preg_grep(): Unknown modifier 'F' in %spreg_grep_error1.php on line %d
bool(false)
Arg value is Array
preg_grep(): Argument #1 ($pattern) must be of type string, array given
Arg value is /[a-zA-Z]/
array(2) {
[1]=>
string(3) "abc"
[2]=>
string(4) "test"
}
preg_grep(): Argument #1 ($pattern) must be of type string, stdClass given
Done