php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68481 feof true on php://temp one byte early
Submitted: 2014-11-22 23:14 UTC Modified: 2015-01-24 18:47 UTC
Votes:3
Avg. Score:3.3 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: tim_siebels_aurich at yahoo dot de Assigned:
Status: Closed Package: Streams related
PHP Version: 5.6.3 OS: Tested on Ubuntu
Private report: No CVE-ID: None
 [2014-11-22 23:14 UTC] tim_siebels_aurich at yahoo dot de
Description:
------------
To get `feof` return true you need to read `length+1` bytes you wrote. Or in general it returns true after you tried to read beyond the last byte.

the `php://temp` (and php://memory too) stream however returns true after `length` bytes read.




Test script:
---------------
<?php
// Test File Stream
$file = tempnam('/tmp', 'feoftest');
$h = fopen("php://temp", 'r+');
fwrite($h, "bug");
fseek($h, 0);
fread($h, 3);
var_dump(feof($h));

// Test Temp Stream
$h = fopen("php://temp", 'r+');
fwrite($h, "bug");
fseek($h, 0);
fread($h, 3);
var_dump(feof($h));


Expected result:
----------------
bool(false)
bool(false)

Actual result:
--------------
bool(false)
bool(true)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-12-13 18:07 UTC] tim_siebels_aurich at yahoo dot de
The test script is not correct obviously. The first fopen is supposed to open the file created in the preceding line.
 [2014-12-13 18:18 UTC] sjaillet at gmail dot com
Maybe relevant, PHP & HHVM behave differently on `feof()`.

When `length` bytes are read, `feof()` returns `true` on PHP while HHVM still returns `false`. See code below:

```php
$stream = fopen('php://temp', 'r+');
fwrite($stream, "HelloWorld");
rewind($stream);
$result = fread($stream, 10);

echo $result . "\n";        // HelloWorld
var_dump(feof($stream));    // with PHP: `true`, with  HHVM: `false`
```

They both returns false after reading `length + 1` bytes.

Would be great to have something consistent.
 [2014-12-15 17:27 UTC] php at mcq8 dot be
I recently changed it for memory https://github.com/php/php-src/pull/936.
It would be better if all the streams behave the same way.
 [2015-01-03 12:48 UTC] yvan dot burrie at hotmail dot com
<?php

// yep, it's a bug!!!

$f = tmpfile();

fwrite($f, 'crap' ); // 4 bytes

rewind($f);

$i=1;
while( !feof($f) )
{
fread($f,1);
echo "$i\n";
$i++;
}

/*
outputs:

1
2
3
4
5

why the hell 5 bytes? should normally output 4 bytes:

1
2
3
4

*/

?>
 [2015-01-06 06:11 UTC] yvan dot burrie at hotmail dot com
Wait!!! This is not a bug! PHP imitates C++ feof() function from the STDIO library.

Read this: http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong
 [2015-01-06 14:09 UTC] tim_siebels_aurich at yahoo dot de
I am complaining that feof returns TRUE one byte early in php://temp.
When you read the description of the bug I explain how C handles it, and how I would expect it to be.
 [2015-01-24 12:47 UTC] php at mcq8 dot be
Can you test again in 5.6.5 or 5.5.21?
It should now behave like you expected it to be.
 [2015-01-24 18:47 UTC] tim_siebels_aurich at yahoo dot de
-Status: Open +Status: Closed
 [2015-01-24 18:47 UTC] tim_siebels_aurich at yahoo dot de
Love it :)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 05:01:28 2024 UTC