1
0
mirror of https://github.com/php/php-src.git synced 2026-04-24 08:28:26 +02:00
Files
Ilia Alshanetsky 08dad09702 Fix GH-8561, GH-8562, GH-8563, GH-8564: SplFileObject iterator desync (#21679)
* Fix GH-8562: SplFileObject::current() returns wrong value after next()

SplFileObject::next() without READ_AHEAD cleared the cached line and
incremented current_line_num but didn't advance the stream. When called
without a preceding current() (e.g. rewind() then next()), the stream
position stayed put, so the subsequent current() read stale data.

Read a line to advance the stream when next() is called with no cached
line.

Closes GH-8562

* Fix GH-8561: SplFileObject key()/current() desync after fgets()

fgets() read a line into the cache and incremented the line counter,
but left the cached line in place. The subsequent current() returned
the stale cached value instead of reading the next line from the
stream's actual position.

Clear the cached line after fgets() copies it to the return value.
This forces current() to re-read from the stream, which has already
advanced past the fgets'd line.

Closes GH-8561

* Fix GH-8563, GH-8564: SplFileObject EOF handling for seek() and next()

spl_filesystem_file_read_ex() treated a NULL buffer from
php_stream_get_line as a successful read of an empty line, creating a
phantom cached line at EOF. This caused seek() to give inconsistent
results between SplFileObject and SplTempFileObject (GH-8563), and
next() to increment the line counter indefinitely past EOF (GH-8564).

Return FAILURE when php_stream_get_line returns NULL, matching the
behavior of the existing php_stream_eof check. In seek(), break out
of the loop on EOF instead of returning, so the post-loop cleanup
runs consistently. In next(), return early at EOF without incrementing.
Make __toString() return empty string at EOF instead of throwing.

Closes GH-8563
Closes GH-8564

* Refine fgets() to reuse cached line when present

When current() reads a line into the cache without advancing line_num,
a subsequent fgets() would re-read the stream and return the next line,
skipping the cached one and leaving key() out of sync with current()
for the rest of the iteration.

Use the cached line if present; otherwise read a fresh line. Either way,
advance line_num by one.
2026-04-13 11:35:20 +01:00

362 B