|  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: 2022-06-14 17:06 UTC
From: jules dot bernable at gmail dot com Assigned: beberlei (profile)
Status: Closed Package: DOM XML related
PHP Version: 8.0.0 OS: debian
Private report: No CVE-ID: None
 [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

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);
 [2022-06-14 17:06 UTC]
The following pull request has been associated:

Patch Name: Fix bug #80602
On GitHub:
 [2023-01-25 03:27 UTC] kertzmann8 at gmail dot com
I’ve especially stayed aware of the article and I will get many benefits from it.
 [2023-03-30 19:02 UTC]
Automatic comment on behalf of NathanFreeman (author) and nielsdos (committer)
Log: Fix bug #80602: Segfault when using DOMChildNode::before()
 [2023-03-30 19:02 UTC]
-Status: Verified +Status: Closed
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Wed May 31 11:03:37 2023 UTC