[php-src] PR #25786: Fix string offset assignments far beyond the memory limit killing the process
| From: | marc-mabe | Date: | Thu, 26 Nov 2026 13:12:24 +0000 |
| Subject: | [php-src] PR #25786: Fix string offset assignments far beyond the memory limit killing the process | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-41052@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/25786
Author: marc-mabe
Assigning to a string offset past the end of the string extends the string up to that offset with a
non-fallible allocation. The offset is user controlled, so it can ask for any size. Since #25626,
such an assignment kills the process once the size exceeds twice
memory_limit, without
running shutdown functions:
```php
// memory_limit=100M
register_shutdown_function(fn() => print "Shutdown function executed\n");
$s = 'a';
$s[300 << 20] = 'b';
```
Before
```
Hard memory limit of 209715200 bytes exhausted (tried to allocate 314576896 bytes) in ... on line 4
(killed, exit code 137; shutdown function not run)
```
This PR
```
Fatal error: Allowed memory size of 104857600 bytes exhausted (tried to allocate 314576896 bytes) in
... on line 4
Shutdown function executed
```
This uses zend_string_fextend() with fallible = true in
zend_assign_to_string_offset() of both the VM and the JIT helper. It's the same
approach str_repeat() and str_pad() already use for their user controlled
sizes. If the memory limit doesn't allow the new size, the string is left untouched and the
regular delayed out of memory error is raised.