php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79060 fgets errno=22
Submitted: 2020-01-03 13:18 UTC Modified: 2020-01-03 17:57 UTC
From: namiltd at yahoo dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 7.4.1 OS: Debian GNU/Linux 9
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: namiltd at yahoo dot com
New email:
PHP Version: OS:

 

 [2020-01-03 13:18 UTC] namiltd at yahoo dot com
Description:
------------
Unexpected notice: Notice: fgets(): read of 8192 bytes failed with errno=22  Invalid argument in /home/www/test.php on line 3

Interestingly, the buffer is 2048 but indicates 8192

Test script:
---------------
<?php
$file = fopen("/sys/class/net/lo/speed","r");
echo ">".fgets($file, 2048)."<";
fclose($file);
?>

Expected result:
----------------
><

Actual result:
--------------
Notice: fgets(): read of 8192 bytes failed with errno=22  Invalid argument in /home/www/test.php on line 3
><

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-01-03 13:24 UTC] namiltd at yahoo dot com
-: namiltd at yahooc dot om +: namiltd at yahoo dot com
 [2020-01-03 13:24 UTC] namiltd at yahoo dot com
E-mail change
 [2020-01-03 14:43 UTC] nikic@php.net
-Status: Open +Status: Feedback
 [2020-01-03 14:43 UTC] nikic@php.net
What is the bug here? The fgets() fails because the "file" is not readable. Since PHP 7.4, file system read/write failures result in a notice and false return value (this is noted in the upgrading notes).
 [2020-01-03 15:20 UTC] namiltd at yahoo dot com
-Status: Feedback +Status: Open
 [2020-01-03 15:20 UTC] namiltd at yahoo dot com
But is_readable("/sys/class/net/lo/speed") returns true

Try:
<?php
$fname = "/sys/class/net/lo/speed";
$file = fopen($fname, "r");
if (is_readable($fname)) {
    echo ">".fgets($file, 2048)."<";
} else {
    echo "File is not readable";
}
fclose($file);
 [2020-01-03 15:42 UTC] nikic@php.net
-Status: Open +Status: Feedback
 [2020-01-03 15:42 UTC] nikic@php.net
is_readable() tells you whether you have "r" permissions on the path (which you clearly do, as the fopen is successful). It does not tell you whether actually reading from the FD will be successful. (A simple example is a directory -- on Linux these will fopen fine, but fail reading with errno 21.)

The behavior of /sys files is determined by the kernel and/or device drivers. This particular file happens to reject reads with errno 22.

That's just how things are. If you try to "cat /sys/class/net/lo/speed" you'll get exactly the same result.
 [2020-01-03 15:50 UTC] namiltd at yahoo dot com
-Status: Feedback +Status: Closed
 [2020-01-03 15:50 UTC] namiltd at yahoo dot com
Everything clear, thank you for the explanation.
 [2020-01-03 17:57 UTC] cmb@php.net
-Status: Closed +Status: Not a bug
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Tue Aug 16 13:05:43 2022 UTC