From 82d71a82aaa20b97581a82f9c8eae30ade7e5c05 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Fri, 17 Jan 2025 18:31:51 +0100 Subject: [PATCH] Fix GH-17500: Segfault with requesting nodeName on nameless doctype Closes GH-17344. --- NEWS | 4 ++++ ext/dom/node.c | 8 +++++++- ext/dom/tests/gh17500.phpt | 14 ++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 ext/dom/tests/gh17500.phpt diff --git a/NEWS b/NEWS index 5de1b803299..0c67da4eba0 100644 --- a/NEWS +++ b/NEWS @@ -13,6 +13,10 @@ PHP NEWS nielsdos) . Fixed potential OOB when checking for trailing spaces on Windows. (cmb) +- DOM: + . Fixed bug GH-17500 (Segfault with requesting nodeName on nameless doctype). + (nielsdos) + - Enchant: . Fix crashes in enchant when passing null bytes. (nielsdos) diff --git a/ext/dom/node.c b/ext/dom/node.c index 07c6859d730..588665830bf 100644 --- a/ext/dom/node.c +++ b/ext/dom/node.c @@ -98,11 +98,17 @@ int dom_node_node_name_read(dom_object *obj, zval *retval) } case XML_DOCUMENT_TYPE_NODE: case XML_DTD_NODE: + if (nodep->name) { + ZVAL_STRING(retval, (const char *) nodep->name); + } else { + ZVAL_EMPTY_STRING(retval); + } + break; case XML_PI_NODE: case XML_ENTITY_DECL: case XML_ENTITY_REF_NODE: case XML_NOTATION_NODE: - ZVAL_STRING(retval, (char *) nodep->name); + ZVAL_STRING(retval, (const char *) nodep->name); break; case XML_CDATA_SECTION_NODE: ZVAL_STRING(retval, "#cdata-section"); diff --git a/ext/dom/tests/gh17500.phpt b/ext/dom/tests/gh17500.phpt new file mode 100644 index 00000000000..02082ed7629 --- /dev/null +++ b/ext/dom/tests/gh17500.phpt @@ -0,0 +1,14 @@ +--TEST-- +GH-17500 (Segfault with requesting nodeName on nameless doctype) +--EXTENSIONS-- +dom +--FILE-- +loadHTML("", LIBXML_NOERROR); +var_dump($doc->doctype->nodeName); + +?> +--EXPECT-- +string(0) ""