Repository navigation
Fix php://temp reporting EOF before the end of data is read - #25662
Open
instantraaamen wants to merge 1 commit into
Open
instantraaamen wants to merge 1 commit into
instantraaamen wants to merge 1 commit into
Conversation
php_stream_temp_read() copied the inner stream's EOF flag to the outer stream unconditionally. Since _php_stream_read() reads memory/plain inner streams greedily, the inner stream already hits EOF while the outer stream is still buffering the remaining data. As a result php://temp (and SplTempFileObject) reported feof() === true right after the last line was read, while php://memory and plain files only report EOF once a read actually hits the end of the data. This made loops like `while (!feof($fp)) fgets($fp);` and iteration over SplTempFileObject behave differently from SplFileObject on a real file. Only propagate the inner EOF when the read returned no data. Also use ssize_t for the result of php_stream_read().
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.
This description was drafted in Japanese with the help of an LLM, edited by me,
and translated into English. I reproduced the issue and verified all results myself.
Problem
With
php://temp(andSplTempFileObject, which uses it),feof()becomestrueright after the last line has been read.With
php://memoryand regular files, EOF is only reached once a read goes past the end, so the behaviour is inconsistent.The only difference between the two snippets below is the
fopen()call.The data that is read is the same, but with php://temp the final
falseis missing.The same happens with
while (!$f->eof()) $f->fgets();onSplTempFileObject: the loop runs 4 times for an SplFileObject on a regular file, but only 3 times for SplTempFileObject.The number of loop iterations per case is summarised below (same 3 lines of data in every case).
The "before" columns use the official Docker images; "after" is master built with this PR applied.
feof()feof()feof()fgets()eof()eof()Only cases 3 and 6 run 3 times, in every version before the fix.
Code that detects the end via the return value of
fgets(), as in case 4, behaves the same before and after the fix.Cause
Fix
The inner EOF is now only propagated to the outer stream when the read returned no data at all.
In addition, the type of
gotwas changed tossize_tto match the return type ofphp_stream_read().Tests
temp_stream_eof.phpt,SplTempFileObject_iteration_eof.phpt(both fail without the fix)key()values, and$closingbeing passed to the filter) is still covered.gh8273.phptbug79984.phptCompatibility
Loops over php://temp / SplTempFileObject driven by
feof()now run one more time at the end (the same behaviour as regular files).Since this is a behaviour change, this PR targets master and adds an UPGRADING note. I'm happy to retarget it to PHP-8.4 if preferred.