php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #60338 SplFixedArray::key returns index for invalid keys
Submitted: 2011-11-19 13:32 UTC Modified: 2016-03-20 18:15 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: gooh@php.net Assigned: levim (profile)
Status: Wont fix Package: SPL related
PHP Version: 5.4SVN-2011-11-19 (SVN) OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: gooh@php.net
New email:
PHP Version: OS:

 

 [2011-11-19 13:32 UTC] gooh@php.net
Description:
------------
SplFixedArray::key() will return a value even when the key does not exist in the SplFixedArray. This does not conform to the behavior we have in regular arrays, ArrayObject and ArrayIterator. SplFixedArray::key() should return NULL when the current key does not exist to conform.

Test script:
---------------
http://codepad.viper-7.com/4hWmUn

Expected result:
----------------
NULL NULL NULL NULL 

Actual result:
--------------
int(3) NULL NULL NULL 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-11-19 13:38 UTC] gooh@php.net
Apparently the Expected Output is achieved when using key() instead of SplFixedArray::key(). See http://codepad.viper-7.com/I3REjD. Thanks to NikiC for pointing it out.
 [2013-09-21 23:38 UTC] levim@php.net
I personally don't see why we should bring this into line with array, ArrayObject 
and ArrayIterator behavior. The fact of the matter is simple: you are calling 
`key()` on an iterator that is not in a valid state; therefore the behavior is 
undefined. The fact that array and the other classes you mention return `NULL` is 
an implementation detail, the only one of which I believe is guaranteed is the 
case of an array.
 [2014-02-27 04:54 UTC] levim@php.net
-Assigned To: +Assigned To: levim
 [2016-03-20 18:15 UTC] nikic@php.net
-Status: Assigned +Status: Wont fix
 [2016-03-20 18:15 UTC] nikic@php.net
Agree with Levi: The return value of key() and other Iterator methods on an invalidated iterator is undefined.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 05:01:28 2024 UTC