Skip to content

foreach by-ref may jump over keys during a rehash #11222

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
bwoebi opened this issue May 10, 2023 · 0 comments · Fixed by ThePHPF/thephp.foundation#90
Closed

foreach by-ref may jump over keys during a rehash #11222

bwoebi opened this issue May 10, 2023 · 0 comments · Fixed by ThePHPF/thephp.foundation#90

Comments

@bwoebi
Copy link
Member

bwoebi commented May 10, 2023

Description

The following code:

<?php

// Not packed
$a = ["k" => 0, 1 => 1, 2, 3, 4, 5, 6];
foreach ($a as $k => &$v) {
    if ($k == 1) {
        // force that it'll be rehashed by adding enough holes
        unset($a[4], $a[5]);
        // actually make the array larger than 8 elements to trigger rehash
        $a[] = 8; $a[] = 9; $a[] = 10;
        
    }
    // observe the iteration jumping from key 1 to key 6, skipping keys 2 and 3
    echo "$k => $v\n";
}

Resulted in this output:

k => 0
1 => 1
6 => 6
7 => 8
8 => 9
9 => 10

But I expected this output instead:

k => 0
1 => 1
2 => 2
3 => 3
6 => 6
7 => 8
8 => 9
9 => 10

PHP Version

PHP 7.2.11 - PHP 8.2

Operating System

No response

@bwoebi bwoebi self-assigned this May 10, 2023
@bwoebi bwoebi closed this as completed in 975d28e May 10, 2023
bwoebi added a commit to DataDog/dd-trace-php that referenced this issue May 10, 2023
…- 8.1

For details, see php/php-src#11222, which uses the same broken underlying API

Signed-off-by: Bob Weinand <[email protected]>
bwoebi added a commit to DataDog/dd-trace-php that referenced this issue May 10, 2023
…- 8.1

For details, see php/php-src#11222, which uses the same broken underlying API

Signed-off-by: Bob Weinand <[email protected]>
bwoebi added a commit to DataDog/dd-trace-php that referenced this issue May 10, 2023
…- 8.1

For details, see php/php-src#11222, which uses the same broken underlying API

Signed-off-by: Bob Weinand <[email protected]>
bwoebi added a commit to DataDog/dd-trace-php that referenced this issue May 11, 2023
…- 8.1

For details, see php/php-src#11222, which uses the same broken underlying API

Signed-off-by: Bob Weinand <[email protected]>
bwoebi added a commit to DataDog/dd-trace-php that referenced this issue May 11, 2023
…- 8.1 (#2045)

For details, see php/php-src#11222, which uses the same broken underlying API

Signed-off-by: Bob Weinand <[email protected]>
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.

2 participants
@bwoebi and others