From:             php at webdevelopers dot cz
Operating system: Linux 2.6.6 (gcc 3.3.4)
PHP version:      5.0.2
PHP Bug Type:     Filesystem function related
Bug description:  invalid feof() results due to Zend's caching of user stream 
wrappers

Description:
------------
PHP caches the userarea wrappers - it results for example
to WRAPPER::stream_read(8192) call by PHP even if the user issued
fread($f, 10);
If the file is for example 20 bytes long then user gets EOF immediately
after
first fread($f, 10) because PHP cached it by calling
WRAPPER::stream_read(8192) instead!

Example:
(file size is 20 bytes)
user: $f=fopen(..., 'r');
zend: stream_open(...);
zend: stream_read(8192); // And we immediately get EOF in my example (8192
> 20)
user: fread($f, 10); // Reads first 10 bytes
user: feof($f);
zend: stream_eof(...); // Result is TRUE thus when feof() used in cyclus
it does not iterate even if there are 10 bytes more in this example.


Reproduce code:
---------------
stream_wrapper_register('xyz', 'MyStream') or die('Failed to register X
protocol.');
test('xyz://me.txt');
test('./test_me2.txt');
function test($fileName) {
  echo "TEST: $fileName\n";
  $f=fopen($fileName, 'w+'); fwrite($f, '123456789'); fclose($f);
  $f=fopen($fileName, 'r');
  while(!feof($f)) { echo "[".fread($f, 5)."]\n";}
  fclose($f);
}
class MyStream {
  private $f;
  function stream_open($path, $mode, $options, &$opened_path) {
    $path=str_replace('xyz://', './test_', $path);
    return (bool) $this->f=fopen($path, $mode);
  }
  function stream_read($count) {return fread($this->f, $count);}
  function stream_write($data) {return fwrite($this->f, $data);}
  function stream_tell() {return ftell($this->f);}
  function stream_eof() {return feof($this->f);}
  function stream_seek($offset, $whence) {return fseek($this->f, $offset,
$whence);}
  function stream_close() {return fclose($this->f);}
}


Expected result:
----------------
TEST: xyz://me.txt
[12345]
[6789]
TEST: ./test_me2.txt
[12345]
[6789]


Actual result:
--------------
TEST: xyz://me.txt
TEST: ./test_me2.txt
[12345]
[6789]


-- 
Edit bug report at http://bugs.php.net/?id=31085&edit=1
-- 
Try a CVS snapshot (php4):   http://bugs.php.net/fix.php?id=31085&r=trysnapshot4
Try a CVS snapshot (php5.0): 
http://bugs.php.net/fix.php?id=31085&r=trysnapshot50
Try a CVS snapshot (php5.1): 
http://bugs.php.net/fix.php?id=31085&r=trysnapshot51
Fixed in CVS:                http://bugs.php.net/fix.php?id=31085&r=fixedcvs
Fixed in release:            http://bugs.php.net/fix.php?id=31085&r=alreadyfixed
Need backtrace:              http://bugs.php.net/fix.php?id=31085&r=needtrace
Need Reproduce Script:       http://bugs.php.net/fix.php?id=31085&r=needscript
Try newer version:           http://bugs.php.net/fix.php?id=31085&r=oldversion
Not developer issue:         http://bugs.php.net/fix.php?id=31085&r=support
Expected behavior:           http://bugs.php.net/fix.php?id=31085&r=notwrong
Not enough info:             
http://bugs.php.net/fix.php?id=31085&r=notenoughinfo
Submitted twice:             
http://bugs.php.net/fix.php?id=31085&r=submittedtwice
register_globals:            http://bugs.php.net/fix.php?id=31085&r=globals
PHP 3 support discontinued:  http://bugs.php.net/fix.php?id=31085&r=php3
Daylight Savings:            http://bugs.php.net/fix.php?id=31085&r=dst
IIS Stability:               http://bugs.php.net/fix.php?id=31085&r=isapi
Install GNU Sed:             http://bugs.php.net/fix.php?id=31085&r=gnused
Floating point limitations:  http://bugs.php.net/fix.php?id=31085&r=float
MySQL Configuration Error:   http://bugs.php.net/fix.php?id=31085&r=mysqlcfg

Reply via email to