[php-src] Issue #25118: `Dom\HTMLDocument::createFromString()` crashes with SIGSEGV instead of a clean OOM fatal error when memory_limit is exhausted
during lexbor→libxml2 conversion
| From: | franciscosantamaria | Date: | Sun, 01 Nov 2026 09:56:48 +0000 |
| Subject: | [php-src] Issue #25118: `Dom\HTMLDocument::createFromString()` crashes with SIGSEGV instead of a clean OOM fatal error when memory_limit is exhausted during lexbor→libxml2 conversion |
||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-253966@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/25118
Author: franciscosantamaria
### Description
When the
memory_limit is exhausted while
Dom\HTMLDocument::createFromString() is converting the parsed lexbor document into the
libxml2 tree, PHP 8.5 crashes with SIGSEGV (no fatal error is printed) instead of raising the usual
"Allowed memory size exhausted" fatal error. Depending on where the failing allocation
lands, a long-running process (e.g. a PHPUnit suite parsing many HTML responses close
to the limit) can also die with zend_mm_heap corrupted.
The following script reproduces it deterministically:
```php
<?php
$html = '<!DOCTYPE html><html><body>'
.str_repeat('<div class="c"><p><a
href="/x">link</a> text</p><span>s</span></div>',
5000)
.'</body></html>';
$fill = [];
for ($i = 0; ; ++$i) {
// eat memory in small steps so the limit is hit in the middle of a parse
$fill[] = str_repeat(chr(65 + ($i % 26)), 4096);
$doc = \Dom\HTMLDocument::createFromString($html, \Dom\HTML_NO_DEFAULT_NS);
unset($doc);
}
```
```
$ docker run --rm -v $PWD/repro.php:/r.php php:8.5-cli php -d memory_limit=32M /r.php
Segmentation fault (core dumped) # exit 139, no fatal error printed
```
The same script on php:8.4-cli ends with the expected clean fatal error
(Allowed memory size of 33554432 bytes exhausted, exit 255).
### Analysis
Backtrace from a --enable-debug build of 8.5.12 (see attached
gdb-backtrace.txt):
[gdb-backtrace.txt](https://github.com/user-attachments/files/34235228/gdb-backtrace.txt)
```
Program received signal SIGSEGV, Segmentation fault.
53 item->node = node;
#0 lexbor_libxml2_bridge_work_list_item_push (...) at ext/dom/html5_parser.c:53
#1 lexbor_libxml2_bridge_convert (...) at ext/dom/html5_parser.c:196
#2 lexbor_libxml2_bridge_convert_document (...) at ext/dom/html5_parser.c:366
#3 zim_Dom_HTMLDocument_createFromString (...) at ext/dom/html_document.c:942
```
Since PHP 8.5, the lexbor allocators registered in ext/lexbor/php_lexbor.c are
*fallible*: php_lexbor_malloc() is femalloc(size, /* fallible */ true),
which returns NULL when the request would exceed memory_limit (in 8.4 they
were plain infallible emalloc() in ext/dom/php_dom.c`, so OOM bailed out immediately
with a clean fatal error).
The lexbor library itself tolerates NULL returns, but the bridge in
ext/dom/html5_parser.c does not:
lexbor_libxml2_bridge_work_list_item_push() dereferences the result of
lexbor_array_obj_push_wo_cls() without checking for NULL:
```c
work_list_item *item = (work_list_item *) lexbor_array_obj_push_wo_cls(array);
item->node = node; /* <-- SIGSEGV when the push failed to grow the array */
```
The code is unchanged in current master. Other unchecked allocation results in the
bridge/conversion path may explain the zend_mm_heap corrupted` variant observed in larger
processes.
### PHP Version
```plain
PHP 8.5.12 (docker image php:8.5-cli, Debian; also reproduced with a local
--enable-debug build of the same source). Not reproducible on PHP 8.4.14.
```
### Operating System
Debian trixie (official docker php images), x86_64.