Skip to content

Commit 1820f2f

Browse files
committed
Reorder conditions
1 parent 29f942b commit 1820f2f

File tree

1 file changed

+66
-44
lines changed

1 file changed

+66
-44
lines changed

ext/pcre/php_pcre.c

Lines changed: 66 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,14 +1105,14 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, char *subject, size_t sub
11051105
/* the string was already proved to be valid UTF-8 */
11061106
options |= PCRE2_NO_UTF_CHECK;
11071107

1108-
/* Check for too many substrings condition. */
1109-
if (count == 0) {
1110-
php_error_docref(NULL, E_NOTICE, "Matched, but too many substrings");
1111-
count = num_subpats;
1112-
}
1113-
11141108
/* If something has matched */
1115-
if (count > 0) {
1109+
if (count >= 0) {
1110+
/* Check for too many substrings condition. */
1111+
if (UNEXPECTED(count == 0)) {
1112+
php_error_docref(NULL, E_NOTICE, "Matched, but too many substrings");
1113+
count = num_subpats;
1114+
}
1115+
11161116
matched++;
11171117

11181118
offsets = pcre2_get_ovector_pointer(match_data);
@@ -1582,19 +1582,28 @@ PHPAPI zend_string *php_pcre_replace_impl(pcre_cache_entry *pce, zend_string *su
15821582
/* the string was already proved to be valid UTF-8 */
15831583
options |= PCRE2_NO_UTF_CHECK;
15841584

1585-
/* Check for too many substrings condition. */
1586-
if (UNEXPECTED(count == 0)) {
1587-
php_error_docref(NULL,E_NOTICE, "Matched, but too many substrings");
1588-
count = num_subpats;
1589-
}
1590-
15911585
piece = subject + start_offset;
15921586

1593-
offsets = pcre2_get_ovector_pointer(match_data);
1594-
1595-
if (count > 0 && (offsets[1] >= offsets[0]) && limit > 0) {
1587+
if (count >= 0 && limit > 0) {
15961588
zend_bool simple_string = 1;
15971589

1590+
/* Check for too many substrings condition. */
1591+
if (UNEXPECTED(count == 0)) {
1592+
php_error_docref(NULL,E_NOTICE, "Matched, but too many substrings");
1593+
count = num_subpats;
1594+
}
1595+
1596+
offsets = pcre2_get_ovector_pointer(match_data);
1597+
1598+
if (UNEXPECTED(offsets[1] < offsets[0])) {
1599+
PCRE_G(error_code) = PHP_PCRE_INTERNAL_ERROR;
1600+
if (result) {
1601+
zend_string_release_ex(result, 0);
1602+
result = NULL;
1603+
}
1604+
break;
1605+
}
1606+
15981607
if (replace_count) {
15991608
++*replace_count;
16001609
}
@@ -1823,19 +1832,28 @@ static zend_string *php_pcre_replace_func_impl(pcre_cache_entry *pce, zend_strin
18231832
/* the string was already proved to be valid UTF-8 */
18241833
options |= PCRE2_NO_UTF_CHECK;
18251834

1826-
/* Check for too many substrings condition. */
1827-
if (count == 0) {
1828-
php_error_docref(NULL,E_NOTICE, "Matched, but too many substrings");
1829-
count = num_subpats;
1830-
}
1831-
18321835
piece = subject + start_offset;
18331836

1834-
offsets = pcre2_get_ovector_pointer(match_data);
18351837
mark = pcre2_get_mark(match_data);
18361838

1837-
/* if (EXPECTED(count > 0 && (limit == -1 || limit > 0))) */
1838-
if (count > 0 && (offsets[1] >= offsets[0]) && limit) {
1839+
if (count >= 0 && limit) {
1840+
/* Check for too many substrings condition. */
1841+
if (UNEXPECTED(count == 0)) {
1842+
php_error_docref(NULL,E_NOTICE, "Matched, but too many substrings");
1843+
count = num_subpats;
1844+
}
1845+
1846+
offsets = pcre2_get_ovector_pointer(match_data);
1847+
1848+
if (UNEXPECTED(offsets[1] < offsets[0])) {
1849+
PCRE_G(error_code) = PHP_PCRE_INTERNAL_ERROR;
1850+
if (result) {
1851+
zend_string_release_ex(result, 0);
1852+
result = NULL;
1853+
}
1854+
break;
1855+
}
1856+
18391857
if (replace_count) {
18401858
++*replace_count;
18411859
}
@@ -2474,16 +2492,21 @@ PHPAPI void php_pcre_split_impl(pcre_cache_entry *pce, zend_string *subject_str,
24742492
/* the string was already proved to be valid UTF-8 */
24752493
options |= PCRE2_NO_UTF_CHECK;
24762494

2477-
/* Check for too many substrings condition. */
2478-
if (count == 0) {
2479-
php_error_docref(NULL,E_NOTICE, "Matched, but too many substrings");
2480-
count = num_subpats;
2481-
}
2495+
/* If something matched */
2496+
if (count >= 0) {
2497+
/* Check for too many substrings condition. */
2498+
if (UNEXPECTED(count == 0)) {
2499+
php_error_docref(NULL,E_NOTICE, "Matched, but too many substrings");
2500+
count = num_subpats;
2501+
}
24822502

2483-
offsets = pcre2_get_ovector_pointer(match_data);
2503+
offsets = pcre2_get_ovector_pointer(match_data);
2504+
2505+
if (UNEXPECTED(offsets[1] < offsets[0])) {
2506+
PCRE_G(error_code) = PHP_PCRE_INTERNAL_ERROR;
2507+
break;
2508+
}
24842509

2485-
/* If something matched */
2486-
if (count > 0 && (offsets[1] >= offsets[0])) {
24872510
if (!no_empty || &ZSTR_VAL(subject_str)[offsets[0]] != last_match) {
24882511

24892512
if (offset_capture) {
@@ -2783,18 +2806,13 @@ PHPAPI void php_pcre_grep_impl(pcre_cache_entry *pce, zval *input, zval *return
27832806
count = pcre2_match(pce->re, (PCRE2_SPTR)ZSTR_VAL(subject_str), ZSTR_LEN(subject_str), 0,
27842807
options, match_data, mctx);
27852808

2786-
/* Check for too many substrings condition. */
2787-
if (count == 0) {
2788-
php_error_docref(NULL, E_NOTICE, "Matched, but too many substrings");
2789-
count = num_subpats;
2790-
} else if (count < 0 && count != PCRE2_ERROR_NOMATCH) {
2791-
pcre_handle_exec_error(count);
2792-
zend_string_release_ex(subject_str, 0);
2793-
break;
2794-
}
2795-
27962809
/* If the entry fits our requirements */
2797-
if ((count > 0 && !invert) || (count == PCRE2_ERROR_NOMATCH && invert)) {
2810+
if ((count >= 0 && !invert) || (count == PCRE2_ERROR_NOMATCH && invert)) {
2811+
/* Check for too many substrings condition. */
2812+
if (UNEXPECTED(count == 0)) {
2813+
php_error_docref(NULL, E_NOTICE, "Matched, but too many substrings");
2814+
count = num_subpats;
2815+
}
27982816
Z_TRY_ADDREF_P(entry);
27992817

28002818
/* Add to return array */
@@ -2803,6 +2821,10 @@ PHPAPI void php_pcre_grep_impl(pcre_cache_entry *pce, zval *input, zval *return
28032821
} else {
28042822
zend_hash_index_update(Z_ARRVAL_P(return_value), num_key, entry);
28052823
}
2824+
} else if (count < 0 && count != PCRE2_ERROR_NOMATCH) {
2825+
pcre_handle_exec_error(count);
2826+
zend_string_release_ex(subject_str, 0);
2827+
break;
28062828
}
28072829

28082830
zend_string_release_ex(subject_str, 0);

0 commit comments

Comments
 (0)