Skip to content

Commit 73722df

Browse files
authored
Improve preg_* functions warnings for NUL byte (#13068)
* Improve error messages for preg_ functions * Adjusted tests and fixed formatting * Removed unnecessary strings from preg_* tests * Removed ZPP tests
1 parent 90800b6 commit 73722df

14 files changed

+218
-302
lines changed

ext/pcre/php_pcre.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache_ex(zend_string *regex, bo
649649
if (key != regex) {
650650
zend_string_release_ex(key, 0);
651651
}
652-
php_error_docref(NULL, E_WARNING, "Delimiter must not be alphanumeric, backslash, or NUL");
652+
php_error_docref(NULL, E_WARNING, "Delimiter must not be alphanumeric, backslash, or NUL byte");
653653
pcre_handle_exec_error(PCRE2_ERROR_INTERNAL);
654654
return NULL;
655655
}
@@ -745,7 +745,7 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache_ex(zend_string *regex, bo
745745
if (pp[-1]) {
746746
php_error_docref(NULL, E_WARNING, "Unknown modifier '%c'", pp[-1]);
747747
} else {
748-
php_error_docref(NULL, E_WARNING, "NUL is not a valid modifier");
748+
php_error_docref(NULL, E_WARNING, "NUL byte is not a valid modifier");
749749
}
750750
pcre_handle_exec_error(PCRE2_ERROR_INTERNAL);
751751
efree(pattern);

ext/pcre/tests/bug73392.phpt

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,21 @@ class Foo {
1515
function b() {
1616
return "b";
1717
}
18-
var_dump(preg_replace_callback_array(
19-
array(
20-
"/a/" => 'b', "/b/" => function () { return "c"; }, "/c/" => new Rep, "reporting" => array("Foo", "rep"), "a1" => array("Foo", "rep"),
21-
), 'a'));
18+
var_dump(
19+
preg_replace_callback_array(
20+
[
21+
"/a/" => 'b',
22+
"/b/" => function () {
23+
return 'c';
24+
},
25+
"/c/" => new Rep(),
26+
"reporting" => ["Foo", "rep"],
27+
"a1" => ["Foo", "rep"],
28+
],
29+
'a'
30+
)
31+
);
2232
?>
2333
--EXPECTF--
24-
Warning: preg_replace_callback_array(): Delimiter must not be alphanumeric, backslash, or NUL in %sbug73392.php on line %d
34+
Warning: preg_replace_callback_array(): Delimiter must not be alphanumeric, backslash, or NUL byte in %sbug73392.php on line %d
2535
NULL

ext/pcre/tests/delimiters.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Warning: preg_match(): Empty regular expression in %sdelimiters.php on line 4
2323
bool(false)
2424
int(1)
2525

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

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

40-
Warning: preg_match(): Delimiter must not be alphanumeric, backslash, or NUL in %sdelimiters.php on line 12
40+
Warning: preg_match(): Delimiter must not be alphanumeric, backslash, or NUL byte in %sdelimiters.php on line 12
4141
bool(false)

ext/pcre/tests/null_bytes.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,28 @@ var_dump(preg_match("[abc\0def]", "abc\0fed"));
2929

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

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

40-
Warning: preg_match(): NUL is not a valid modifier in %snull_bytes.php on line 7
40+
Warning: preg_match(): NUL byte is not a valid modifier in %snull_bytes.php on line 7
4141
bool(false)
4242
int(0)
4343
int(1)
4444

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

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

53-
Warning: preg_match(): NUL is not a valid modifier in %snull_bytes.php on line 15
53+
Warning: preg_match(): NUL byte is not a valid modifier in %snull_bytes.php on line 15
5454
bool(false)
5555
int(0)
5656
int(1)

ext/pcre/tests/preg_grep_error1.phpt

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,25 @@ Test preg_grep() function : error conditions - bad regular expressions
33
--FILE--
44
<?php
55
/*
6-
* Function is implemented in ext/pcre/php_pcre.c
7-
*/
6+
* Function is implemented in ext/pcre/php_pcre.c
7+
*/
88
/*
9-
* Testing how preg_grep reacts to being passed bad regexes
10-
*/
11-
echo "*** Testing preg_grep() : error conditions ***\n";
12-
$values = array('abcdef', //Regex without delimiter
13-
'/[a-zA-Z]', //Regex without closing delimiter
14-
'[a-zA-Z]/', //Regex without opening delimiter
15-
'/[a-zA-Z]/F', array('[a-z]', //Array of Regexes
16-
'[A-Z]', '[0-9]'), '/[a-zA-Z]/', //Regex string
17-
);
18-
$array = array(123, 'abc', 'test');
19-
foreach($values as $value) {
20-
@print "\nArg value is $value\n";
9+
* Testing how preg_grep reacts to being passed bad regexes
10+
*/
11+
$values = [
12+
'abcdef', //Regex without delimiter
13+
'/[a-zA-Z]', //Regex without closing delimiter
14+
'[a-zA-Z]/', //Regex without opening delimiter
15+
'/[a-zA-Z]/F',
16+
[
17+
'[a-z]', //Array of Regexes
18+
'[A-Z]',
19+
'[0-9]',
20+
],
21+
'/[a-zA-Z]/', //Regex string
22+
];
23+
$array = [123, 'abc', 'test'];
24+
foreach ($values as $value) {
2125
try {
2226
var_dump(preg_grep($value, $array));
2327
} catch (TypeError $e) {
@@ -30,40 +34,25 @@ try {
3034
} catch (TypeError $e) {
3135
echo $e->getMessage(), "\n";
3236
}
33-
echo "Done"
3437
?>
3538
--EXPECTF--
36-
*** Testing preg_grep() : error conditions ***
3739

38-
Arg value is abcdef
39-
40-
Warning: preg_grep(): Delimiter must not be alphanumeric, backslash, or NUL in %spreg_grep_error1.php on line %d
40+
Warning: preg_grep(): Delimiter must not be alphanumeric, backslash, or NUL byte in %spreg_grep_error1.php on line %d
4141
bool(false)
4242

43-
Arg value is /[a-zA-Z]
44-
4543
Warning: preg_grep(): No ending delimiter '/' found in %spreg_grep_error1.php on line %d
4644
bool(false)
4745

48-
Arg value is [a-zA-Z]/
49-
5046
Warning: preg_grep(): Unknown modifier '/' in %spreg_grep_error1.php on line %d
5147
bool(false)
5248

53-
Arg value is /[a-zA-Z]/F
54-
5549
Warning: preg_grep(): Unknown modifier 'F' in %spreg_grep_error1.php on line %d
5650
bool(false)
57-
58-
Arg value is Array
5951
preg_grep(): Argument #1 ($pattern) must be of type string, array given
60-
61-
Arg value is /[a-zA-Z]/
6252
array(2) {
6353
[1]=>
6454
string(3) "abc"
6555
[2]=>
6656
string(4) "test"
6757
}
6858
preg_grep(): Argument #1 ($pattern) must be of type string, stdClass given
69-
Done

ext/pcre/tests/preg_match_all_error1.phpt

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,25 @@ Test preg_match_all() function : error conditions - bad regular expressions
33
--FILE--
44
<?php
55
/*
6-
* Function is implemented in ext/pcre/php_pcre.c
7-
*/
6+
* Function is implemented in ext/pcre/php_pcre.c
7+
*/
88
/*
9-
* Testing how preg_match_all reacts to being passed the wrong type of regex argument
10-
*/
11-
echo "*** Testing preg_match_all() : error conditions ***\n";
12-
$regex_array = array('abcdef', //Regex without delimiter
13-
'/[a-zA-Z]', //Regex without closing delimiter
14-
'[a-zA-Z]/', //Regex without opening delimiter
15-
'/[a-zA-Z]/F', array('[a-z]', //Array of Regexes
16-
'[A-Z]', '[0-9]'), '/[a-zA-Z]/', //Regex string
17-
);
9+
* Testing how preg_match_all reacts to being passed the wrong type of regex argument
10+
*/
11+
$regex_array = [
12+
'abcdef', //Regex without delimiter
13+
'/[a-zA-Z]', //Regex without closing delimiter
14+
'[a-zA-Z]/', //Regex without opening delimiter
15+
'/[a-zA-Z]/F',
16+
[
17+
'[a-z]', //Array of Regexes
18+
'[A-Z]',
19+
'[0-9]',
20+
],
21+
'/[a-zA-Z]/', //Regex string
22+
];
1823
$subject = 'test';
19-
foreach($regex_array as $regex_value) {
20-
@print "\nArg value is $regex_value\n";
24+
foreach ($regex_array as $regex_value) {
2125
try {
2226
var_dump(preg_match_all($regex_value, $subject, $matches1));
2327
} catch (TypeError $e) {
@@ -34,37 +38,24 @@ try {
3438
var_dump($matches);
3539
?>
3640
--EXPECTF--
37-
*** Testing preg_match_all() : error conditions ***
3841

39-
Arg value is abcdef
40-
41-
Warning: preg_match_all(): Delimiter must not be alphanumeric, backslash, or NUL in %spreg_match_all_error1.php on line %d
42+
Warning: preg_match_all(): Delimiter must not be alphanumeric, backslash, or NUL byte in %spreg_match_all_error1.php on line %d
4243
bool(false)
4344
NULL
4445

45-
Arg value is /[a-zA-Z]
46-
4746
Warning: preg_match_all(): No ending delimiter '/' found in %spreg_match_all_error1.php on line %d
4847
bool(false)
4948
NULL
5049

51-
Arg value is [a-zA-Z]/
52-
5350
Warning: preg_match_all(): Unknown modifier '/' in %spreg_match_all_error1.php on line %d
5451
bool(false)
5552
NULL
5653

57-
Arg value is /[a-zA-Z]/F
58-
5954
Warning: preg_match_all(): Unknown modifier 'F' in %spreg_match_all_error1.php on line %d
6055
bool(false)
6156
NULL
62-
63-
Arg value is Array
6457
preg_match_all(): Argument #1 ($pattern) must be of type string, array given
6558
NULL
66-
67-
Arg value is /[a-zA-Z]/
6859
int(4)
6960
array(1) {
7061
[0]=>

ext/pcre/tests/preg_match_all_error2.phpt

Lines changed: 0 additions & 47 deletions
This file was deleted.

ext/pcre/tests/preg_match_error1.phpt

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,22 @@ Test preg_match() function : error conditions - bad regular expressions
44
<?php
55
/* Function is implemented in ext/pcre/php_pcre.c */
66
/*
7-
* Testing how preg_match reacts to being passed the wrong type of regex argument
8-
*/
9-
echo "*** Testing preg_match() : error conditions ***\n";
10-
$regex_array = array('abcdef', //Regex without delimiter
11-
'/[a-zA-Z]', //Regex without closing delimiter
12-
'[a-zA-Z]/', //Regex without opening delimiter
13-
'/[a-zA-Z]/F', array('[a-z]', //Array of Regexes
14-
'[A-Z]', '[0-9]'), '/[a-zA-Z]/', //Regex string
15-
);
7+
* Testing how preg_match reacts to being passed the wrong type of regex argument
8+
*/
9+
$regex_array = [
10+
'abcdef', //Regex without delimiter
11+
'/[a-zA-Z]', //Regex without closing delimiter
12+
'[a-zA-Z]/', //Regex without opening delimiter
13+
'/[a-zA-Z]/F',
14+
[
15+
'[a-z]', //Array of Regexes
16+
'[A-Z]',
17+
'[0-9]',
18+
],
19+
'/[a-zA-Z]/', //Regex string
20+
];
1621
$subject = 'this is a test';
17-
foreach($regex_array as $regex_value) {
18-
@print "\nArg value is $regex_value\n";
22+
foreach ($regex_array as $regex_value) {
1923
try {
2024
var_dump(preg_match($regex_value, $subject));
2125
} catch (TypeError $e) {
@@ -28,33 +32,21 @@ try {
2832
} catch (TypeError $e) {
2933
echo $e->getMessage(), "\n";
3034
}
35+
3136
?>
3237
--EXPECTF--
33-
*** Testing preg_match() : error conditions ***
34-
35-
Arg value is abcdef
3638

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

40-
Arg value is /[a-zA-Z]
41-
4242
Warning: preg_match(): No ending delimiter '/' found in %spreg_match_error1.php on line %d
4343
bool(false)
4444

45-
Arg value is [a-zA-Z]/
46-
4745
Warning: preg_match(): Unknown modifier '/' in %spreg_match_error1.php on line %d
4846
bool(false)
4947

50-
Arg value is /[a-zA-Z]/F
51-
5248
Warning: preg_match(): Unknown modifier 'F' in %spreg_match_error1.php on line %d
5349
bool(false)
54-
55-
Arg value is Array
5650
preg_match(): Argument #1 ($pattern) must be of type string, array given
57-
58-
Arg value is /[a-zA-Z]/
5951
int(1)
6052
preg_match(): Argument #1 ($pattern) must be of type string, stdClass given

0 commit comments

Comments
 (0)