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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
35 - 13 = ?
Subscribe to this entry?

 
 [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-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 07:01:27 2024 UTC