Home » File System » File_SearchReplace » Bug #8103
Length parameter must be greater than 0 on empty files
Details
| Submitted | 2006-07-02 23:55 UTC |
|---|---|
| From | roychri at php dot net |
| Assigned | techtonik |
| Status | Closed |
| Package | File_SearchReplace |
| PHP Version | 4.3.11 |
| OS | Mandrake Linux release 8.2 (Blue |
| Roadmaps | (Not assigned) |
Comments
[2006-07-02 23:55 UTC] roychri at php dot net
Description:
------------
When using the preg search function on files which are empty, I get an error message:
fread(): Length parameter must be greater than 0.
The pregSearch() function is using fopen and filesize and fread to open the file to search in. However, since the file is empty, fread gets 0 as second parameter and generate that warning.
I do not think the function pregSearch() should open and search an empty file.
I was able to get rid of the error by making this change (in patch format):
--- /usr/local/lib/php/File/SearchReplace.php~ Sun Jul 2 11:31:09 2006
+++ /usr/local/lib/php/File/SearchReplace.php Sun Jul 2 16:52:16 2006
@@ -367,7 +367,12 @@
clearstatcache();
- $file = fread($fp = fopen($filename, 'r'), filesize($filename)); fclose($fp);
+ $size = filesize($filename);
+ if ( empty($size) ) {
+ return FALSE;
+ }
+ $file = fread($fp = fopen($filename, 'r'), $size);
+ fclose($fp);
$local_find = array_values((array) $this->find);
$local_replace = (is_array($this->replace)) ? array_values($this->replace) : $this->replace;
Let me know if you have any questions.
Test script:
---------------
$files = Array("/tmp/test/nonemptyfile",
"/tmp/test/emptyfile");
$from = "/foo/";
$to = "bar";
$snr = new File_SearchReplace( $from, $to, $files);
$snr->setSearchFunction("preg");
$snr->doSearch();
print "DONE\n";
Expected result:
----------------
DONE
Actual result:
--------------
Warning: fread(): Length parameter must be greater than 0. in /usr/local/lib/php/File/SearchReplace.php on line 370
DONE