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

Conversation

cmb69
Copy link
Member

@cmb69 cmb69 commented Nov 18, 2020

As it is, ::seek(0) sets the file pointer to the beginning of the
file, but ::seek($n) where $n > 0 sets the file pointer to the
beginning of the following line, having line $n already read into the
line buffer. This is pretty inconsistent; we fix it by always seeking
to the beginning of the line.


Frankly, I'm not really sure how that is supposed to behave. While SplFileObject::fgets() is documented as "returns a string containing the next line from the file", its alias SplFileObject::getCurrentLine() suggests otherwise. Anyway, the current behavior makes no sense. However, given the need to change other test cases suggest that a fix might better target PHP-8.0 or the master branch.

@cmb69 cmb69 added the Bug label Nov 18, 2020
@Girgias
Copy link
Member

Girgias commented Nov 18, 2020

I think fixing this in PHP 8.0 only, with a possible upgrading mention might make sense.

As it is, `::seek(0)` sets the file pointer to the beginning of the
file, but `::seek($n)` where `$n > 0` sets the file pointer to the
beginning of the following line, having line `$n` already read into the
line buffer.  This is pretty inconsistent; we fix it by always seeking
to the beginning of the line.
@cmb69 cmb69 changed the base branch from PHP-7.4 to PHP-8.0 November 19, 2020 22:30
@cmb69
Copy link
Member Author

cmb69 commented Nov 19, 2020

Given that the bug has been reported more than seven years ago, there seems no need to hurry, so I've rebased onto PHP-8.0. @sgolemon, @carusogabriel, are you fine with this landing in 8.0.1?

If we target the "master" branch only, I think an alternative solution which does not actually store every line temporarily in memory would be preferable.

@php-pulls php-pulls closed this in f1d11c1 Nov 30, 2020
@cmb69 cmb69 deleted the cmb/62004 branch November 30, 2020 15:05
php-pulls pushed a commit to php/doc-en that referenced this pull request Nov 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants