Files
Ilia Alshanetsky 9a530a1c5e Fix GH-21742: SplFileObject::fgets() throws at EOF in eof/fgets loop (#21853)
The SplFileObject iterator-desync fix in 08dad09702 made
spl_filesystem_file_read_ex throw "Cannot read from file" on the
NULL-buffer path. SplFileObject::fgets() now throws inside the
documented while (!$spl->eof()) $spl->fgets() idiom, because eof()
returns false until a read attempt returns zero bytes.

Keep the stricter semantics for next(), seek(), current(), fscanf().
Narrow fgets() to silent=true and return empty string on FAILURE,
restoring the PHP-8.5 contract.

Fixes GH-21742
2026-04-23 17:55:15 +01:00

36 lines
681 B
PHP

--TEST--
GH-21742 (SplFileObject::fgets() throws at EOF in while (!$spl->eof()) loop)
--FILE--
<?php
$file = tempnam(sys_get_temp_dir(), 'spl');
file_put_contents($file, "Line 0\nLine 1\nLine 2\nLine 3\nLine 4\n");
$spl = new SplFileObject($file, 'r');
while (!$spl->eof()) {
echo $spl->fgets();
}
echo "clean exit\n";
$empty = tempnam(sys_get_temp_dir(), 'spl');
file_put_contents($empty, '');
$spl2 = new SplFileObject($empty, 'r');
$iter = 0;
while (!$spl2->eof()) {
$iter++;
$spl2->fgets();
if ($iter > 3) break;
}
echo "empty-file iters=$iter\n";
unlink($file);
unlink($empty);
?>
--EXPECT--
Line 0
Line 1
Line 2
Line 3
Line 4
clean exit
empty-file iters=1