Skip to content

Fix php://temp reporting EOF before the end of data is read - #25662

Open
instantraaamen wants to merge 1 commit into
php:masterfrom
instantraaamen:fix-php-temp-early-eof
Open

instantraaamen wants to merge 1 commit into
php:masterfrom
instantraaamen:fix-php-temp-early-eof

Conversation

@instantraaamen

Copy link
Copy Markdown

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 (and SplTempFileObject, which uses it), feof() becomes true right after the last line has been read.
With php://memory and 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.

$h = fopen('php://memory', 'w+');
fwrite($h, "a\nb\nc\n");
rewind($h);
$n = 0;
while (!feof($h)) {
    $n++;
    echo "#{$n}: ", json_encode(fgets($h)), "\n";
}
#1: "a\n"
#2: "b\n"
#3: "c\n"
#4: false
$h = fopen('php://temp', 'w+');
fwrite($h, "a\nb\nc\n");
rewind($h);
$n = 0;
while (!feof($h)) {
    $n++;
    echo "#{$n}: ", json_encode(fgets($h)), "\n";
}
#1: "a\n"
#2: "b\n"
#3: "c\n"

The data that is read is the same, but with php://temp the final false is missing.
The same happens with while (!$f->eof()) $f->fgets(); on SplTempFileObject: 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.

# Case Before 8.4.28 Before 8.5.13 Before 8.6.0 After
1 Regular file + loop on feof() 4 4 4 4
2 php://memory + loop on feof() 4 4 4 4
3 php://temp + loop on feof() 3 ⚠️ 3 ⚠️ 3 ⚠️ 4
4 php://temp + loop on the return value of fgets() 3 3 3 3
5 SplFileObject (regular file) + loop on eof() 4 4 4 4
6 SplTempFileObject + loop on eof() 3 ⚠️ 3 ⚠️ 3 ⚠️ 4

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

_php_stream_read() keeps reading memory / temp / regular-file streams until the requested amount is filled. So when the outer temp stream fills its buffer, the inner memory stream is read to the end and its EOF flag is set. php_stream_temp_read() copies this EOF flag to the outer stream unconditionally, so the outer stream is treated as EOF while its buffer still holds data.

Fix

The inner EOF is now only propagated to the outer stream when the read returned no data at all.

stream->eof = got <= 0 && ts->innerstream->eof;

In addition, the type of got was changed to ssize_t to match the return type of php_stream_read().

Tests

  • New: temp_stream_eof.phpt, SplTempFileObject_iteration_eof.phpt (both fail without the fix)
  • Two existing tests had expectations that relied on the premature EOF of php://temp, so I updated them. The original purpose of each test (the key() values, and $closing being passed to the filter) is still covered.
    • gh8273.phpt
    • bug79984.phpt

Compatibility

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.

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().
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.

1 participant