From:             admin at ifyouwantblood dot de
Operating system: Windows XP SP2
PHP version:      5.2.4
PHP Bug Type:     Class/Object related
Bug description:  file operations in destructor cause weird results

Description:
------------
Calling clearstatcache() in destructor causes some very weird stuff.
In the code below a file is created / truncated in the constructor. If you
want to read the same file with fread in the destructor while it's still
empty i'm expecting fread to print an error which it does. However if you
write something to the file AFTER reading it and run the script twice fread
returns what's written before. This should not happen, because before
reading the file, we truncate it. Moreover if you continue calling the
script, the string that fread returns contains all the writing operations
done before.

The strangest thing is, that it looks like that this behavior got
something to do with the filename "datei.txt" (is German, means file.txt).
If you change the filename, the fopen($this->filename,'r+') after
clearstatcache() fails with the error "file does not exists".

If you explicite truncate the file before reading it with ftruncate() no
error is printed.

If you remove clearstatcache() in the destructor it works as expected.

This also appears on PHP 5.2.3


Reproduce code:
---------------
<pre>
<?php

#call this script more than once!

error_reporting(E_ALL|E_NOTICE);

$test=new test;

class test
{
        private $filename='datei.txt';
        
        public function __construct()
        {
                # open and truncate
                $file=fopen($this->filename,'w');
                fclose($file);

                echo 'filesize after truncate: '.filesize($this->filename)."\n";
        }

        public function __destruct()
        {
                echo 'Destructing test'."\n";
                echo 'filesize before clearstatcache: 
'.filesize($this->filename)."\n";

                clearstatcache();

                echo 'filesize after clearstatcache: 
'.filesize($this->filename)."\n";

                $file=fopen($this->filename,'r+');
                if($file)
                {
                        echo 'Reading file: 
'.fread($file,filesize($this->filename));
                        fputs($file,'aasadf');
                }
        }
}

?>

Expected result:
----------------
filesize after truncate: 0
Destructing test
filesize before clearstatcache: 0
filesize after clearstatcache: 0

Warning: fread() [function.fread]: Length parameter must be greater than 0
in G:\php\htdocs\cms\1jailbreak\admin\constructor.php on line 34

Reading file: 

Actual result:
--------------
filesize after truncate: 0
Destructing test
filesize before clearstatcache: 0
filesize after clearstatcache: 6
Reading file: aasadf

-----

on repeated calling:

filesize after truncate: 0
Destructing test
filesize before clearstatcache: 0
filesize after clearstatcache: 12
Reading file: aasadfaasadf

-----

filesize after truncate: 0
Destructing test
filesize before clearstatcache: 0
filesize after clearstatcache: 18
Reading file: aasadfaasadfaasadf

and so on...

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

Reply via email to