Skip to content

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

Closed

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

  1. self-assigned this
    on Sep 12, 2023
  2. added a commit that references this issue on Sep 12, 2023
    621c25a
  3. added 3 commits that reference this issue on Sep 16, 2023
    5df473d
    4bb27e2
    fad0e4f
  4. added a commit that references this issue on Jan 31, 2024
    4d888cf
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