[php-src] PR #25662: Fix php://temp reporting EOF before the end of data is read
| From: | instantraaamen | Date: | Mon, 23 Nov 2026 03:46:11 +0000 |
| Subject: | [php-src] PR #25662: Fix php://temp reporting EOF before the end of data is read | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-40915@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/25662
Author: instantraaamen
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.
```php
$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
```
```php
$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.
```c
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.