Skip to content

SimpleXML infinite loop when getName() is called within foreach #12192

Description

@ndossche

Description

The following code:

<?php

$xml = "<root><a/><a/></root>";
$xml = simplexml_load_string($xml);

$a = $xml->a;

foreach ($a as $test) {
    var_dump($a->key());
    var_dump($a->getName());
}

Resulted in this output:
an infinite loop

But I expected this output instead:

string(1) "a"
string(1) "a"

This happens because getName() resets the iterator to the start. It should be fixed by restoring the iterator after fetching the name...

PHP Version

At least 8.1+, probably a very old bug

Operating System

Linux

Activity

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions