php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62773 fgets() is not binary safe
Submitted: 2012-08-07 23:23 UTC Modified: 2012-08-08 02:47 UTC
From: scott at smashcat dot org Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: Irrelevant OS: Linux
Private report: No CVE-ID: None
 [2012-08-07 23:23 UTC] scott at smashcat dot org
Description:
------------
fgets() does not load the binary data for unpack to process. If the fgets() function is replaced with fread() then the example below works as expected. The current documentation claims that fgets() is binary safe, which appears to be incorrect.

Test script:
---------------
<?php

    print "Saving...\n";
    $F=fopen('1.evid','wb');
    fwrite($F,pack('C6',65,66,67,68,69,70));
    fclose($F);
     
    print "Loading...\n";
    $F=fopen('1.evid','rb');
    $d=unpack('C6',fgets($F,6));
    print "Result: ".print_r($d,1)."\n";
    print count($d)."\n";
    fclose($F);


Expected result:
----------------
It should display an array with 6 values, as written to the file.

Actual result:
--------------
No data is read, the array is empty.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-08-08 02:47 UTC] aharvey@php.net
-Status: Open +Status: Not a bug
 [2012-08-08 02:47 UTC] aharvey@php.net
fgets() is binary safe, as the documentation says. However, you've given an explicit length, and to quote the manual:

"Returns a string of up to length - 1 bytes read from the file pointed to by handle. If there is no more data to read in the file pointer, then FALSE is returned."

fgets($F, 6) returns a 5 byte string, which can't be unpacked because unpack() is expecting 6 bytes per the format.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sun Jul 25 13:01:23 2021 UTC