Fix GH-21742: SplFileObject::fgets() throws at EOF in eof/fgets loop#21853
Merged
Girgias merged 1 commit intophp:masterfrom Apr 23, 2026
Merged
Fix GH-21742: SplFileObject::fgets() throws at EOF in eof/fgets loop#21853Girgias merged 1 commit intophp:masterfrom
Girgias merged 1 commit intophp:masterfrom
Conversation
The SplFileObject iterator-desync fix in 08dad09 made spl_filesystem_file_read_ex throw "Cannot read from file" on the NULL-buffer path. SplFileObject::fgets() now throws inside the documented while (!$spl->eof()) $spl->fgets() idiom, because eof() returns false until a read attempt returns zero bytes. Keep the stricter semantics for next(), seek(), current(), fscanf(). Narrow fgets() to silent=true and return empty string on FAILURE, restoring the PHP-8.5 contract. Fixes phpGH-21742
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix in
08dad097025madespl_filesystem_file_read_exthrowCannot read from fileon NULL-buffer.SplFileObject::fgets()now throws inside the documentedwhile (!$spl->eof()) $spl->fgets(), because eof() returns false until a read attempt returns zero bytes, so the loop always makes one extra fgets call past the final newline.Narrow fgets to
silent=trueand return empty string when the stream is at EOF; keep the throw for non-EOF read failures so userland wrappers that return NULL without setting EOF still surface the error. next, seek, current, fscanf keep the stricter semantics.Fixes #21742 - Specifically the issue reported by @xabbuh
P.S. Not a new issue, side-effect of earlier SPL fixes only in master, so no news entry is needed.