Skip to content
Open
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
5 changes: 5 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions UPGRADING.INTERNALS
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
60 changes: 28 additions & 32 deletions ext/pcre/php_pcre.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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--;
}
/* }}} */
Expand All @@ -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 */
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion ext/pcre/php_pcre.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 5 additions & 13 deletions ext/pcre/tests/gh16189.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,9 @@ GH-16189 (preg_match/preg_match_all underflow on start_offset argument)
--FILE--
<?php

try {
preg_match( '/<(\w+)[\s\w\-]+ id="S44_i89ew">/', '<br><div id="S44_i89ew">', $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">/', '<br><div 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">/', '<br><div id="S44_i89ew">', $matches, 0, PHP_INT_MIN));
var_dump(preg_match_all( '/<(\w+)[\s\w\-]+ id="S44_i89ew">/', '<br><div 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)
Loading