Skip to content

Fix Bug #80972: Memory exhaustion on invalid string offset #6890

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ PHP NEWS
(cmb)
. Fixed bug #67792 (HTTP Authorization schemes are treated as case-sensitive).
(cmb)
. Fixed bug Bug #80972 (Memory exhaustion on invalid string offset). (girgias)

- pgsql:
. Fixed php_pgsql_fd_cast() wrt. php_stream_can_cast(). (cmb)
Expand Down
3 changes: 2 additions & 1 deletion Zend/tests/bug31098.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ try {
}
echo $simpleString["0"] === "B"?"ok\n":"bug\n";
try {
/* This must not affect the string value */
$simpleString["wrong"] = "f";
} catch (\TypeError $e) {
echo $e->getMessage() . \PHP_EOL;
}
echo $simpleString["0"] === "f"?"ok\n":"bug\n";
echo $simpleString["0"] === "B"?"ok\n":"bug\n";
?>
--EXPECTF--
bool(false)
Expand Down
2 changes: 1 addition & 1 deletion Zend/tests/bug53432.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Warning: Illegal string offset -1 in %s on line %d
NULL
string(0) ""
Cannot access offset of type string on string
string(1) "a"
string(0) ""
Error: [] operator not supported for strings
string(0) ""
Error: Cannot use assign-op operators with string offsets
Expand Down
41 changes: 41 additions & 0 deletions Zend/tests/bug80972.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
--TEST--
Bug #80972: Memory exhaustion on invalid string offset
--FILE--
<?php

function exceptions_error_handler($severity, $message, $filename, $lineno) {
if (error_reporting() & $severity) {
throw new ErrorException($message, 0, $severity, $filename, $lineno);
}
}
set_error_handler('exceptions_error_handler');

$float = 10e120;
$string_float = (string) $float;

$string = 'Here is some text for good measure';

try {
echo 'Float casted to string compile', \PHP_EOL;
$string[(string) 10e120] = 'E';
var_dump($string);
} catch (\TypeError $e) {
echo $e->getMessage(), \PHP_EOL;
}

/* This same bug also permits to modify the first byte of a string even if
* the offset is invalid */
try {
/* This must not affect the string value */
$string["wrong"] = "f";
} catch (\Throwable $e) {
echo $e->getMessage() . \PHP_EOL;
}
var_dump($string);

?>
--EXPECT--
Float casted to string compile
Cannot access offset of type string on string
Cannot access offset of type string on string
string(34) "Here is some text for good measure"
6 changes: 1 addition & 5 deletions Zend/tests/indexing_001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ foreach ($testvalues as $testvalue) {
}

?>
--EXPECTF--
--EXPECT--
*** Indexing - Testing value assignment with key ***
array(1) {
["foo"]=>
Expand All @@ -74,12 +74,8 @@ array(1) {
int(1)
}
}

Warning: Array to string conversion in %s on line %d
Cannot access offset of type string on string
string(0) ""

Warning: Array to string conversion in %s on line %d
Cannot access offset of type string on string
string(1) " "
Cannot use a scalar value as an array
Expand Down
8 changes: 8 additions & 0 deletions Zend/zend_execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,14 @@ static zend_never_inline void zend_assign_to_string_offset(zval *str, zval *dim,
zend_long offset;

offset = zend_check_string_offset(dim, BP_VAR_W EXECUTE_DATA_CC);
/* Illegal offset assignment */
if (UNEXPECTED(EG(exception) != NULL)) {
if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
ZVAL_UNDEF(EX_VAR(opline->result.var));
}
return;
}

if (offset < -(zend_long)Z_STRLEN_P(str)) {
/* Error on negative offset */
zend_error(E_WARNING, "Illegal string offset " ZEND_LONG_FMT, offset);
Expand Down