Skip to content

Commit 58e6299

Browse files
committed
Fix #74604: Out of bounds in php_pcre_replace_impl
Trying to allocate a `zend_string` with a length only slighty smaller than `SIZE_MAX` causes an integer overflow; we need to ensure this does not happen, and throw an Error instead.
1 parent 6ab9b38 commit 58e6299

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

ext/pcre/php_pcre.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1720,6 +1720,14 @@ PHPAPI zend_string *php_pcre_replace_impl(pcre_cache_entry *pce, zend_string *su
17201720

17211721
if (new_len >= alloc_len) {
17221722
alloc_len = zend_safe_address_guarded(2, new_len, 0);
1723+
if (UNEXPECTED(alloc_len > ZSTR_MAX_LEN)) {
1724+
zend_throw_error(NULL, "String size overflow");
1725+
if (result != NULL) {
1726+
zend_string_release_ex(result, 0);
1727+
result = NULL;
1728+
}
1729+
break;
1730+
}
17231731
if (result == NULL) {
17241732
result = zend_string_alloc(alloc_len, 0);
17251733
} else {
@@ -1958,6 +1966,14 @@ static zend_string *php_pcre_replace_func_impl(pcre_cache_entry *pce, zend_strin
19581966
new_len = zend_safe_address_guarded(1, ZSTR_LEN(eval_result), new_len);
19591967
if (new_len >= alloc_len) {
19601968
alloc_len = zend_safe_address_guarded(2, new_len, 0);
1969+
if (UNEXPECTED(alloc_len > ZSTR_MAX_LEN)) {
1970+
zend_throw_error(NULL, "String size overflow");
1971+
if (result != NULL) {
1972+
zend_string_release_ex(result, 0);
1973+
result = NULL;
1974+
}
1975+
break;
1976+
}
19611977
if (result == NULL) {
19621978
result = zend_string_alloc(alloc_len, 0);
19631979
} else {

0 commit comments

Comments
 (0)