Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nielsdos opened this issue Sep 12, 2023 · 0 comments
Closed

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

nielsdos opened this issue Sep 12, 2023 · 0 comments

Comments

@nielsdos
Copy link
Member

nielsdos commented Sep 12, 2023

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

@nielsdos nielsdos self-assigned this Sep 12, 2023
nielsdos added a commit to nielsdos/php-src that referenced this issue Sep 12, 2023
…hin foreach

This happens because getName() resets the iterator to the start because
it overwrites the iterator data.
We add a version of get_first_node that does not overwrite the iterator
data.
nielsdos added a commit that referenced this issue Sep 16, 2023
* PHP-8.1:
  Fix GH-12223: Entity reference produces infinite loop in var_dump/print_r
  Fix GH-12192: SimpleXML infinite loop when getName() is called within foreach
  Fix GH-12186: segfault copying/cloning a finalized HashContext
nielsdos added a commit that referenced this issue Sep 16, 2023
* PHP-8.2:
  Fix GH-12223: Entity reference produces infinite loop in var_dump/print_r
  Fix GH-12192: SimpleXML infinite loop when getName() is called within foreach
  Fix GH-12186: segfault copying/cloning a finalized HashContext
nielsdos added a commit that referenced this issue Sep 16, 2023
* PHP-8.3:
  Fix GH-12223: Entity reference produces infinite loop in var_dump/print_r
  Fix GH-12192: SimpleXML infinite loop when getName() is called within foreach
  Fix GH-12186: segfault copying/cloning a finalized HashContext
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant