From fd623782085c0e47f9fa1297e356755a755f603a Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Wed, 25 Nov 2026 06:52:31 +0100 Subject: [PATCH] Resolve negative preg_match() offsets outside php_pcre_match_impl() php_pcre_match_impl() took the start offset as zend_off_t, a type meant for file offsets, and resolved negative offsets itself by negating them, which overflows for ZEND_LONG_MIN. To avoid that, preg_match() and preg_match_all() rejected PHP_INT_MIN with a ValueError (GH-16189). Resolve negative offsets in preg_match() and preg_match_all() instead, where PHP_INT_MIN now starts at the beginning of the subject like any other negative offset beyond its length, and make php_pcre_match_impl() take a size_t offset. Callers with a constant offset of 0, such as the frameless preg_match() call and RegexIterator, no longer pay for the negative offset handling. --- NEWS | 5 ++++ UPGRADING | 5 ++++ UPGRADING.INTERNALS | 6 ++++ ext/pcre/php_pcre.c | 60 +++++++++++++++++-------------------- ext/pcre/php_pcre.h | 2 +- ext/pcre/tests/gh16189.phpt | 18 ++++------- 6 files changed, 50 insertions(+), 46 deletions(-) diff --git a/NEWS b/NEWS index aabe78ad2a7c..4eb2ac4c8c3f 100644 --- a/NEWS +++ b/NEWS @@ -108,6 +108,11 @@ PHP NEWS (ndossche) . Fixed JIT ignoring exceptions from typed property set hooks. (David Carlier) +- PCRE: + . preg_match() and preg_match_all() no longer throw a ValueError for an + offset of PHP_INT_MIN, which now starts at the beginning of the subject + like any other negative offset beyond its length. (Marc Bennewitz) + - PDO: . Fixed PDOStatement::getColumnMeta() reading out of bounds for an invalid column index. (Ilia Alshanetsky) diff --git a/UPGRADING b/UPGRADING index 607f9427ed6b..3fe80cedba8d 100644 --- a/UPGRADING +++ b/UPGRADING @@ -37,6 +37,11 @@ PHP 8.7 UPGRADE NOTES 127 bytes. Previously, the length could be truncated and only a prefix of the string used for substitution. +- PCRE: + . preg_match() and preg_match_all() no longer throw a ValueError when the + offset is PHP_INT_MIN. Like any other negative offset beyond the length of + the subject, the search now starts at the beginning of the subject. + - PDO: . PDOStatement::bindColumn() now throws a ValueError when the column name is not present in the result set. It previously reported the condition diff --git a/UPGRADING.INTERNALS b/UPGRADING.INTERNALS index a2f9bc7f01c9..8c3fbc228ae5 100644 --- a/UPGRADING.INTERNALS +++ b/UPGRADING.INTERNALS @@ -138,6 +138,12 @@ PHP 8.7 INTERNALS UPGRADE NOTES . The MYSQLND_STATS_LOCK() and MYSQLND_STATS_UNLOCK() macros were removed. MYSQLND_STATS_UPDATE_VALUE() is now an expression instead of a block. +- PCRE: + . php_pcre_match_impl() now takes the start offset as size_t instead of + zend_off_t. Negative offsets need to be handled by the caller, which saves + that work for callers with a constant offset of 0, such as the frameless + preg_match() call and RegexIterator. + - Session: . The module_number member of php_ps_globals was removed. diff --git a/ext/pcre/php_pcre.c b/ext/pcre/php_pcre.c index 3eeb50e2ecff..9dc727b6d1df 100644 --- a/ext/pcre/php_pcre.c +++ b/ext/pcre/php_pcre.c @@ -1157,6 +1157,7 @@ static void php_do_pcre_match(INTERNAL_FUNCTION_PARAMETERS, bool global) /* {{{ zval *subpats = NULL; /* Array for subpatterns */ zend_long flags = 0; /* Match control flags */ zend_long start_offset = 0; /* Where the new search starts */ + size_t offset; ZEND_PARSE_PARAMETERS_START(2, 5) Z_PARAM_STR(regex) @@ -1172,14 +1173,20 @@ static void php_do_pcre_match(INTERNAL_FUNCTION_PARAMETERS, bool global) /* {{{ RETURN_FALSE; } - if (start_offset == ZEND_LONG_MIN) { - zend_argument_value_error(5, "must be greater than " ZEND_LONG_FMT, ZEND_LONG_MIN); - RETURN_THROWS(); + if (start_offset < 0) { + /* Negative offset counts from the end of the string. */ + if (EXPECTED(start_offset != ZEND_LONG_MIN && (size_t)-start_offset <= ZSTR_LEN(subject))) { + offset = ZSTR_LEN(subject) - (size_t)-start_offset; + } else { + offset = 0; + } + } else { + offset = (size_t)start_offset; } pce->refcount++; php_pcre_match_impl(pce, subject, return_value, subpats, - global, flags, start_offset); + global, flags, offset); pce->refcount--; } /* }}} */ @@ -1202,7 +1209,7 @@ static zend_always_inline bool is_known_valid_utf8( /* {{{ php_pcre_match_impl() */ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, zend_string *subject_str, zval *return_value, - zval *subpats, bool global, zend_long flags, zend_off_t start_offset) + zval *subpats, bool global, zend_long flags, size_t start_offset) { zval result_set; /* Holds a set of subpatterns after a global match */ @@ -1219,7 +1226,7 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, zend_string *subject_str, PCRE2_SPTR mark = NULL; /* Target for MARK name */ HashTable *marks = NULL; /* Array of marks for PREG_PATTERN_ORDER */ pcre2_match_data *match_data; - PCRE2_SIZE start_offset2, orig_start_offset; + PCRE2_SIZE search_offset; bool old_mdata_used; char *subject = ZSTR_VAL(subject_str); @@ -1256,21 +1263,11 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, zend_string *subject_str, unmatched_as_null = 0; } - /* Negative offset counts from the end of the string. */ - if (start_offset < 0) { - if ((PCRE2_SIZE)-start_offset <= subject_len) { - start_offset2 = subject_len + start_offset; - } else { - start_offset2 = 0; - } - } else { - start_offset2 = (PCRE2_SIZE)start_offset; - } - - if (start_offset2 > subject_len) { + if (start_offset > subject_len) { pcre_handle_exec_error(PCRE2_ERROR_BADOFFSET); RETURN_FALSE; } + search_offset = start_offset; /* Calculate the size of the offsets array, and allocate memory for it. */ num_subpats = pce->capture_count + 1; @@ -1313,19 +1310,18 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, zend_string *subject_str, /* Array of subpattern offsets */ PCRE2_SIZE *const offsets = pcre2_get_ovector_pointer(match_data); - orig_start_offset = start_offset2; options = - (pce->compile_options & PCRE2_UTF) && !is_known_valid_utf8(subject_str, orig_start_offset) + (pce->compile_options & PCRE2_UTF) && !is_known_valid_utf8(subject_str, start_offset) ? 0 : PCRE2_NO_UTF_CHECK; /* Execute the regular expression. */ #ifdef HAVE_PCRE_JIT_SUPPORT if ((pce->preg_options & PREG_JIT) && options) { - count = pcre2_jit_match(pce->re, (PCRE2_SPTR)subject, subject_len, start_offset2, + count = pcre2_jit_match(pce->re, (PCRE2_SPTR)subject, subject_len, search_offset, PCRE2_NO_UTF_CHECK, match_data, mctx); } else #endif - count = pcre2_match(pce->re, (PCRE2_SPTR)subject, subject_len, start_offset2, + count = pcre2_match(pce->re, (PCRE2_SPTR)subject, subject_len, search_offset, options, match_data, mctx); while (1) { @@ -1449,14 +1445,14 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, zend_string *subject_str, } /* Advance to the next piece. */ - start_offset2 = offsets[1]; + search_offset = offsets[1]; /* If we have matched an empty string, mimic what Perl's /g options does. This turns out to be rather cunning. First we set PCRE2_NOTEMPTY_ATSTART and try the match again at the same point. If this fails (picked up above) we advance to the next character. */ - if (start_offset2 == offsets[0]) { - count = pcre2_match(pce->re, (PCRE2_SPTR)subject, subject_len, start_offset2, + if (search_offset == offsets[0]) { + count = pcre2_match(pce->re, (PCRE2_SPTR)subject, subject_len, search_offset, PCRE2_NO_UTF_CHECK | PCRE2_NOTEMPTY_ATSTART | PCRE2_ANCHORED, match_data, mctx); if (count >= 0) { if (global) { @@ -1469,10 +1465,10 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, zend_string *subject_str, this is not necessarily the end. We need to advance the start offset, and continue. Fudge the offset values to achieve this, unless we're already at the end of the string. */ - if (start_offset2 < subject_len) { - size_t unit_len = calculate_unit_length(pce, subject + start_offset2); + if (search_offset < subject_len) { + size_t unit_len = calculate_unit_length(pce, subject + search_offset); - start_offset2 += unit_len; + search_offset += unit_len; } else { break; } @@ -1495,15 +1491,15 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, zend_string *subject_str, /* Execute the regular expression. */ #ifdef HAVE_PCRE_JIT_SUPPORT if ((pce->preg_options & PREG_JIT)) { - if (start_offset2 > subject_len) { + if (search_offset > subject_len) { pcre_handle_exec_error(PCRE2_ERROR_BADOFFSET); break; } - count = pcre2_jit_match(pce->re, (PCRE2_SPTR)subject, subject_len, start_offset2, + count = pcre2_jit_match(pce->re, (PCRE2_SPTR)subject, subject_len, search_offset, PCRE2_NO_UTF_CHECK, match_data, mctx); } else #endif - count = pcre2_match(pce->re, (PCRE2_SPTR)subject, subject_len, start_offset2, + count = pcre2_match(pce->re, (PCRE2_SPTR)subject, subject_len, search_offset, PCRE2_NO_UTF_CHECK, match_data, mctx); } if (match_data != mdata) { @@ -1542,7 +1538,7 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, zend_string *subject_str, if (PCRE_G(error_code) == PHP_PCRE_NO_ERROR) { /* If there was no error and we're in /u mode, remember that the string is valid UTF-8. */ if ((pce->compile_options & PCRE2_UTF) - && !ZSTR_IS_INTERNED(subject_str) && orig_start_offset == 0) { + && !ZSTR_IS_INTERNED(subject_str) && start_offset == 0) { GC_ADD_FLAGS(subject_str, IS_STR_VALID_UTF8); } diff --git a/ext/pcre/php_pcre.h b/ext/pcre/php_pcre.h index 7312b36307fd..52499b6e506c 100644 --- a/ext/pcre/php_pcre.h +++ b/ext/pcre/php_pcre.h @@ -48,7 +48,7 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache(zend_string *regex); PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache_ex(zend_string *regex, bool locale_aware); PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, zend_string *subject_str, zval *return_value, - zval *subpats, bool global, zend_long flags, zend_off_t start_offset); + zval *subpats, bool global, zend_long flags, size_t start_offset); PHPAPI zend_string *php_pcre_replace_impl(const pcre_cache_entry *pce, zend_string *subject_str, const char *subject, size_t subject_len, zend_string *replace_str, size_t limit, size_t *replace_count); diff --git a/ext/pcre/tests/gh16189.phpt b/ext/pcre/tests/gh16189.phpt index 6e7dbaaf3941..6a4ea6cf5993 100644 --- a/ext/pcre/tests/gh16189.phpt +++ b/ext/pcre/tests/gh16189.phpt @@ -3,17 +3,9 @@ GH-16189 (preg_match/preg_match_all underflow on start_offset argument) --FILE-- /', '
', $matches, 0, PHP_INT_MIN); -} catch (\ValueError $e) { - echo $e::class, ': ', $e->getMessage(), PHP_EOL; -} -try { - preg_match_all( '/<(\w+)[\s\w\-]+ id="S44_i89ew">/', '
', $matches, 0, PHP_INT_MIN); -} catch (\ValueError $e) { - echo $e::class, ': ', $e->getMessage(), PHP_EOL; -} +var_dump(preg_match( '/<(\w+)[\s\w\-]+ id="S44_i89ew">/', '
', $matches, 0, PHP_INT_MIN)); +var_dump(preg_match_all( '/<(\w+)[\s\w\-]+ id="S44_i89ew">/', '
', $matches, 0, PHP_INT_MIN)); ?> ---EXPECTF-- -ValueError: preg_match(): Argument #5 ($offset) must be greater than %s -ValueError: preg_match_all(): Argument #5 ($offset) must be greater than %s +--EXPECT-- +int(1) +int(1)