From: salsi at icosaedro dot it
Operating system: Slackware 14.1
PHP version: master-Git-2016-01-15 (Git)
Package: Streams related
Bug Type: Bug
Bug description:fread() does not detect file access error
Description:
------------
Possibly related: bug #71263 fread() does not detects decoding errors
from filter bzip2.decompressed (https://bugs.php.net/bug.php?id=71263).
Trying to read an unreadable file, fread() does not to detect the
problem and simply returns the empty string; no error nor exception
whatsover.
The simplest way to simulate an unreadable file on Linux is to read
/proc/self/mem, a file that can be succesfully openend but cannot be
read from the very beginning (credits for this trick:
http://unix.stackexchange.com/a/6302):
$ cat /proc/self/mem
cat: /proc/self/mem: Input/output error
A simple test made with gcc C confirms that fopen() succeeds while
fread() gives input/output error.
Under PHP, instead, no errors are detected, and fread() simply returns
the empty string, as the following test script proves:
Test script:
---------------
<?php
echo "PHP version: ", PHP_VERSION, "\n";
// set safe test environment:
error_reporting(-1);
ini_set("track_errors", "1");
// maps errors to ErrorException:
function my_error_handler($errno, $message) {
throw new ErrorException($message);
}
set_error_handler("my_error_handler");
$f = fopen("/proc/self/mem", "r");
do {
$bytes = fread($f, 1000);
echo "bytes=";
var_dump($bytes);
} while (!feof($f));
fclose($f);
// --> bytes=string(0) "", no error whatsoever
echo "Just testing if error detection is still on:\n";
$f = fopen("this file does not exist!", "rb");
?>
Expected result:
----------------
PHP version: 7.1.0-dev
(exception on the first fread() telling the file is unreadable)
Actual result:
--------------
PHP version: 7.1.0-dev
bytes=string(0) ""
Just testing if error detection is still on:
PHP Fatal error: Uncaught ErrorException: fopen(this file does not
exist!): failed to open stream: No such file or directory in
/home/salsi/src/phplint/bug-fread-unreadable-file.php:9
Stack trace:
#0 [internal function]: my_error_handler(2, 'fopen(this file...',
'/home/salsi/src...', 23, Array)
#1 /home/salsi/src/phplint/bug-fread-unreadable-file.php(23):
fopen('this file does ...', 'rb')
#2 {main}
thrown in /home/salsi/src/phplint/bug-fread-unreadable-file.php on
line 9
Fatal error: Uncaught ErrorException: fopen(this file does not exist!):
failed to open stream: No such file or directory in
/home/salsi/src/phplint/bug-fread-unreadable-file.php:9
Stack trace:
#0 [internal function]: my_error_handler(2, 'fopen(this file...',
'/home/salsi/src...', 23, Array)
#1 /home/salsi/src/phplint/bug-fread-unreadable-file.php(23):
fopen('this file does ...', 'rb')
#2 {main}
thrown in /home/salsi/src/phplint/bug-fread-unreadable-file.php on
line 9
--
Edit bug report at https://bugs.php.net/bug.php?id=71384&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=71384&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=71384&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=71384&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=71384&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=71384&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=71384&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=71384&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=71384&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=71384&r=support
Expected behavior: https://bugs.php.net/fix.php?id=71384&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=71384&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=71384&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=71384&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=71384&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=71384&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=71384&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=71384&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=71384&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=71384&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=71384&r=mysqlcfg