summaryrefslogtreecommitdiff
path: root/ext/pcre/php_pcre.c
diff options
context:
space:
mode:
authorDmitry Stogov <dmitry@zend.com>2018-07-18 17:46:48 +0300
committerDmitry Stogov <dmitry@zend.com>2018-07-18 17:46:48 +0300
commit1820f2f2f34bb9080fa4b1fad0c04e090feb33c1 (patch)
tree24044954841495cc789a5704a00f4ee751729d76 /ext/pcre/php_pcre.c
parent29f942b3d03e9b7ef4cc06e05928012a092ffdb7 (diff)
downloadphp-git-1820f2f2f34bb9080fa4b1fad0c04e090feb33c1.tar.gz
Reorder conditions
Diffstat (limited to 'ext/pcre/php_pcre.c')
-rw-r--r--ext/pcre/php_pcre.c110
1 files changed, 66 insertions, 44 deletions
diff --git a/ext/pcre/php_pcre.c b/ext/pcre/php_pcre.c
index ffa9d79e0a..e139e531ce 100644
--- a/ext/pcre/php_pcre.c
+++ b/ext/pcre/php_pcre.c
@@ -1105,14 +1105,14 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, char *subject, size_t sub
/* the string was already proved to be valid UTF-8 */
options |= PCRE2_NO_UTF_CHECK;
- /* Check for too many substrings condition. */
- if (count == 0) {
- php_error_docref(NULL, E_NOTICE, "Matched, but too many substrings");
- count = num_subpats;
- }
-
/* If something has matched */
- if (count > 0) {
+ if (count >= 0) {
+ /* Check for too many substrings condition. */
+ if (UNEXPECTED(count == 0)) {
+ php_error_docref(NULL, E_NOTICE, "Matched, but too many substrings");
+ count = num_subpats;
+ }
+
matched++;
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
/* the string was already proved to be valid UTF-8 */
options |= PCRE2_NO_UTF_CHECK;
- /* Check for too many substrings condition. */
- if (UNEXPECTED(count == 0)) {
- php_error_docref(NULL,E_NOTICE, "Matched, but too many substrings");
- count = num_subpats;
- }
-
piece = subject + start_offset;
- offsets = pcre2_get_ovector_pointer(match_data);
-
- if (count > 0 && (offsets[1] >= offsets[0]) && limit > 0) {
+ if (count >= 0 && limit > 0) {
zend_bool simple_string = 1;
+ /* Check for too many substrings condition. */
+ if (UNEXPECTED(count == 0)) {
+ php_error_docref(NULL,E_NOTICE, "Matched, but too many substrings");
+ count = num_subpats;
+ }
+
+ offsets = pcre2_get_ovector_pointer(match_data);
+
+ if (UNEXPECTED(offsets[1] < offsets[0])) {
+ PCRE_G(error_code) = PHP_PCRE_INTERNAL_ERROR;
+ if (result) {
+ zend_string_release_ex(result, 0);
+ result = NULL;
+ }
+ break;
+ }
+
if (replace_count) {
++*replace_count;
}
@@ -1823,19 +1832,28 @@ static zend_string *php_pcre_replace_func_impl(pcre_cache_entry *pce, zend_strin
/* the string was already proved to be valid UTF-8 */
options |= PCRE2_NO_UTF_CHECK;
- /* Check for too many substrings condition. */
- if (count == 0) {
- php_error_docref(NULL,E_NOTICE, "Matched, but too many substrings");
- count = num_subpats;
- }
-
piece = subject + start_offset;
- offsets = pcre2_get_ovector_pointer(match_data);
mark = pcre2_get_mark(match_data);
- /* if (EXPECTED(count > 0 && (limit == -1 || limit > 0))) */
- if (count > 0 && (offsets[1] >= offsets[0]) && limit) {
+ if (count >= 0 && limit) {
+ /* Check for too many substrings condition. */
+ if (UNEXPECTED(count == 0)) {
+ php_error_docref(NULL,E_NOTICE, "Matched, but too many substrings");
+ count = num_subpats;
+ }
+
+ offsets = pcre2_get_ovector_pointer(match_data);
+
+ if (UNEXPECTED(offsets[1] < offsets[0])) {
+ PCRE_G(error_code) = PHP_PCRE_INTERNAL_ERROR;
+ if (result) {
+ zend_string_release_ex(result, 0);
+ result = NULL;
+ }
+ break;
+ }
+
if (replace_count) {
++*replace_count;
}
@@ -2474,16 +2492,21 @@ PHPAPI void php_pcre_split_impl(pcre_cache_entry *pce, zend_string *subject_str,
/* the string was already proved to be valid UTF-8 */
options |= PCRE2_NO_UTF_CHECK;
- /* Check for too many substrings condition. */
- if (count == 0) {
- php_error_docref(NULL,E_NOTICE, "Matched, but too many substrings");
- count = num_subpats;
- }
+ /* If something matched */
+ if (count >= 0) {
+ /* Check for too many substrings condition. */
+ if (UNEXPECTED(count == 0)) {
+ php_error_docref(NULL,E_NOTICE, "Matched, but too many substrings");
+ count = num_subpats;
+ }
- offsets = pcre2_get_ovector_pointer(match_data);
+ offsets = pcre2_get_ovector_pointer(match_data);
+
+ if (UNEXPECTED(offsets[1] < offsets[0])) {
+ PCRE_G(error_code) = PHP_PCRE_INTERNAL_ERROR;
+ break;
+ }
- /* If something matched */
- if (count > 0 && (offsets[1] >= offsets[0])) {
if (!no_empty || &ZSTR_VAL(subject_str)[offsets[0]] != last_match) {
if (offset_capture) {
@@ -2783,18 +2806,13 @@ PHPAPI void php_pcre_grep_impl(pcre_cache_entry *pce, zval *input, zval *return
count = pcre2_match(pce->re, (PCRE2_SPTR)ZSTR_VAL(subject_str), ZSTR_LEN(subject_str), 0,
options, match_data, mctx);
- /* Check for too many substrings condition. */
- if (count == 0) {
- php_error_docref(NULL, E_NOTICE, "Matched, but too many substrings");
- count = num_subpats;
- } else if (count < 0 && count != PCRE2_ERROR_NOMATCH) {
- pcre_handle_exec_error(count);
- zend_string_release_ex(subject_str, 0);
- break;
- }
-
/* If the entry fits our requirements */
- if ((count > 0 && !invert) || (count == PCRE2_ERROR_NOMATCH && invert)) {
+ if ((count >= 0 && !invert) || (count == PCRE2_ERROR_NOMATCH && invert)) {
+ /* Check for too many substrings condition. */
+ if (UNEXPECTED(count == 0)) {
+ php_error_docref(NULL, E_NOTICE, "Matched, but too many substrings");
+ count = num_subpats;
+ }
Z_TRY_ADDREF_P(entry);
/* Add to return array */
@@ -2803,6 +2821,10 @@ PHPAPI void php_pcre_grep_impl(pcre_cache_entry *pce, zval *input, zval *return
} else {
zend_hash_index_update(Z_ARRVAL_P(return_value), num_key, entry);
}
+ } else if (count < 0 && count != PCRE2_ERROR_NOMATCH) {
+ pcre_handle_exec_error(count);
+ zend_string_release_ex(subject_str, 0);
+ break;
}
zend_string_release_ex(subject_str, 0);