Skip to content

Commit

Permalink
Improve preg_* functions warnings for NUL byte (#13068)
Browse files Browse the repository at this point in the history
* Improve error messages for preg_ functions
* Adjusted tests and fixed formatting
* Removed unnecessary strings from preg_* tests
* Removed ZPP tests
  • Loading branch information
jorgsowa committed Jan 7, 2024
1 parent 90800b6 commit 73722df
Show file tree
Hide file tree
Showing 14 changed files with 218 additions and 302 deletions.
4 changes: 2 additions & 2 deletions ext/pcre/php_pcre.c
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache_ex(zend_string *regex, bo
if (key != regex) {
zend_string_release_ex(key, 0);
}
php_error_docref(NULL, E_WARNING, "Delimiter must not be alphanumeric, backslash, or NUL");
php_error_docref(NULL, E_WARNING, "Delimiter must not be alphanumeric, backslash, or NUL byte");
pcre_handle_exec_error(PCRE2_ERROR_INTERNAL);
return NULL;
}
Expand Down Expand Up @@ -745,7 +745,7 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache_ex(zend_string *regex, bo
if (pp[-1]) {
php_error_docref(NULL, E_WARNING, "Unknown modifier '%c'", pp[-1]);
} else {
php_error_docref(NULL, E_WARNING, "NUL is not a valid modifier");
php_error_docref(NULL, E_WARNING, "NUL byte is not a valid modifier");
}
pcre_handle_exec_error(PCRE2_ERROR_INTERNAL);
efree(pattern);
Expand Down
20 changes: 15 additions & 5 deletions ext/pcre/tests/bug73392.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,21 @@ class Foo {
function b() {
return "b";
}
var_dump(preg_replace_callback_array(
array(
"/a/" => 'b', "/b/" => function () { return "c"; }, "/c/" => new Rep, "reporting" => array("Foo", "rep"), "a1" => array("Foo", "rep"),
), 'a'));
var_dump(
preg_replace_callback_array(
[
"/a/" => 'b',
"/b/" => function () {
return 'c';
},
"/c/" => new Rep(),
"reporting" => ["Foo", "rep"],
"a1" => ["Foo", "rep"],
],
'a'
)
);
?>
--EXPECTF--
Warning: preg_replace_callback_array(): Delimiter must not be alphanumeric, backslash, or NUL in %sbug73392.php on line %d
Warning: preg_replace_callback_array(): Delimiter must not be alphanumeric, backslash, or NUL byte in %sbug73392.php on line %d
NULL
4 changes: 2 additions & 2 deletions ext/pcre/tests/delimiters.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Warning: preg_match(): Empty regular expression in %sdelimiters.php on line 4
bool(false)
int(1)

Warning: preg_match(): Delimiter must not be alphanumeric, backslash, or NUL in %sdelimiters.php on line 6
Warning: preg_match(): Delimiter must not be alphanumeric, backslash, or NUL byte in %sdelimiters.php on line 6
bool(false)
int(1)

Expand All @@ -37,5 +37,5 @@ bool(false)
Warning: preg_match(): No ending matching delimiter '}' found in %sdelimiters.php on line 11
bool(false)

Warning: preg_match(): Delimiter must not be alphanumeric, backslash, or NUL in %sdelimiters.php on line 12
Warning: preg_match(): Delimiter must not be alphanumeric, backslash, or NUL byte in %sdelimiters.php on line 12
bool(false)
12 changes: 6 additions & 6 deletions ext/pcre/tests/null_bytes.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,28 @@ var_dump(preg_match("[abc\0def]", "abc\0fed"));

?>
--EXPECTF--
Warning: preg_match(): Delimiter must not be alphanumeric, backslash, or NUL in %snull_bytes.php on line 3
Warning: preg_match(): Delimiter must not be alphanumeric, backslash, or NUL byte in %snull_bytes.php on line 3
bool(false)
int(0)
int(1)

Warning: preg_match(): NUL is not a valid modifier in %snull_bytes.php on line 6
Warning: preg_match(): NUL byte is not a valid modifier in %snull_bytes.php on line 6
bool(false)

Warning: preg_match(): NUL is not a valid modifier in %snull_bytes.php on line 7
Warning: preg_match(): NUL byte is not a valid modifier in %snull_bytes.php on line 7
bool(false)
int(0)
int(1)

Warning: preg_match(): Delimiter must not be alphanumeric, backslash, or NUL in %snull_bytes.php on line 11
Warning: preg_match(): Delimiter must not be alphanumeric, backslash, or NUL byte in %snull_bytes.php on line 11
bool(false)
int(0)
int(1)

Warning: preg_match(): NUL is not a valid modifier in %snull_bytes.php on line 14
Warning: preg_match(): NUL byte is not a valid modifier in %snull_bytes.php on line 14
bool(false)

Warning: preg_match(): NUL is not a valid modifier in %snull_bytes.php on line 15
Warning: preg_match(): NUL byte is not a valid modifier in %snull_bytes.php on line 15
bool(false)
int(0)
int(1)
Expand Down
49 changes: 19 additions & 30 deletions ext/pcre/tests/preg_grep_error1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@ Test preg_grep() function : error conditions - bad regular expressions
--FILE--
<?php
/*
* Function is implemented in ext/pcre/php_pcre.c
*/
* 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";
* Testing how preg_grep reacts to being passed bad regexes
*/
$values = [
'abcdef', //Regex without delimiter
'/[a-zA-Z]', //Regex without closing delimiter
'[a-zA-Z]/', //Regex without opening delimiter
'/[a-zA-Z]/F',
[
'[a-z]', //Array of Regexes
'[A-Z]',
'[0-9]',
],
'/[a-zA-Z]/', //Regex string
];
$array = [123, 'abc', 'test'];
foreach ($values as $value) {
try {
var_dump(preg_grep($value, $array));
} catch (TypeError $e) {
Expand All @@ -30,40 +34,25 @@ try {
} 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
Warning: preg_grep(): Delimiter must not be alphanumeric, backslash, or NUL byte 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
45 changes: 18 additions & 27 deletions ext/pcre/tests/preg_match_all_error1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@ Test preg_match_all() function : error conditions - bad regular expressions
--FILE--
<?php
/*
* Function is implemented in ext/pcre/php_pcre.c
*/
* Function is implemented in ext/pcre/php_pcre.c
*/
/*
* Testing how preg_match_all reacts to being passed the wrong type of regex argument
*/
echo "*** Testing preg_match_all() : error conditions ***\n";
$regex_array = 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
);
* Testing how preg_match_all reacts to being passed the wrong type of regex argument
*/
$regex_array = [
'abcdef', //Regex without delimiter
'/[a-zA-Z]', //Regex without closing delimiter
'[a-zA-Z]/', //Regex without opening delimiter
'/[a-zA-Z]/F',
[
'[a-z]', //Array of Regexes
'[A-Z]',
'[0-9]',
],
'/[a-zA-Z]/', //Regex string
];
$subject = 'test';
foreach($regex_array as $regex_value) {
@print "\nArg value is $regex_value\n";
foreach ($regex_array as $regex_value) {
try {
var_dump(preg_match_all($regex_value, $subject, $matches1));
} catch (TypeError $e) {
Expand All @@ -34,37 +38,24 @@ try {
var_dump($matches);
?>
--EXPECTF--
*** Testing preg_match_all() : error conditions ***

Arg value is abcdef

Warning: preg_match_all(): Delimiter must not be alphanumeric, backslash, or NUL in %spreg_match_all_error1.php on line %d
Warning: preg_match_all(): Delimiter must not be alphanumeric, backslash, or NUL byte in %spreg_match_all_error1.php on line %d
bool(false)
NULL

Arg value is /[a-zA-Z]

Warning: preg_match_all(): No ending delimiter '/' found in %spreg_match_all_error1.php on line %d
bool(false)
NULL

Arg value is [a-zA-Z]/

Warning: preg_match_all(): Unknown modifier '/' in %spreg_match_all_error1.php on line %d
bool(false)
NULL

Arg value is /[a-zA-Z]/F

Warning: preg_match_all(): Unknown modifier 'F' in %spreg_match_all_error1.php on line %d
bool(false)
NULL

Arg value is Array
preg_match_all(): Argument #1 ($pattern) must be of type string, array given
NULL

Arg value is /[a-zA-Z]/
int(4)
array(1) {
[0]=>
Expand Down
47 changes: 0 additions & 47 deletions ext/pcre/tests/preg_match_all_error2.phpt

This file was deleted.

42 changes: 17 additions & 25 deletions ext/pcre/tests/preg_match_error1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@ Test preg_match() function : error conditions - bad regular expressions
<?php
/* Function is implemented in ext/pcre/php_pcre.c */
/*
* Testing how preg_match reacts to being passed the wrong type of regex argument
*/
echo "*** Testing preg_match() : error conditions ***\n";
$regex_array = 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
);
* Testing how preg_match reacts to being passed the wrong type of regex argument
*/
$regex_array = [
'abcdef', //Regex without delimiter
'/[a-zA-Z]', //Regex without closing delimiter
'[a-zA-Z]/', //Regex without opening delimiter
'/[a-zA-Z]/F',
[
'[a-z]', //Array of Regexes
'[A-Z]',
'[0-9]',
],
'/[a-zA-Z]/', //Regex string
];
$subject = 'this is a test';
foreach($regex_array as $regex_value) {
@print "\nArg value is $regex_value\n";
foreach ($regex_array as $regex_value) {
try {
var_dump(preg_match($regex_value, $subject));
} catch (TypeError $e) {
Expand All @@ -28,33 +32,21 @@ try {
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}

?>
--EXPECTF--
*** Testing preg_match() : error conditions ***

Arg value is abcdef

Warning: preg_match(): Delimiter must not be alphanumeric, backslash, or NUL in %spreg_match_error1.php on line %d
Warning: preg_match(): Delimiter must not be alphanumeric, backslash, or NUL byte in %spreg_match_error1.php on line %d
bool(false)

Arg value is /[a-zA-Z]

Warning: preg_match(): No ending delimiter '/' found in %spreg_match_error1.php on line %d
bool(false)

Arg value is [a-zA-Z]/

Warning: preg_match(): Unknown modifier '/' in %spreg_match_error1.php on line %d
bool(false)

Arg value is /[a-zA-Z]/F

Warning: preg_match(): Unknown modifier 'F' in %spreg_match_error1.php on line %d
bool(false)

Arg value is Array
preg_match(): Argument #1 ($pattern) must be of type string, array given

Arg value is /[a-zA-Z]/
int(1)
preg_match(): Argument #1 ($pattern) must be of type string, stdClass given

0 comments on commit 73722df

Please sign in to comment.