Skip to content

Commit 33f8dfb

Browse files
committed
Fix #72146: Integer overflow on substr_replace
Adding two `zend_long`s may overflow, and casting `size_t` to `zend_long` may truncate; we can avoid this here by enforcing unsigned arithmetic. Closes GH-7240.
1 parent ebd3a21 commit 33f8dfb

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? ????, PHP 7.4.23
44

5+
- Standard:
6+
. Fixed bug #72146 (Integer overflow on substr_replace). (cmb)
57

68
29 Jul 2021, PHP 7.4.22
79

ext/standard/string.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2664,7 +2664,9 @@ PHP_FUNCTION(substr_replace)
26642664
}
26652665
}
26662666

2667-
if ((f + l) > (zend_long)ZSTR_LEN(orig_str)) {
2667+
ZEND_ASSERT(0 <= f && f <= ZEND_LONG_MAX);
2668+
ZEND_ASSERT(0 <= l && l <= ZEND_LONG_MAX);
2669+
if (((size_t) f + l) > ZSTR_LEN(orig_str)) {
26682670
l = ZSTR_LEN(orig_str) - f;
26692671
}
26702672

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
Bug #72146 (Integer overflow on substr_replace)
3+
--FILE--
4+
<?php
5+
var_dump(substr_replace(["ABCDE"], "123", 3, PHP_INT_MAX));
6+
?>
7+
--EXPECT--
8+
array(1) {
9+
[0]=>
10+
string(6) "ABC123"
11+
}

0 commit comments

Comments
 (0)