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; }