Skip to content

SimpleXML infinite loop when a cast is used inside a foreach #12208

Closed

Description

@ndossche

Description

The following code:

<?php

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

$a = $xml->a;

foreach ($a as $test) {
    var_dump((string) $a->current());
    var_dump((string) $a);
}

Resulted in this output:
an infinite loop

But I expected this output instead:

string(1) "1"
string(1) "1"
string(1) "2"
string(1) "1"

I think at least, because $a always refers to the first element implicitly, and only current points to the current iterated-over item?

This is basically the same as #12192, but triggered in a different yet similar way.

PHP Version

PHP 8.1+

Operating System

Linux

Activity

  1. added a commit that references this issue on Sep 17, 2023
    961869f
  2. added 2 commits that reference this issue on Sep 17, 2023
    486276f
    f4f34b6
  3. added 2 commits that reference this issue on Sep 17, 2023
    673babe
    7f0705b
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