diff --git a/UPGRADING b/UPGRADING index 9dbcd594a29a..a0c2d1b1ab78 100644 --- a/UPGRADING +++ b/UPGRADING @@ -72,6 +72,14 @@ PHP 8.7 UPGRADE NOTES to 16 by default. Set the stream context option max_filter_count to change this. +- Streams: + . php://temp streams (and therefore SplTempFileObject) no longer report EOF + as soon as the last byte has been buffered. EOF is now only reported once a + read hits the end of the data, consistent with php://memory and plain + files. As a result, loops such as `while (!feof($fp)) { fgets($fp); }` or + iterating an SplTempFileObject perform one additional iteration at the + end, exactly as they already did for php://memory and plain files. + ======================================== 2. New Features ======================================== diff --git a/ext/spl/tests/SplFileObject/SplTempFileObject_iteration_eof.phpt b/ext/spl/tests/SplFileObject/SplTempFileObject_iteration_eof.phpt new file mode 100644 index 000000000000..adc0161cb8af --- /dev/null +++ b/ext/spl/tests/SplFileObject/SplTempFileObject_iteration_eof.phpt @@ -0,0 +1,43 @@ +--TEST-- +SplTempFileObject iterates like SplFileObject at end of file +--FILE-- + new SplFileObject($path, 'w+'), + 'php://memory' => new SplFileObject('php://memory', 'w+'), + 'SplTempFileObject' => new SplTempFileObject(), + 'SplTempFileObject(0)' => new SplTempFileObject(0), +]; + +foreach ($files as $name => $file) { + $file->fwrite("a\nb\nc\n"); + $file->rewind(); + + $lines = []; + foreach ($file as $key => $line) { + $lines[] = $key . '=' . json_encode($line); + } + echo $name, ' foreach: ', implode(' ', $lines), "\n"; + + $file->rewind(); + $lines = []; + while (!$file->eof()) { + $lines[] = json_encode($file->fgets()); + } + echo $name, ' fgets: ', implode(' ', $lines), "\n"; +} +?> +--CLEAN-- + +--EXPECT-- +SplFileObject foreach: 0="a\n" 1="b\n" 2="c\n" 3=false +SplFileObject fgets: "a\n" "b\n" "c\n" "" +php://memory foreach: 0="a\n" 1="b\n" 2="c\n" 3=false +php://memory fgets: "a\n" "b\n" "c\n" "" +SplTempFileObject foreach: 0="a\n" 1="b\n" 2="c\n" 3=false +SplTempFileObject fgets: "a\n" "b\n" "c\n" "" +SplTempFileObject(0) foreach: 0="a\n" 1="b\n" 2="c\n" 3=false +SplTempFileObject(0) fgets: "a\n" "b\n" "c\n" "" diff --git a/ext/spl/tests/SplFileObject/gh8273.phpt b/ext/spl/tests/SplFileObject/gh8273.phpt index 87931541653b..c46085bfac6c 100644 --- a/ext/spl/tests/SplFileObject/gh8273.phpt +++ b/ext/spl/tests/SplFileObject/gh8273.phpt @@ -22,3 +22,4 @@ while ($file->valid()) { 2: line 2 3: line 3 4: line 4 +5: diff --git a/ext/standard/tests/filters/bug79984.phpt b/ext/standard/tests/filters/bug79984.phpt index f919e07e0773..d7de084bd0f5 100644 --- a/ext/standard/tests/filters/bug79984.phpt +++ b/ext/standard/tests/filters/bug79984.phpt @@ -52,6 +52,7 @@ fclose($f2); --EXPECT-- filter onCreate filtered 8192 bytes. -filtered 128 bytes and closing. Stream has reached end-of-file. +filtered 128 bytes. +filtered 0 bytes and closing. int(8320) filter onClose diff --git a/ext/standard/tests/streams/temp_stream_eof.phpt b/ext/standard/tests/streams/temp_stream_eof.phpt new file mode 100644 index 000000000000..d38b22c8c432 --- /dev/null +++ b/ext/standard/tests/streams/temp_stream_eof.phpt @@ -0,0 +1,21 @@ +--TEST-- +php://temp must not report EOF before a read hits the end of the data +--FILE-- + +--EXPECT-- +php://memory: "a\n" "b\n" "c\n" false(EOF) +php://temp: "a\n" "b\n" "c\n" false(EOF) +php://temp/maxmemory:2: "a\n" "b\n" "c\n" false(EOF) diff --git a/main/streams/memory.c b/main/streams/memory.c index c43ddca579b6..4d3ae9089071 100644 --- a/main/streams/memory.c +++ b/main/streams/memory.c @@ -395,7 +395,7 @@ static ssize_t php_stream_temp_write(php_stream *stream, const char *buf, size_t static ssize_t php_stream_temp_read(php_stream *stream, char *buf, size_t count) { php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract; - size_t got; + ssize_t got; assert(ts != NULL); @@ -405,7 +405,10 @@ static ssize_t php_stream_temp_read(php_stream *stream, char *buf, size_t count) got = php_stream_read(ts->innerstream, buf, count); - stream->eof = ts->innerstream->eof; + /* The inner stream is read greedily and may already hit EOF while still + * returning data. Only propagate EOF once no more data is available, so that + * php://temp reports EOF at the same point as php://memory and plain files. */ + stream->eof = got <= 0 && ts->innerstream->eof; return got; }