@@ -1105,14 +1105,14 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, char *subject, size_t sub
1105
1105
/* the string was already proved to be valid UTF-8 */
1106
1106
options |= PCRE2_NO_UTF_CHECK ;
1107
1107
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
-
1114
1108
/* 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
+
1116
1116
matched ++ ;
1117
1117
1118
1118
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
1582
1582
/* the string was already proved to be valid UTF-8 */
1583
1583
options |= PCRE2_NO_UTF_CHECK ;
1584
1584
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
-
1591
1585
piece = subject + start_offset ;
1592
1586
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 ) {
1596
1588
zend_bool simple_string = 1 ;
1597
1589
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
+
1598
1607
if (replace_count ) {
1599
1608
++ * replace_count ;
1600
1609
}
@@ -1823,19 +1832,28 @@ static zend_string *php_pcre_replace_func_impl(pcre_cache_entry *pce, zend_strin
1823
1832
/* the string was already proved to be valid UTF-8 */
1824
1833
options |= PCRE2_NO_UTF_CHECK ;
1825
1834
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
-
1832
1835
piece = subject + start_offset ;
1833
1836
1834
- offsets = pcre2_get_ovector_pointer (match_data );
1835
1837
mark = pcre2_get_mark (match_data );
1836
1838
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
+
1839
1857
if (replace_count ) {
1840
1858
++ * replace_count ;
1841
1859
}
@@ -2474,16 +2492,21 @@ PHPAPI void php_pcre_split_impl(pcre_cache_entry *pce, zend_string *subject_str,
2474
2492
/* the string was already proved to be valid UTF-8 */
2475
2493
options |= PCRE2_NO_UTF_CHECK ;
2476
2494
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
+ }
2482
2502
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
+ }
2484
2509
2485
- /* If something matched */
2486
- if (count > 0 && (offsets [1 ] >= offsets [0 ])) {
2487
2510
if (!no_empty || & ZSTR_VAL (subject_str )[offsets [0 ]] != last_match ) {
2488
2511
2489
2512
if (offset_capture ) {
@@ -2783,18 +2806,13 @@ PHPAPI void php_pcre_grep_impl(pcre_cache_entry *pce, zval *input, zval *return
2783
2806
count = pcre2_match (pce -> re , (PCRE2_SPTR )ZSTR_VAL (subject_str ), ZSTR_LEN (subject_str ), 0 ,
2784
2807
options , match_data , mctx );
2785
2808
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
-
2796
2809
/* 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
+ }
2798
2816
Z_TRY_ADDREF_P (entry );
2799
2817
2800
2818
/* Add to return array */
@@ -2803,6 +2821,10 @@ PHPAPI void php_pcre_grep_impl(pcre_cache_entry *pce, zval *input, zval *return
2803
2821
} else {
2804
2822
zend_hash_index_update (Z_ARRVAL_P (return_value ), num_key , entry );
2805
2823
}
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 ;
2806
2828
}
2807
2829
2808
2830
zend_string_release_ex (subject_str , 0 );
0 commit comments