Skip to content
Open
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
20 changes: 20 additions & 0 deletions Zend/tests/oom_str_offset_assign.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
OOM: string offset assignment beyond the memory limit
--INI--
memory_limit=100M
--FILE--
<?php

function shutdown_function() {
echo "Shutdown function executed\n";
}

register_shutdown_function('shutdown_function');

$s = 'a';
$s[300<<20] = 'b';

?>
--EXPECTF--
Fatal error: Allowed memory size of 104857600 bytes exhausted %S(tried to allocate %d bytes) in %s on line 10
Shutdown function executed
13 changes: 11 additions & 2 deletions Zend/zend_execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -2169,9 +2169,18 @@ static zend_never_inline void zend_assign_to_string_offset(zval *str, zval *dim,
}

if ((size_t)offset >= ZSTR_LEN(s)) {
/* Extend string if needed */
/* Extend string if needed. The offset is user controlled, so allocate
* fallibly: if the memory limit doesn't allow the new size, the string
* is left untouched and an out of memory error is raised. */
zend_long old_len = ZSTR_LEN(s);
ZVAL_NEW_STR(str, zend_string_extend(s, (size_t)offset + 1, 0));
zend_string *extended = zend_string_fextend(s, (size_t)offset + 1, 0, /* fallible */ true);
if (UNEXPECTED(!extended)) {
if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
ZVAL_UNDEF(EX_VAR(opline->result.var));
}
return;
}
ZVAL_NEW_STR(str, extended);
memset(Z_STRVAL_P(str) + old_len, ' ', offset - old_len);
Z_STRVAL_P(str)[offset+1] = 0;
} else {
Expand Down
13 changes: 11 additions & 2 deletions ext/opcache/jit/zend_jit_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -1474,9 +1474,18 @@ static zend_never_inline void zend_assign_to_string_offset(zval *str, zval *dim,
}

if ((size_t)offset >= ZSTR_LEN(s)) {
/* Extend string if needed */
/* Extend string if needed. The offset is user controlled, so allocate
* fallibly: if the memory limit doesn't allow the new size, the string
* is left untouched and an out of memory error is raised. */
zend_long old_len = ZSTR_LEN(s);
ZVAL_NEW_STR(str, zend_string_extend(s, (size_t)offset + 1, 0));
zend_string *extended = zend_string_fextend(s, (size_t)offset + 1, 0, /* fallible */ true);
if (UNEXPECTED(!extended)) {
if (result) {
ZVAL_UNDEF(result);
}
return;
}
ZVAL_NEW_STR(str, extended);
memset(Z_STRVAL_P(str) + old_len, ' ', offset - old_len);
Z_STRVAL_P(str)[offset+1] = 0;
} else {
Expand Down
Loading