Skip to content

Fix #62004: SplFileObject: fgets after seek returns wrong line #6434

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
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions ext/spl/spl_directory.c
Original file line number Diff line number Diff line change
Expand Up @@ -2727,7 +2727,7 @@ PHP_METHOD(SplFileObject, ftruncate)
PHP_METHOD(SplFileObject, seek)
{
spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
zend_long line_pos;
zend_long line_pos, i;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &line_pos) == FAILURE) {
RETURN_THROWS();
Expand All @@ -2742,11 +2742,15 @@ PHP_METHOD(SplFileObject, seek)

spl_filesystem_file_rewind(ZEND_THIS, intern);

while(intern->u.file.current_line_num < line_pos) {
for (i = 0; i < line_pos; i++) {
if (spl_filesystem_file_read_line(ZEND_THIS, intern, 1) == FAILURE) {
break;
return;
}
}
if (line_pos > 0) {
intern->u.file.current_line_num++;
spl_filesystem_file_free_line(intern);
}
} /* }}} */

/* {{{ PHP_MINIT_FUNCTION(spl_directory) */
Expand Down
4 changes: 2 additions & 2 deletions ext/spl/tests/SplFileObject_key_error001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ Erwin Poeze <erwin.poeze@gmail.com>
//line 5
$s = new SplFileObject(__FILE__);

$s->seek(12);
$s->seek(13);
$s->next();
var_dump($s->key());
var_dump($s->valid());
?>
--EXPECT--
int(13)
int(14)
bool(false)
2 changes: 1 addition & 1 deletion ext/spl/tests/SplFileObject_next_variation002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ echo $s->current();
--EXPECT--
//line 3
//line 4
//line 3
//line 4
//line 5
5 changes: 5 additions & 0 deletions ext/spl/tests/bug46569.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
first,line
second,line
third,line
fourth,line
fifth,line
14 changes: 14 additions & 0 deletions ext/spl/tests/bug46569.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Bug #46569 (SplFileObject: fgetcsv after seek returns wrong line)
--FILE--
<?php
$file = new SplFileObject(__DIR__ . '/bug46569.csv');
$file->seek(1);
print_r($file->fgetcsv());
?>
--EXPECT--
Array
(
[0] => second
[1] => line
)
17 changes: 17 additions & 0 deletions ext/spl/tests/bug62004.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
Bug #62004 (SplFileObject: fgets after seek returns wrong line)
--FILE--
<?php
$f = new SplFileObject(__DIR__ . '/bug62004.txt');
$f->setFlags(SplFileObject::SKIP_EMPTY);
$f->seek(0);
echo $f->fgets();
$f->seek(1);
echo $f->fgets();
$f->seek(2);
echo $f->fgets();
?>
--EXPECT--
Line 1
Line 2
Line 3
4 changes: 4 additions & 0 deletions ext/spl/tests/bug62004.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Line 1
Line 2
Line 3
Line 4
2 changes: 1 addition & 1 deletion ext/spl/tests/fileobject_getcurrentline_basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ echo $s->getCurrentLine();
echo $s->getCurrentLine();
?>
--EXPECT--
//line 2
//line 3
//line 4