Skip to content

Fix GH-21742: SplFileObject::fgets() throws at EOF in eof/fgets loop#21853

Merged
Girgias merged 1 commit intophp:masterfrom
iliaal:fix/gh21742-fgets-eof
Apr 23, 2026
Merged

Fix GH-21742: SplFileObject::fgets() throws at EOF in eof/fgets loop#21853
Girgias merged 1 commit intophp:masterfrom
iliaal:fix/gh21742-fgets-eof

Conversation

@iliaal
Copy link
Copy Markdown
Contributor

@iliaal iliaal commented Apr 23, 2026

Fix in 08dad097025 made spl_filesystem_file_read_ex throw Cannot read from file on NULL-buffer. SplFileObject::fgets() now throws inside the documented while (!$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=true and 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.

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
Copy link
Copy Markdown
Member

@Girgias Girgias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MSTM

@Girgias Girgias merged commit 9a530a1 into php:master Apr 23, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feedback: Please let us know if #21679 fixes in SplFileObject will be backported or not

2 participants