xref: /php-src/ext/xml/tests/gh14637.phpt (revision b418d19c)
1--TEST--
2GH-14637 memory leak under memory limit
3--EXTENSIONS--
4xml
5--INI--
6memory_limit=20M
7--CREDITS--
8YuanchengJiang
9--FILE--
10<?php
11function createParser(bool $huge) {
12    $parser = xml_parser_create();
13    xml_parser_set_option($parser, XML_OPTION_PARSE_HUGE, $huge);
14    xml_set_element_handler($parser, function($parser, $data) {
15    }, function($parser, $data) {
16    });
17    return $parser;
18}
19// Construct XML that is too large to parse without XML_OPTION_PARSE_HUGE
20$long_text = str_repeat("A", 1000 * 1000 * 5 /* 5 MB */);
21$long_xml_head = "<?xml version=\"1.0\"?><container><$long_text/><$long_text/><second>foo</second>";
22$long_xml_tail = "</container>";
23$parser = createParser(false);
24$ret = xml_parse($parser, $long_xml_head, true);
25$parser = createParser(true);
26$ret = xml_parse($parser, $long_xml_head, false);
27$parser = createParser(true);
28$ret = xml_parse_into_struct($parser, $long_xml_head . $long_xml_tail, $values, $index);
29?>
30--EXPECTF--
31Fatal error: Allowed memory size of %d bytes exhausted %s in %s on line %d
32