php-src icon indicating copy to clipboard operation
php-src copied to clipboard

SplFileObject: next() moves to inexistent indexes

Open gabrielpl opened this issue 4 years ago • 10 comments

Description

The following code:

<?php

declare(strict_types=1);

const LINES = 5;
const INDEX = 10;
        
$file = new SplFileObject('/tmp/test.txt', 'w+');

// write to files
for ($i = 0; $i < LINES; $i++) {
    $file->fwrite("line {$i}" . PHP_EOL);
}

// behaviour of next()
$file->rewind();
for ($i = 0; $i < INDEX; $file->next(), $i++);
echo 'next(): ', $file->key(), PHP_EOL;

// behaviour of seek()
$file->rewind();
$file->seek(INDEX);
echo 'seek(): ', $file->key(), PHP_EOL;

Resulted in this output:

next(): 10
seek(): 5

But I expected this output instead:

next(): 5
seek(): 5

PHP Version

PHP 8.1.6

Operating System

Windows 11

gabrielpl avatar May 16 '22 04:05 gabrielpl

Note:

The documentation of next() is incomplete and does not define what should happen in this scenario.

May be:

  1. When key() is in the last line, next() does nothing.
  2. When key() is in the last line, next() moves to EOF. When eof() is true, next() throws an exception.

In my opinion, option 2 is the most correct. Either way, the exact behaviour of next() must be explicit stated in the docs

gabrielpl avatar May 16 '22 04:05 gabrielpl

https://github.com/php/php-src/blob/6beee1a5bb04d399518e4e4d94a4b82d367161fd/ext/spl/spl_directory.c#L2237-L2250

So, unless ::READ_AHEAD is set, the method just increments the internal line number without checking that the line actually exists. SplFileObject::key() just returns that line number.

This also explains the behavior reported as #8562.

cmb69 avatar May 16 '22 14:05 cmb69

Looking at splfileobject.inc (which is removed as of PHP 7.4.0), confirms the issue: https://github.com/php/php-src/blob/7d92153182f86722f83963d6fb054609c40bed8d/ext/spl/internal/splfileobject.inc#L306-L311 This is fundamentally flawed. ::next() needs to read a line, or at least set a marker that a line has to be read/skipped before ::next() is called again. Apparently, the Iterator interface has been implemented with only Traversable functionality in mind. ::READ_AHEAD has been implemeted somewhat later, probably to somehow cater to this, but I'm having a hard time to claim that multiple consecutive ::next() calls without the ::READ_AHEAD flag being set are simply not supported, or that the behavior reported in this ticket makes any sense.

cmb69 avatar May 18 '22 12:05 cmb69

I think the only way to fix it is to make enable the READ_AHEAD flag by default, people who want the prior behaviour can always remove it.

But this can only be fixed in 8.2

Girgias avatar May 27 '22 10:05 Girgias

I think the only way to fix it is to make enable the READ_AHEAD flag by default, […]

Frankly, I think that disabling READ_AHEAD should be deprecated. Unless I'm missing something, there is no point in not reading ahead, since sooner or later it would have to be done anyway.

cmb69 avatar May 27 '22 11:05 cmb69

That's also fair, but READ_AHEAD is not the default.

Girgias avatar May 27 '22 11:05 Girgias

Yes, I know that. :)

cmb69 avatar May 27 '22 11:05 cmb69

And I don't think we can make READ_AHEAD the default as it seems to break a hell of a lot of stuff

Girgias avatar Jun 23 '22 14:06 Girgias

Not setting READ_AHEAD could be deprecated; of course, that would require lots of code to be adapted, but it might be the only reasonable thing to do in the long run (unless there are plans to deprecate SplFileObject altogether).

cmb69 avatar Jun 23 '22 15:06 cmb69

READ_AHEAD has other side-effects like producing a phantom empty value on an empty file , so I don't think that's a reasonable replacement at this time.

pilif avatar Sep 24 '25 06:09 pilif