Skip to content

Commit 8eb0889

Browse files
authored
Fix GH-14637: memory leak after failed heap allocation due to mem limit. (#14641)
1 parent c6dc3bd commit 8eb0889

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

ext/xml/compat.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,7 @@ _start_element_handler(void *user, const xmlChar *name, const xmlChar **attribut
7878
return;
7979
}
8080

81-
qualified_name = xmlStrdup(name);
82-
83-
parser->h_start_element(parser->user, (const XML_Char *) qualified_name, (const XML_Char **) attributes);
84-
85-
xmlFree(qualified_name);
81+
parser->h_start_element(parser->user, name, (const XML_Char **) attributes);
8682
}
8783

8884
static void

ext/xml/tests/gh14637.phpt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
GH-14637 memory leak under memory limit
3+
--EXTENSIONS--
4+
xml
5+
--INI--
6+
memory_limit=20M
7+
--CREDITS--
8+
YuanchengJiang
9+
--FILE--
10+
<?php
11+
function 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+
echo "ret = $ret (", xml_error_string(xml_get_error_code($parser)), ")\n";
26+
$parser = createParser(true);
27+
$ret = xml_parse($parser, $long_xml_head, false);
28+
$parser = createParser(true);
29+
$ret = xml_parse_into_struct($parser, $long_xml_head . $long_xml_tail, $values, $index);
30+
?>
31+
--EXPECTF--
32+
ret = 0 (XML_ERR_NAME_REQUIRED)
33+
34+
Fatal error: Allowed memory size of %d bytes exhausted %s in %s on line %d

0 commit comments

Comments
 (0)