Skip to content

Different results for seek() on SplFileObject and SplTempFileObject #8563

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gabrielpl opened this issue May 16, 2022 · 1 comment
Closed

Comments

@gabrielpl
Copy link

Description

The following code:

<?php

declare(strict_types=1);

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

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

// reset
$file_01->rewind();
$file_02->rewind();

// seek
$file_01->seek(INDEX);
$file_02->seek(INDEX);

// show results
echo 'file_01: ' . $file_01->key(), PHP_EOL;
echo 'file_02: ' . $file_02->key(), PHP_EOL;

Resulted in this output:

file_01: 4
file_02: 5

But I expected this output instead:

file_01: 5
file_02: 5

PHP Version

PHP 8.1.6

Operating System

Windows 11

@MorganLOCode
Copy link

MorganLOCode commented May 19, 2022

Appending to the test code above:

if($file_01->eof()) echo "Temp file at EOF", PHP_EOL;
if($file_02->eof()) echo "Real file at EOF", PHP_EOL;

try {
	echo "[",$file_01->fgets(),"]", PHP_EOL;
} catch(RuntimeException) {
	echo "Failed to read from temp file", PHP_EOL;
}
try {
	echo "[",$file_02->fgets(),"]", PHP_EOL;
} catch(RuntimeException) {
	echo "Failed to read from real file", PHP_EOL;
}

When INDEX is between 0 and 4 everything works as expected.
When INDEX is greater than 5 then everything works as expected (files are at EOF, reads fail), but the temp file's current line number is down as reported.
When INDEX is equal to 5, both files are on line 5, but the temp file is at EOF and fails to read, while the real file reckons there is still more to go, reading and outputting an empty string (the one following the final PHP_EOL in the file).

...But the temp file does have the same contents as the real file.

Girgias added a commit to Girgias/php-src that referenced this issue May 27, 2022
With memory streams if we get a NULL buffer we must not instantiate an empty line
Girgias added a commit to Girgias/php-src that referenced this issue May 27, 2022
With memory streams if we get a NULL buffer we must not instantiate an empty line
Girgias added a commit to Girgias/php-src that referenced this issue May 29, 2022
With memory streams if we get a NULL buffer we must not instantiate an empty line
Girgias added a commit to Girgias/php-src that referenced this issue May 29, 2022
With memory streams if we get a NULL buffer we must not instantiate an empty line
Girgias added a commit to Girgias/php-src that referenced this issue Jun 8, 2022
With memory streams if we get a NULL buffer we must not instantiate an empty line
Girgias added a commit that referenced this issue Jun 20, 2022
* PHP-8.0:
  Fix GH-8563  Different results for seek() on SplFileObject and SplTempFileObject
Girgias added a commit that referenced this issue Jun 20, 2022
* PHP-8.1:
  Fix GH-8563  Different results for seek() on SplFileObject and SplTempFileObject
Girgias added a commit that referenced this issue Jul 5, 2022
… SplTempFileObject"

Although the fix is partially correct it also breaks long standing behaviour which has been produced since PHP 5.3.

This reverts commit 6f87a5c.
Girgias added a commit that referenced this issue Jul 5, 2022
* PHP-8.0:
  Revert "Fix GH-8563  Different results for seek() on SplFileObject and SplTempFileObject"
Girgias added a commit that referenced this issue Jul 5, 2022
* PHP-8.1:
  Revert "Fix GH-8563  Different results for seek() on SplFileObject and SplTempFileObject"
  Revert "Update FreeBSD CI image."
ramsey pushed a commit that referenced this issue Jul 5, 2022
… SplTempFileObject"

Although the fix is partially correct it also breaks long standing behaviour which has been produced since PHP 5.3.

This reverts commit 6f87a5c.

This was cherry-picked to PHP-8.1.8 from 79a2832
carusogabriel pushed a commit that referenced this issue Jul 6, 2022
… SplTempFileObject"

Although the fix is partially correct it also breaks long standing behaviour which has been produced since PHP 5.3.

This reverts commit 6f87a5c.
Girgias added a commit to Girgias/php-src that referenced this issue Dec 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants