|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80602 Segfault when using DOMChildNode::before()
Submitted: 2021-01-06 20:34 UTC Modified: 2021-01-18 16:14 UTC
From: jules dot bernable at gmail dot com Assigned: beberlei (profile)
Status: Verified Package: DOM XML related
PHP Version: 8.0.0 OS: debian
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2021-01-06 20:34 UTC] jules dot bernable at gmail dot com
When calling DOMChildNode::before() on an element, if one of the parameters is a text node that is the previous sibling of that element, the program segfaults.

Test script:
<?php declare(strict_types=1);

$doc = new \DOMDocument();
$target = $doc->documentElement->lastChild;
$target->before('bar', $doc->documentElement->firstChild, 'baz');
echo $doc->saveXML($doc->documentElement);

Expected result:

Actual result:
free(): double free detected in tcache 2


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2021-01-06 20:45 UTC]
-Status: Open +Status: Verified
 [2021-01-18 16:05 UTC]
-Assigned To: +Assigned To: beberlei
 [2021-01-18 16:05 UTC]
I believe this happens because can free "newNode" according to libxml docs:

> Add a new node to @parent, at the end of the child (or property) list merging adjacent TEXT nodes (in which case @cur is freed)

This seems like rather peculiar behavior, guess that it's necessary to handle text nodes in some other way that does not end up destroying them?
 [2021-01-18 16:14 UTC]
Confirmed, will take a look asap.
 [2021-02-01 22:25 UTC]
Confusingly, it segfaults with $target->before('bar', $doc->documentElement->firstChild, 'baz');

but it works with $target->before($doc->documentElement->firstChild, 'baz'); or even $target->before($doc->documentElement->firstChild);
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Mon Oct 25 14:03:45 2021 UTC