Skip to content

Commit 793f632

Browse files
authored
Fix NULL pointer dereference with NULL content in legacy nodes (#15546)
1 parent d6c06ed commit 793f632

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

ext/dom/html5_serializer.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ static zend_result dom_html5_serialize_doctype(dom_html5_serialize_context *ctx,
4242
static zend_result dom_html5_serialize_comment(dom_html5_serialize_context *ctx, const xmlNode *node)
4343
{
4444
TRY(ctx->write_string_len(ctx->application_data, "<!--", strlen("<!--")));
45-
TRY(ctx->write_string(ctx->application_data, (const char *) node->content));
45+
if (node->content) {
46+
TRY(ctx->write_string(ctx->application_data, (const char*) node->content));
47+
}
4648
return ctx->write_string_len(ctx->application_data, "-->", strlen("-->"));
4749
}
4850

@@ -131,6 +133,10 @@ static zend_result dom_html5_escape_string(dom_html5_serialize_context *ctx, con
131133

132134
static zend_result dom_html5_serialize_text_node(dom_html5_serialize_context *ctx, const xmlNode *node)
133135
{
136+
if (!node->content) {
137+
return SUCCESS;
138+
}
139+
134140
if (node->parent->type == XML_ELEMENT_NODE && php_dom_ns_is_fast(node->parent, php_dom_ns_is_html_magic_token)) {
135141
const xmlNode *parent = node->parent;
136142
size_t name_length = strlen((const char *) parent->name);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
Serialize legacy nodes with NULL content
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
$dom = Dom\HTMLDocument::createEmpty();
8+
$root = $dom->appendChild($dom->createElement('html'));
9+
10+
$root->appendChild($dom->importLegacyNode(new DOMText));
11+
$root->appendChild($dom->importLegacyNode(new DOMComment));
12+
$root->appendChild($dom->importLegacyNode(new DOMProcessingInstruction('target')));
13+
$root->appendChild($dom->importLegacyNode(new DOMCdataSection('')));
14+
15+
echo $dom->saveHTML(), "\n";
16+
echo $dom->documentElement->innerHTML, "\n";
17+
?>
18+
--EXPECT--
19+
<html><!----><?target ></html>
20+
<!----><?target >

0 commit comments

Comments
 (0)