Skip to content

DOMElement::replaceWith causes crash #11290

Closed

Description

@fluffycondor

Description

This is a longstanding one (sorry for the php-dom day!). Sometimes the same code from #11288 causes crash on every 8.x PHP version. The code is the same, but the html markup is slightly differs.

Live reproducing:
https://3v4l.org/KjJgQ // On every PHP 8.x version throws DOMException: Not Found Error, hangs for 1-15 seconds, then crashes.
https://3v4l.org/FdOZR // Hangs for 1-15 seconds, crashes on every PHP versions but PHP 8.2.6 and 8.1.19.

It's kinda related to #11289 and #11288, but this is a longstanding one. I guess this is a part of a big picture, so I hope this would help to investigate the main cause of weird php-dom behavior.

The following code:

<?php
$html = <<<HTML
<!DOCTYPE HTML>
<html>
<body>
    <p><span class="unwrap_me">Lorem</span><span class="unwrap_me">ipsum</span><span class="unwrap_me">dolor</span></p>
</body>
</html>
HTML;

$dom = new DOMDocument();
$dom->loadHTML($html);

$spans = iterator_to_array($dom->getElementsByTagName('span')->getIterator());
foreach ($spans as $span) {
    if ('unwrap_me' === $span->getAttribute('class')) {
        $span->replaceWith(...$span->childNodes);
    }
}

results in crash on every PHP 8.x version.

PHP Version

PHP 8.x

Operating System

No response

Activity

  1. fluffycondor commented on May 22, 2023

    @fluffycondor
    Author

    Oh, looks like this could be a duplicate of #9142.

  2. ndossche commented on May 22, 2023

    @ndossche
    Member

    This one's also fixed by #11299, so I'll amend that PR to include this test. Thanks for the report & testcase!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions