php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65492 Seeking beyond EOF leads to inconsistent results for key() and current()
Submitted: 2013-08-21 06:50 UTC Modified: 2013-10-16 04:59 UTC
From: foo at example dot com Assigned: levim (profile)
Status: Not a bug Package: SPL related
PHP Version: Irrelevant OS: any
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: foo at example dot com
New email:
PHP Version: OS:

 

 [2013-08-21 06:50 UTC] foo at example dot com
Description:
------------
When seek()'ing beyond the end of file in an SplFileObject, SplFileObject::key() 
will return the number of the last line in the file but SplFileObject::current() 
will return false. This is inconsistent. 

Either SplFileObject::current() returns the content of the last line then, too. Or 
SplFileObject::key() should return false as well. As an alternative, 
SplFileObject::seek() could raise an OutOfBoundsException when trying to seek 
beyond EOF.

Test script:
---------------
$txt = <<< TXT
foo
bar
baz
TXT;

$file = new SplTempFileObject(-1);
$file->fwrite($txt);
$file->seek(100);
var_dump( $file->key(), $file->fgetcsv());

Expected result:
----------------
int(2)
baz

or

false
false

or 

OutOfBoundsException

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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-08-21 06:56 UTC] foo at example dot com
Sorry, the last line in the Test Script should read

    var_dump( $file->key(), $file->current() );

The problem does exist for fgetcsv() though, too. It will return NULL instead of 
the data at the last line.
 [2013-09-19 18:55 UTC] bixuehujin at gmail dot com
Another inconsistent:

Script:
----------------
$txt = <<< TXT
foo
bar
baz
TXT;

$file = new SplTempFileObject(-1);
$file->fwrite($txt);
$file->seek(0);
var_dump($file->fgets());
$file->seek(1);
var_dump($file->fgets());

Expect result:
----------------
string(4) "foo
"
string(3) "bar
"

Actual result:
--------------
string(4) "foo
"
string(3) "baz"

BTW:
---------------
The issue also existed in SplFileObject.
 [2013-09-19 18:58 UTC] bixuehujin at gmail dot com
Sorry, the expect result should be:

Expect result:
----------------
string(4) "foo
"
string(4) "bar
"
 [2013-10-16 01:52 UTC] levim@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: levim
 [2013-10-16 01:52 UTC] levim@php.net
When `valid()` returns `false` and then you use any other Iterator method the result is undefined. This does properly return `false` for `valid()`, so there is no issue here.
 [2013-10-16 02:22 UTC] levim@php.net
I should have said "any other Iterator method besides `rewind` except on iterators that can't be rewound anyway".
 [2013-10-16 04:59 UTC] nikic@php.net
-Status: Closed +Status: Not a bug
 [2014-06-29 07:05 UTC] tim_siebels_aurich at yahoo dot de
So, using Iteratormethods after valid() returns false is undefined behaviour? Why do run tests against this? (i.e. SplFileObject_seek_error002.php)

This isn't a bug, but "fixing" this would also be fine?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 02:01:29 2024 UTC