Skip to content

Commit

Permalink
Fix registerNodeClass with abstract class crashing
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nielsdos committed Oct 13, 2023
1 parent 734afa0 commit d7de0ce
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 4 additions & 0 deletions ext/dom/document.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
24 changes: 24 additions & 0 deletions ext/dom/tests/registerNodeClass_abstract_class.phpt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d7de0ce

Please sign in to comment.