Skip to content

Commit

Permalink
Fix #72146: Integer overflow on substr_replace
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cmb69 committed Jul 15, 2021
1 parent ebd3a21 commit 33f8dfb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ PHP NEWS
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
?? ??? ????, PHP 7.4.23

- Standard:
. Fixed bug #72146 (Integer overflow on substr_replace). (cmb)

29 Jul 2021, PHP 7.4.22

Expand Down
4 changes: 3 additions & 1 deletion ext/standard/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -2664,7 +2664,9 @@ PHP_FUNCTION(substr_replace)
}
}

if ((f + l) > (zend_long)ZSTR_LEN(orig_str)) {
ZEND_ASSERT(0 <= f && f <= ZEND_LONG_MAX);
ZEND_ASSERT(0 <= l && l <= ZEND_LONG_MAX);
if (((size_t) f + l) > ZSTR_LEN(orig_str)) {
l = ZSTR_LEN(orig_str) - f;
}

Expand Down
11 changes: 11 additions & 0 deletions ext/standard/tests/strings/bug72146.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--TEST--
Bug #72146 (Integer overflow on substr_replace)
--FILE--
<?php
var_dump(substr_replace(["ABCDE"], "123", 3, PHP_INT_MAX));
?>
--EXPECT--
array(1) {
[0]=>
string(6) "ABC123"
}

0 comments on commit 33f8dfb

Please sign in to comment.