From 02fae929afa5f1855a4744ecee01ede355826f7e Mon Sep 17 00:00:00 2001 From: Ilia Alshanetsky Date: Fri, 9 Mar 2007 01:58:34 +0000 Subject: [PATCH] Fixed bug #40754 (added substr() & substr_replace() overflow checks). --- NEWS | 1 + ext/standard/string.c | 12 +++++ ext/standard/tests/strings/bug40754.phpt | 63 ++++++++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 ext/standard/tests/strings/bug40754.phpt diff --git a/NEWS b/NEWS index 754c58a095..be0774ce92 100644 --- a/NEWS +++ b/NEWS @@ -16,6 +16,7 @@ PHP NEWS - Added tidyNode::getParent() method (John, Nuno) - Fixed zend_llist_remove_tail (Michael Wallner, Dmitry) - Fixed a thread safety issue in gd gif read code (Nuno, Roman Nemecek) +- Fixed bug #40754 (added substr() & substr_replace() overflow checks). (Ilia) - Fixed bug #40752 (parse_ini_file() segfaults when a scalar setting is redeclared as an array). (Tony) - Fixed bug #40727 (segfault in PDO when failed to bind parameters). (Tony) diff --git a/ext/standard/string.c b/ext/standard/string.c index 91f27ee54c..d05a9bfed2 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -2063,11 +2063,17 @@ PHP_FUNCTION(substr) if (argc > 2) { convert_to_long_ex(len); l = Z_LVAL_PP(len); + if (l > Z_STRLEN_PP(str) || (l < 0 && -l > Z_STRLEN_PP(str))) { + RETURN_FALSE; + } } else { l = Z_STRLEN_PP(str); } f = Z_LVAL_PP(from); + if (f > Z_STRLEN_PP(str) || (f < 0 && -f > Z_STRLEN_PP(str))) { + RETURN_FALSE; + } /* if "from" position is negative, count start position from the end * of the string @@ -2190,6 +2196,12 @@ PHP_FUNCTION(substr_replace) } } + if (f > Z_STRLEN_PP(str) || (f < 0 && -f > Z_STRLEN_PP(str))) { + RETURN_FALSE; + } else if (l > Z_STRLEN_PP(str) || (l < 0 && -l > Z_STRLEN_PP(str))) { + RETURN_FALSE; + } + if ((f + l) > Z_STRLEN_PP(str)) { l = Z_STRLEN_PP(str) - f; } diff --git a/ext/standard/tests/strings/bug40754.phpt b/ext/standard/tests/strings/bug40754.phpt new file mode 100644 index 0000000000..f722ecc784 --- /dev/null +++ b/ext/standard/tests/strings/bug40754.phpt @@ -0,0 +1,63 @@ +--TEST-- +Bug #40754 (Overflow checks inside string functions) +--FILE-- + +--EXPECTF-- +bool(false) +bool(false) +bool(false) +bool(false) + +Warning: substr_count(): Offset value 2147483647 exceeds string length. in %s/bug40754.php on line %d +bool(false) + +Warning: substr_compare(): The start position cannot exceed initial string length in %s/bug40754.php on line %d +bool(false) + +Warning: stripos(): Offset not contained in string. in %s/bug40754.php on line %d +bool(false) + +Warning: substr_count(): Offset value 2147483647 exceeds string length. in %s/bug40754.php on line %d +bool(false) + +Warning: substr_count(): Length value 2147483647 exceeds string length. in %s/bug40754.php on line %d +bool(false) + +Warning: strpos(): Offset not contained in string. in %s/bug40754.php on line %d +bool(false) + +Warning: stripos(): Offset not contained in string. in %s/bug40754.php on line %d +bool(false) + +Notice: strrpos(): Offset is greater than the length of haystack string in %s/bug40754.php on line %d +bool(false) + +Notice: strripos(): Offset is greater than the length of haystack string in %s/bug40754.php on line %d +bool(false) +int(2) +string(8) "abcdeabc" +bool(false) -- 2.50.1