php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #67370 Fseek does not return error (-1) on read out of file
Submitted: 2014-06-02 07:47 UTC Modified: 2014-06-02 09:11 UTC
From: roman4e at gmail dot com Assigned:
Status: Not a bug Package: Filesystem function related
PHP Version: 5.5.13 OS: linux mint 15
Private report: No CVE-ID: None
 [2014-06-02 07:47 UTC] roman4e at gmail dot com
Description:
------------
php v.5.5.5 (cli) (built: Mar 17 2014 12:31:03) (DEBUG)

fseek() does not return error (-1) on read out of file, returns 0


Test script:
---------------
$f = fopen("file","r"); // file must be several KBytes size
$page = 0;
while ( !fseek($f,$page,SEEK_SET) )
{
   $page += 1024;
   $str = fread($f,1024);
   if ( $str === false )
      die("error reading block");
}

echo "ok"

Expected result:
----------------
fseek should return -1;


Actual result:
--------------
file size of 76800
result of reading out of file. last parametr is type of data returned by fread
Pageoff = 75776 fseek=0 ftell=75776 read=1024 string
Pageoff = 76800 fseek=0 ftell=76800 read=0 string
Pageoff = 77824 fseek=0 ftell=77824 read=0 string

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-06-02 08:23 UTC] mike@php.net
-Status: Open +Status: Not a bug
 [2014-06-02 08:23 UTC] mike@php.net
One can set the file pointer beyond EOF, this is not a bug.
 [2014-06-02 08:58 UTC] roman4e at gmail dot com
change code:
while ( !fseek($f,$page,SEEK_SET) && !feof($f))

So why in that case !feof($f) does not return true when go out end of file?
 [2014-06-02 09:11 UTC] mike@php.net
Because fseek clears the EOF condition, reverse your conditions.
 [2014-06-02 10:36 UTC] roman4e at gmail dot com
Thank you much!
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 19:01:33 2024 UTC