Skip to content

ext/dom: Fix Dom\Notation nodes missing tree connection#21868

Closed
jordikroon wants to merge 5 commits intophp:masterfrom
jordikroon:fix/dom-notation-wiring
Closed

ext/dom: Fix Dom\Notation nodes missing tree connection#21868
jordikroon wants to merge 5 commits intophp:masterfrom
jordikroon:fix/dom-notation-wiring

Conversation

@jordikroon
Copy link
Copy Markdown
Contributor

Fixes an existing TODO by @ndossche.

Notation nodes returned from $doctype->notations were not linked to their owning document or parent DocumentType. This caused several incorrect behaviour; including:

  • ownerDocument was missing
  • parentNode was NULL
  • isConnected returned false
  • baseURI fell back to "about:blank"
  • textContent returned an empty string instead of NULL

The last point is a violation of the DOM specification. Since Notation is not an Element, CharacterData, Attr, or DocumentFragment, its textContent must return NULL.

Spec reference: https://dom.spec.whatwg.org/#dom-node-textcontent

@jordikroon jordikroon force-pushed the fix/dom-notation-wiring branch from b5f92c2 to fe554cf Compare April 24, 2026 18:38
@devnexen
Copy link
Copy Markdown
Member

devnexen commented Apr 24, 2026

I have a memory leak with this test

<?php
$xml = <<<XML
<!DOCTYPE root [
    <!NOTATION GIF SYSTEM "image/gif">
    <!NOTATION JPEG PUBLIC "-//W3C//NOTATION JPEG//EN" "image/jpeg">
]>
<root/>
XML;

$dom = Dom\XMLDocument::createFromString($xml);

for ($i = 0; $i < 3; $i++) {
    $n = $dom->doctype->notations->getNamedItem('GIF');
    $n = $dom->doctype->notations->getNamedItem('JPEG');
    unset($n);
}
==243031==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 864 byte(s) in 6 object(s) allocated from:
    #0 0x55d3990a8398 in malloc (/tmp/php-src/sapi/cli/php+0x4a8398) (BuildId: 1c230b699569f9967bf81ebb00a650988950221d)
    #1 0x55d399389278 in create_notation /tmp/php-src/ext/dom/dom_iterators.c:59:21
    #2 0x55d3993e3f91 in dom_map_get_ns_named_item_notation /tmp/php-src/ext/dom/obj_map.c:509:10
    #3 0x55d3993e27a3 in php_dom_obj_map_get_ns_named_item_into_zval /tmp/php-src/ext/dom/obj_map.c:482:24
    #4 0x55d3993bfd49 in zim_DOMNamedNodeMap_getNamedItem /tmp/php-src/ext/dom/namednodemap.c:64:2
    #5 0x55d39a4f4ed5 in ZEND_DO_FCALL_SPEC_RETVAL_USED_TAILCALL_HANDLER /tmp/php-src/Zend/zend_vm_execute.h:54920:4
    #6 0x55d39a2766a3 in execute_ex /tmp/php-src/Zend/zend_vm_execute.h:110168:12
    #7 0x55d39a276eef in zend_execute /tmp/php-src/Zend/zend_vm_execute.h:115586:2
    #8 0x55d39a7f69f0 in zend_execute_script /tmp/php-src/Zend/zend.c:1971:3
    #9 0x55d399f23b3f in php_execute_script_ex /tmp/php-src/main/main.c:2646:13
    #10 0x55d399f240d8 in php_execute_script /tmp/php-src/main/main.c:2686:9
    #11 0x55d39a7fc7ed in do_cli /tmp/php-src/sapi/cli/php_cli.c:947:5
    #12 0x55d39a7fac57 in main /tmp/php-src/sapi/cli/php_cli.c:1370:18
    #13 0x7fe8f65bbf76 in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16
    #14 0x7ffdc955bfe7  (<unknown module>)

@devnexen
Copy link
Copy Markdown
Member

hint: XML_NOTATION_NODE.

@jordikroon
Copy link
Copy Markdown
Contributor Author

Good catch. Technically this memory leak existed before, but it didn't surface because parent and doc were NULL.

@devnexen
Copy link
Copy Markdown
Member

look nice, I ll have a better look this week-end.

Comment thread ext/dom/php_dom.c Outdated
Comment thread ext/dom/tests/modern/xml/XMLDocument_node_notation_wiring.phpt Outdated
Comment thread ext/dom/tests/modern/xml/XMLDocument_node_notation_wiring.phpt Outdated
jordikroon and others added 2 commits April 25, 2026 14:43
Co-authored-by: David CARLIER <devnexen@gmail.com>
Copy link
Copy Markdown
Member

@devnexen devnexen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM if CI green (minus unrelated items). Thanks for the work !

@devnexen devnexen closed this in 9498bc3 Apr 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants