php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44681 Resources used as array keys get converted to 0
Submitted: 2008-04-10 09:32 UTC Modified: 2008-05-09 09:30 UTC
From: ilewis at uk dot ibm dot com Assigned:
Status: Closed Package: Arrays related
PHP Version: 5.3CVS-2008-04-10 (snap) OS: Windows XP SP2
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ilewis at uk dot ibm dot com
New email:
PHP Version: OS:

 

 [2008-04-10 09:32 UTC] ilewis at uk dot ibm dot com
Description:
------------
Using todays prebuilt snap of 5.3 on Windows, it looks like resources being used as array keys are being converted to int(0). The prevents them being used as unique array keys. This is a big change from php 5.2.5,

Reproduce code:
---------------
<?php

error_reporting(E_ALL | E_STRICT);

$fres = fopen("g:/tmp/arraytest.php", "r");
$fres2 = fopen("g:/tmp/arraytest.phpb", "w");

var_dump($fres);
var_dump($fres2);

$myarray = array(1,2);

var_dump($myarray);

$myarray[$fres] = "bob";
$myarray[$fres2] = "bob2";

var_dump($myarray);

?>


Expected result:
----------------
PHP Strict Standards:  Resource ID#5 used as offset, casting to integer (5) in G:\tmp\arraytest.php on line 15


PHP Strict Standards:  Resource ID#6 used as offset, casting to integer (6) in G:\tmp\arraytest.php on line 16


array(4) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [5]=>
  string(3) "bob"
  [6]=>
  string(4) "bob2"
}

Actual result:
--------------
Strict Standards: Resource ID#5 used as offset, casting to integer (5) in G:\tmp\arraytest.php on line 15

Strict Standards: Resource ID#6 used as offset, casting to integer (6) in G:\tmp\arraytest.php on line 16

array(2) {
  [0]=>
  string(4) "bob2"
  [1]=>
  int(2)
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-04-13 10:42 UTC] mattwil@php.net
This was introduced after the Jan 23rd changes to zend_execute.c, causing the resource ID to be read as a double. case statements just need to be rearranged. :-)

I also noticed that trying to use a resource key like array($fres => 'bob') doesn't work (never has) and just gives "Illegal offset type." Seems like it should be consistent either way? The following patches also make that work, if they want to add it.

http://realplain.com/php/bug44681.diff
http://realplain.com/php/bug44681_5_3.diff
 [2008-05-09 09:30 UTC] mattwil@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.

Just fixed the reported bug with resources used as offsets: $myarray[$fres]. They will still give "Illegal offset type" (as always) if used as an index in an array( ... ) construct. I wasn't sure if that should be changed to be consistent...
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon May 27 21:01:33 2024 UTC