Skip to content

Distinguish between unmatched subpatterns and empty matches in preg_*() #1303

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

Merged
merged 4 commits into from
Jan 6, 2017
Merged
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
64 changes: 48 additions & 16 deletions ext/pcre/php_pcre.c
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,11 @@ static inline void add_offset_pair(zval *result, char *str, int len, int offset,
array_init_size(&match_pair, 2);

/* Add (match, offset) to the return value */
ZVAL_STRINGL(&tmp, str, len);
if (offset < 0) { /* unset substring */
ZVAL_NULL(&tmp);
} else {
ZVAL_STRINGL(&tmp, str, len);
}
zend_hash_next_index_insert_new(Z_ARRVAL(match_pair), &tmp);
ZVAL_LONG(&tmp, offset);
zend_hash_next_index_insert_new(Z_ARRVAL(match_pair), &tmp);
Expand Down Expand Up @@ -741,8 +745,12 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, char *subject, int subjec
}
} else {
for (i = 0; i < count; i++) {
add_next_index_stringl(&match_sets[i], (char *)stringlist[i],
offsets[(i<<1)+1] - offsets[i<<1]);
if (offsets[i<<1] < 0) { /* unset substring */
add_next_index_null(&match_sets[i]);
} else {
add_next_index_stringl(&match_sets[i], (char *)stringlist[i],
offsets[(i<<1)+1] - offsets[i<<1]);
}
}
}
/* Add MARK, if available */
Expand All @@ -755,11 +763,11 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, char *subject, int subjec
/*
* If the number of captured subpatterns on this run is
* less than the total possible number, pad the result
* arrays with empty strings.
* arrays with NULLs.
*/
if (count < num_subpats) {
for (; i < num_subpats; i++) {
add_next_index_string(&match_sets[i], "");
add_next_index_null(&match_sets[i]);
}
}
} else {
Expand All @@ -776,11 +784,19 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, char *subject, int subjec
} else {
for (i = 0; i < count; i++) {
if (subpat_names[i]) {
add_assoc_stringl(&result_set, subpat_names[i], (char *)stringlist[i],
if (offsets[i<<1] < 0) { /* unset substring */
add_assoc_null(&result_set, subpat_names[i]);
} else {
add_assoc_stringl(&result_set, subpat_names[i], (char *)stringlist[i],
offsets[(i<<1)+1] - offsets[i<<1]);
}
}
if (offsets[i<<1] < 0) { /* unset substring */
add_next_index_null(&result_set);
} else {
add_next_index_stringl(&result_set, (char *)stringlist[i],
offsets[(i<<1)+1] - offsets[i<<1]);
}
add_next_index_stringl(&result_set, (char *)stringlist[i],
offsets[(i<<1)+1] - offsets[i<<1]);
}
}
} else {
Expand All @@ -791,8 +807,12 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, char *subject, int subjec
}
} else {
for (i = 0; i < count; i++) {
add_next_index_stringl(&result_set, (char *)stringlist[i],
offsets[(i<<1)+1] - offsets[i<<1]);
if (offsets[i<<1] < 0) { /* unset substring */
add_next_index_null(&result_set);
} else {
add_next_index_stringl(&result_set, (char *)stringlist[i],
offsets[(i<<1)+1] - offsets[i<<1]);
}
}
}
}
Expand All @@ -815,11 +835,19 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, char *subject, int subjec
} else {
for (i = 0; i < count; i++) {
if (subpat_names[i]) {
add_assoc_stringl(subpats, subpat_names[i], (char *)stringlist[i],
offsets[(i<<1)+1] - offsets[i<<1]);
if (offsets[i<<1] < 0) { /* unset substring */
add_assoc_null(subpats, subpat_names[i]);
} else {
add_assoc_stringl(subpats, subpat_names[i], (char *)stringlist[i],
offsets[(i<<1)+1] - offsets[i<<1]);
}
}
if (offsets[i<<1] < 0) { /* unset substring */
add_next_index_null(subpats);
} else {
add_next_index_stringl(subpats, (char *)stringlist[i],
offsets[(i<<1)+1] - offsets[i<<1]);
}
add_next_index_stringl(subpats, (char *)stringlist[i],
offsets[(i<<1)+1] - offsets[i<<1]);
}
}
} else {
Expand All @@ -831,8 +859,12 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, char *subject, int subjec
}
} else {
for (i = 0; i < count; i++) {
add_next_index_stringl(subpats, (char *)stringlist[i],
offsets[(i<<1)+1] - offsets[i<<1]);
if (offsets[i<<1] < 0) { /* unset substring */
add_next_index_null(subpats);
} else {
add_next_index_stringl(subpats, (char *)stringlist[i],
offsets[(i<<1)+1] - offsets[i<<1]);
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions ext/pcre/tests/001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ array(10) {
[2]=>
string(2) "06"
[3]=>
string(0) ""
NULL
["month"]=>
string(2) "12"
[4]=>
Expand All @@ -75,7 +75,7 @@ array(10) {
[2]=>
string(2) "12"
[3]=>
string(0) ""
NULL
["month"]=>
string(3) "Aug"
[4]=>
Expand Down
4 changes: 2 additions & 2 deletions ext/pcre/tests/003.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ array(10) {
[0]=>
string(2) "20"
[1]=>
string(0) ""
NULL
}
["month"]=>
array(2) {
Expand Down Expand Up @@ -127,7 +127,7 @@ array(2) {
[2]=>
string(2) "12"
[3]=>
string(0) ""
NULL
["month"]=>
string(3) "Aug"
[4]=>
Expand Down
6 changes: 3 additions & 3 deletions ext/pcre/tests/004.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ array(2) {
[1]=>
string(12) "unsigned int"
[2]=>
string(0) ""
NULL
[3]=>
string(0) ""
[4]=>
Expand All @@ -41,13 +41,13 @@ array(2) {
[1]=>
string(5) "short"
[2]=>
string(0) ""
NULL
[3]=>
string(0) ""
[4]=>
string(1) "a"
[5]=>
string(0) ""
NULL
[6]=>
string(3) ", b"
}
Expand Down
18 changes: 18 additions & 0 deletions ext/pcre/tests/bug61780.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Bug #61780 (Inconsistent PCRE captures in match results): basics
--FILE--
<?php
preg_match('/(a)?([a-z]*)(\d*)/', '123', $matches);
var_dump($matches);
?>
--EXPECT--
array(4) {
[0]=>
string(3) "123"
[1]=>
NULL
[2]=>
string(0) ""
[3]=>
string(3) "123"
}
194 changes: 194 additions & 0 deletions ext/pcre/tests/bug61780_1.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
--TEST--
Bug #61780 (Inconsistent PCRE captures in match results): numeric subpatterns
--FILE--
<?php
preg_match('/(4)?(2)?\d/', '23456', $matches);
var_export($matches);
echo "\n\n";
preg_match('/(4)?(2)?\d/', '23456', $matches, PREG_OFFSET_CAPTURE);
var_export($matches);
echo "\n\n";
preg_match_all('/(4)?(2)?\d/', '123456', $matches);
var_export($matches);
echo "\n\n";
preg_match_all('/(4)?(2)?\d/', '123456', $matches, PREG_OFFSET_CAPTURE);
var_export($matches);
echo "\n\n";
preg_match_all('/(4)?(2)?\d/', '123456', $matches, PREG_SET_ORDER);
var_export($matches);
echo "\n\n";
preg_match_all('/(4)?(2)?\d/', '123456', $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
var_export($matches);
?>
--EXPECT--
array (
0 => '23',
1 => NULL,
2 => '2',
)

array (
0 =>
array (
0 => '23',
1 => 0,
),
1 =>
array (
0 => NULL,
1 => -1,
),
2 =>
array (
0 => '2',
1 => 0,
),
)

array (
0 =>
array (
0 => '1',
1 => '23',
2 => '45',
3 => '6',
),
1 =>
array (
0 => NULL,
1 => NULL,
2 => '4',
3 => NULL,
),
2 =>
array (
0 => NULL,
1 => '2',
2 => NULL,
3 => NULL,
),
)

array (
0 =>
array (
0 =>
array (
0 => '1',
1 => 0,
),
1 =>
array (
0 => '23',
1 => 1,
),
2 =>
array (
0 => '45',
1 => 3,
),
3 =>
array (
0 => '6',
1 => 5,
),
),
1 =>
array (
0 => NULL,
1 =>
array (
0 => NULL,
1 => -1,
),
2 =>
array (
0 => '4',
1 => 3,
),
3 => NULL,
),
2 =>
array (
0 => NULL,
1 =>
array (
0 => '2',
1 => 1,
),
2 => NULL,
3 => NULL,
),
)

array (
0 =>
array (
0 => '1',
),
1 =>
array (
0 => '23',
1 => NULL,
2 => '2',
),
2 =>
array (
0 => '45',
1 => '4',
),
3 =>
array (
0 => '6',
),
)

array (
0 =>
array (
0 =>
array (
0 => '1',
1 => 0,
),
),
1 =>
array (
0 =>
array (
0 => '23',
1 => 1,
),
1 =>
array (
0 => NULL,
1 => -1,
),
2 =>
array (
0 => '2',
1 => 1,
),
),
2 =>
array (
0 =>
array (
0 => '45',
1 => 3,
),
1 =>
array (
0 => '4',
1 => 3,
),
),
3 =>
array (
0 =>
array (
0 => '6',
1 => 5,
),
),
)
Loading