Skip to content

Commit efaae93

Browse files
committed
Merge branch 'PHP-8.4'
* PHP-8.4: Fix GH-18597: Heap-buffer-overflow in zend_alloc.c when assigning string with UTF-8 bytes
2 parents 76791e9 + 40e6672 commit efaae93

File tree

5 files changed

+23
-5
lines changed

5 files changed

+23
-5
lines changed

ext/dom/inner_outer_html_mixin.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ static zend_string *dom_element_html_fragment_serialize(dom_object *obj, xmlNode
9595
status |= xmlOutputBufferFlush(out);
9696
status |= xmlOutputBufferClose(out);
9797
}
98-
(void) xmlSaveClose(ctxt);
98+
status |= xmlSaveClose(ctxt);
9999
xmlCharEncCloseFunc(handler);
100100
}
101101
if (UNEXPECTED(status < 0)) {

ext/dom/xml_document.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ static zend_string *php_new_dom_dump_node_to_str_ex(xmlNodePtr node, int options
282282
} else {
283283
xmlCharEncCloseFunc(handler);
284284
}
285-
(void) xmlSaveClose(ctxt);
285+
status |= xmlSaveClose(ctxt);
286286
}
287287

288288
if (UNEXPECTED(status < 0)) {
@@ -319,7 +319,7 @@ zend_long php_new_dom_dump_node_to_file(const char *filename, xmlDocPtr doc, xml
319319
if (EXPECTED(ctxt != NULL)) {
320320
status = dom_xml_serialize(ctxt, out, node, format, false, get_private_data_from_node(node));
321321
status |= xmlOutputBufferFlush(out);
322-
(void) xmlSaveClose(ctxt);
322+
status |= xmlSaveClose(ctxt);
323323
}
324324

325325
size_t offset = php_stream_tell(stream);

ext/libxml/libxml.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1505,7 +1505,7 @@ static zend_string *php_libxml_default_dump_doc_to_str(xmlDocPtr doc, int option
15051505
}
15061506

15071507
long status = xmlSaveDoc(ctxt, doc);
1508-
(void) xmlSaveClose(ctxt);
1508+
status |= xmlSaveClose(ctxt);
15091509
if (status < 0) {
15101510
smart_str_free_ex(&str, false);
15111511
return NULL;

ext/simplexml/simplexml.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1403,7 +1403,8 @@ PHP_METHOD(SimpleXMLElement, asXML)
14031403
if (!result) {
14041404
RETURN_FALSE;
14051405
} else {
1406-
RETURN_NEW_STR(result);
1406+
/* Defense-in-depth: don't use the NEW variant in case somehow an empty string gets returned */
1407+
RETURN_STR(result);
14071408
}
14081409
}
14091410
/* }}} */

ext/simplexml/tests/gh18597.phpt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
GH-18597 (Heap-buffer-overflow in zend_alloc.c when assigning string with UTF-8 bytes)
3+
--EXTENSIONS--
4+
simplexml
5+
--FILE--
6+
<?php
7+
$sx1 = new SimpleXMLElement("<root />");
8+
$sx1->node[0] = 'node1';
9+
$node = $sx1->node[0];
10+
11+
$node[0] = '��c';
12+
13+
$sx1->asXML(); // Depends on the available system encodings whether this fails or not, point is, it should not crash
14+
echo "Done\n";
15+
?>
16+
--EXPECT--
17+
Done

0 commit comments

Comments
 (0)