php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68873 First element of array of the SplInt values corrupted after array iterated
Submitted: 2015-01-21 10:58 UTC Modified: 2017-03-13 08:13 UTC
From: i dot tech dot inspector at gmail dot com Assigned:
Status: Closed Package: SPL_Types (PECL)
PHP Version: 5.5.20 OS: Irrelevant
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: i dot tech dot inspector at gmail dot com
New email:
PHP Version: OS:

 

 [2015-01-21 10:58 UTC] i dot tech dot inspector at gmail dot com
Description:
------------
First element of array of the SplInt values corrupted after array iterated.

Normal behavior with internal int:

$a = [1,2,3];
foreach($a as $k => $v) {}
var_dump($a); // $a[0] = 1, ok.

Bug with SplInt:

$a = [new \SplInt(1), new \SplInt(2), new \SplInt(3)];
foreach($a as $k => $v) {}
var_dump($a); // $a[0] = 3, but must be 1

The same situation with SplFloat.

SPL_Types version: 0.4.0 stable

Tested on PHP versions:
5.5.12 (Linux)
5.5.18 (Linux)
5.4.29 (Windows)

Test script:
---------------
class SplTypesTest extends PHPUnit_Framework_TestCase
{
    public function testSplInt()
    {
        $a = [new \SplInt(1), new \SplInt(2), new \SplInt(3)];
        foreach($a as $k => $v) {}
        $this->assertEquals(1, (int)$a[0]); // 3!
    }

    public function testSplFloat()
    {
        $a = [new \SplFloat(1.0), new \SplFloat(2.0), new \SplFloat(3.0)];
        foreach($a as $k => $v) {}
        $this->assertEquals(1.0, (float)$a[0]); // 3!
    }
}


Expected result:
----------------
Passing test.

Actual result:
--------------
PHPUnit 3.7.28 by Sebastian Bergmann.

F

Time: 283 ms, Memory: 2.75Mb

There was 1 failure:

1) SplIntTest::test1
Failed asserting that 3 matches expected 1.

/home/alex/webdev/test/SplIntTest.php:9

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-02-27 23:54 UTC] edgar dot r dot sandi at gmail dot com
Hi!

Please, test this script in the PHP 7.0.0-dev, i tested here in this version and the result is ok.

I guess that this behavior has been fixed in the RFC:
https://wiki.php.net/rfc/php7_foreach

I dont know if this fix will go merged in the PHP 5.x versions also.

I guess that no. 

Cheers.
Edgar R. Sandi
 [2015-02-28 01:09 UTC] edgar dot r dot sandi at gmail dot com
Hi again!

I'm so sorry for my previous comment, I reviewed my test and not, the result is not ok.

Maybe, anyone can help-us.

Cheers
Edgar R. Sandi
 [2017-01-10 08:14 UTC] kalle@php.net
-Status: Open +Status: Suspended
 [2017-01-10 08:14 UTC] kalle@php.net
Suspending this report as the extension have not had a release for almost 5 years.  Please revive this if the extension once again shows life
 [2017-03-13 08:13 UTC] i dot tech dot inspector at gmail dot com
-Status: Suspended +Status: Closed
 [2017-03-13 08:13 UTC] i dot tech dot inspector at gmail dot com
Not a bug.
This is how PHP works with references.
We should unset reference after "foreach circle" is done.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Oct 08 06:01:27 2024 UTC