php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71907 fwrite returns 0 instead of FALSE when feof
Submitted: 2016-03-27 15:38 UTC Modified: 2019-09-07 12:25 UTC
Votes:2
Avg. Score:3.5 ± 0.5
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: contact at sshilko dot com Assigned: nikic (profile)
Status: Closed Package: Sockets related
PHP Version: 7.0.4 OS: Ubuntu 14.04.3 LTS
Private report: No CVE-ID: None
 [2016-03-27 15:38 UTC] contact at sshilko dot com
Description:
------------
Hi, there is an issue when detecting broken socket connections:
The behavour is different between 7.0.2 and 7.0.4

According to documentation fwrite should
"fwrite() returns the number of bytes written, or FALSE on error."

But it returns 0 (int) instead of FALSE (bool) and generates NOTICE.


Also feof() never works on 7.0.2 (MacOSX) but works on 7.0.4 on Ubuntu 
             if (feof($fp)) {
                 echo 'FEOF detected';
             }

Test script:
---------------
Open 2 terminals and launch

1# php server.php
2# php client.php

Then press CTRL+C on server.php terminal:


https://gist.github.com/sshilko/443ae40a4d115ca8325b

Expected result:
----------------
Written result bool(FALSE)

Actual result:
--------------
$ php server.php
Dispatching...
RCV: 12325 Message to send...1459092741
RCV: 12325 Message to send...1459092742
RCV: 12325 Message to send...1459092743
RCV: 12325 Message to send...1459092744
RCV: 12325 Message to send...1459092745
^C


$ php client.php
SS result=bool(true)
Written result:int(35)
Written=35
SS result=bool(true)
Written result:int(35)
Written=35
SS result=bool(true)
Written result:int(35)
Written=35
SS result=bool(true)
Written result:int(35)
Written=35
SS result=bool(true)
Written result:int(35)
Written=35
SS result=bool(true)
PHP Notice:  fwrite(): send of 35 bytes failed with errno=32 Broken pipe in /home/ubuntu/client.php on line 18
Written result:int(0)
Array
(
    [timed_out] =>
    [blocked] =>
    [eof] =>
    [stream_type] => tcp_socket/ssl
    [mode] => r+
    [unread_bytes] => 0
    [seekable] =>
)
FEOF detectedWritten=0
SS result=bool(true)
PHP Notice:  fwrite(): send of 35 bytes failed with errno=32 Broken pipe in /home/ubuntu/client.php on line 18
Written result:int(0)
Array
(
    [timed_out] =>
    [blocked] =>
    [eof] => 1
    [stream_type] => tcp_socket/ssl
    [mode] => r+
    [unread_bytes] => 0
    [seekable] =>
)
FEOF detected^C


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-03-27 15:40 UTC] contact at sshilko dot com
Additionnaly stream_get_meta_data()['eof'] != feof()
 [2016-03-27 16:12 UTC] contact at sshilko dot com
Also the function suggested in manual (documentation)

function fwrite_stream($fp, $string) {
    for ($written = 0; $written < strlen($string); $written += $fwrite) {
        $fwrite = fwrite($fp, substr($string, $written));
        if ($fwrite === false) {
            return $written;
        }
    }
    return $written;
}

may infinite-loop, this is also mentioned in comments, 6 YEARS ago by 
nate at frickenate dot com ΒΆ

but bad example still there
 [2018-10-04 16:22 UTC] michael at butlerpc dot net
The provided client.php and server.php reproduce this bug for me; on PHP 7.1.15 and a Scientific Linux 6 based system (RHEL 6 like). If an E_NOTICE is raised during the fwrite, I really think FALSE (bool) should be returned and not int(0).
 [2019-09-07 11:42 UTC] contact at sshilko dot com
Seems like 7.4 is fixing the issue with fread/fwrite finally, they will be consistenly returning false

https://github.com/php/php-src/blob/php-7.4.0RC1/UPGRADING
 [2019-09-07 12:25 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2019-09-07 12:25 UTC] nikic@php.net
Indeed, on 7.4 the output is:

Notice: fwrite(): send of 35 bytes failed with errno=32 Broken pipe in /home/nikic/php-7.4/client.php on line 16
Written result:bool(false)
FAILED TO WRITE
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 12 18:01:26 2024 UTC