From 9978ee56b9dc728723dc86ef630da10cfd631ffc Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Wed, 9 Oct 2024 18:11:22 +0200 Subject: [PATCH] Fix GH-16316: DOMXPath breaks when not initialized properly --- ext/dom/tests/gh16316.phpt | 32 ++++++++++++++++++++++++++++++++ ext/dom/xpath.c | 5 +++++ 2 files changed, 37 insertions(+) create mode 100644 ext/dom/tests/gh16316.phpt diff --git a/ext/dom/tests/gh16316.phpt b/ext/dom/tests/gh16316.phpt new file mode 100644 index 0000000000000..43368746bc462 --- /dev/null +++ b/ext/dom/tests/gh16316.phpt @@ -0,0 +1,32 @@ +--TEST-- +GH-16316 (DOMXPath breaks when not initialized properly) +--EXTENSIONS-- +dom +--FILE-- +getMessage(), "\n"; +} + +try { + var_dump($demo->document); +} catch (DOMException $e) { + echo $e->getMessage(), "\n"; +} + +?> +--EXPECT-- +object(Demo)#1 (1) { + ["registerNodeNamespaces"]=> + bool(true) +} +Invalid State Error +Invalid State Error diff --git a/ext/dom/xpath.c b/ext/dom/xpath.c index 22a7e0ecc5a91..cad08e0a8d8a6 100644 --- a/ext/dom/xpath.c +++ b/ext/dom/xpath.c @@ -264,6 +264,11 @@ int dom_xpath_document_read(dom_object *obj, zval *retval) docp = (xmlDocPtr) ctx->doc; } + if (UNEXPECTED(!docp)) { + php_dom_throw_error(INVALID_STATE_ERR, /* strict */ true); + return FAILURE; + } + php_dom_create_object((xmlNodePtr) docp, retval, obj); return SUCCESS; }