Bug #71384 [Com]: fread() does not detect file access error

From: Date: Sat, 16 Jan 2016 13:20:08 +0000
Subject: Bug #71384 [Com]: fread() does not detect file access error
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-198714@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=71384&edit=1 ID: 71384 Comment by: danack@php.net Reported by: salsi at icosaedro dot it Summary: fread() does not detect file access error Status: Open Type: Bug Package: Streams related Operating System: Slackware 14.1 PHP Version: master-Git-2016-01-15 (Git) Block user comment: N Private report: N New Comment: To increase the likelihood of this being fixed, would it be possible for you to attach the C code that you tested with, to show the exact error conditions. (I'm not saying that will guarantee that it will be fixed, it just makes it a bit more likely.) Previous Comments: ------------------------------------------------------------------------ [2016-01-15 23:23:19] salsi at icosaedro dot it 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 this bug report at https://bugs.php.net/bug.php?id=71384&edit=1

« previous php.bugs (#198714) next »