Fix registerNodeClass with abstract class crashing

This always results in a segfault when trying to instantiate, so this never
worked. At least throw an error instead of segfaulting to prevent developers
from being confused.

Closes GH-12420.
This commit is contained in:
Niels Dossche 2023-10-11 20:51:52 +02:00
parent 734afa0ba8
commit d7de0ceca6
3 changed files with 31 additions and 0 deletions

3
NEWS
View file

@ -2,6 +2,9 @@ PHP NEWS
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
?? ??? ????, PHP 8.1.26
- DOM:
. Fix registerNodeClass with abstract class crashing. (nielsdos)
- Fiber:
. Fixed bug GH-11121 (ReflectionFiber segfault). (danog, trowski, bwoebi)

View file

@ -2096,6 +2096,10 @@ PHP_METHOD(DOMDocument, registerNodeClass)
}
if (ce == NULL || instanceof_function(ce, basece)) {
if (UNEXPECTED(ce != NULL && (ce->ce_flags & ZEND_ACC_ABSTRACT))) {
zend_argument_value_error(2, "must not be an abstract class");
RETURN_THROWS();
}
DOM_GET_OBJ(docp, id, xmlDocPtr, intern);
dom_set_doc_classmap(intern->document, basece, ce);
RETURN_TRUE;

View file

@ -0,0 +1,24 @@
--TEST--
registerNodeClass() with an abstract class should fail
--EXTENSIONS--
dom
--FILE--
<?php
abstract class Test extends DOMElement {
abstract function foo();
}
$dom = new DOMDocument;
try {
$dom->registerNodeClass("DOMElement", "Test");
} catch (ValueError $e) {
echo "ValueError: ", $e->getMessage(), "\n";
}
$dom->createElement("foo");
?>
--EXPECT--
ValueError: DOMDocument::registerNodeClass(): Argument #2 ($extendedClass) must not be an abstract class