xref: /PHP-8.2/ext/dom/tests/bug70359.phpt (revision f2d673fb)
1--TEST--
2Bug #70359 (print_r() on DOMAttr causes Segfault in php_libxml_node_free_list())
3--EXTENSIONS--
4dom
5--FILE--
6<?php
7
8echo "-- Test without parent --\n";
9
10$dom = new DOMDocument();
11$dom->loadXML(<<<XML
12<?xml version="1.0" encoding="UTF-8"?>
13<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="fooooooooooooooooooooo"/>
14XML);
15$spaceNode = $dom->documentElement->getAttributeNode('xmlns');
16print_r($spaceNode);
17
18echo "-- Test with parent and non-ns attribute --\n";
19
20$dom = new DOMDocument();
21$dom->loadXML(<<<XML
22<?xml version="1.0" encoding="UTF-8"?>
23<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
24<url xmlns:xsi="fooooooooooooooooooooo" myattrib="bar"/>
25</urlset>
26XML);
27$spaceNode = $dom->documentElement->firstElementChild->getAttributeNode('myattrib');
28var_dump($spaceNode->nodeType);
29var_dump($spaceNode->nodeValue);
30
31$dom->documentElement->firstElementChild->remove();
32try {
33    print_r($spaceNode->parentNode);
34} catch (\Error $e) {
35    echo $e->getMessage(), "\n";
36}
37
38echo "-- Test with parent and ns attribute --\n";
39
40$dom = new DOMDocument();
41$dom->loadXML(<<<XML
42<?xml version="1.0" encoding="UTF-8"?>
43<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
44<url xmlns:xsi="fooooooooooooooooooooo" myattrib="bar"/>
45</urlset>
46XML);
47$spaceNode = $dom->documentElement->firstElementChild->getAttributeNode('xmlns:xsi');
48print_r($spaceNode);
49
50$dom->documentElement->firstElementChild->remove();
51var_dump($spaceNode->parentNode->nodeName); // Shouldn't crash
52
53?>
54--EXPECT--
55-- Test without parent --
56DOMNameSpaceNode Object
57(
58    [nodeName] => xmlns
59    [nodeValue] => http://www.sitemaps.org/schemas/sitemap/0.9
60    [nodeType] => 18
61    [prefix] =>
62    [localName] => xmlns
63    [namespaceURI] => http://www.sitemaps.org/schemas/sitemap/0.9
64    [ownerDocument] => (object value omitted)
65    [parentNode] => (object value omitted)
66)
67-- Test with parent and non-ns attribute --
68int(2)
69string(3) "bar"
70Couldn't fetch DOMAttr. Node no longer exists
71-- Test with parent and ns attribute --
72DOMNameSpaceNode Object
73(
74    [nodeName] => xmlns:xsi
75    [nodeValue] => fooooooooooooooooooooo
76    [nodeType] => 18
77    [prefix] => xsi
78    [localName] => xsi
79    [namespaceURI] => fooooooooooooooooooooo
80    [ownerDocument] => (object value omitted)
81    [parentNode] => (object value omitted)
82)
83string(3) "url"
84