diff --git a/ext/dom/node.c b/ext/dom/node.c index 83404d0f598d..bbe8311147f9 100644 --- a/ext/dom/node.c +++ b/ext/dom/node.c @@ -2263,7 +2263,11 @@ static void dom_canonicalization(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{ } zend_hash_init(&links, 0, NULL, NULL, false); - dom_relink_ns_decls(&links, xmlDocGetRootElement(docp)); + xmlNodePtr root_element = xmlDocGetRootElement(docp); + + if (root_element) { + dom_relink_ns_decls(&links, root_element); + } } else if (!docp) { /* Note: not triggerable with modern DOM */ zend_throw_error(NULL, "Node must be associated with a document"); diff --git a/ext/dom/tests/modern/html/gh21688.phpt b/ext/dom/tests/modern/html/gh21688.phpt new file mode 100644 index 000000000000..ddfb4d326c63 --- /dev/null +++ b/ext/dom/tests/modern/html/gh21688.phpt @@ -0,0 +1,13 @@ +--TEST-- +GH-21688 (SEGV in C14N on empty HTMLDocument) +--CREDITS-- +YuanchengJiang +--EXTENSIONS-- +dom +--FILE-- +C14N()); +?> +--EXPECT-- +string(0) ""