Bug #71907 [Com]: fwrite returns 0 instead of FALSE when feof

From: Date: Thu, 04 Oct 2018 16:22:51 +0000
Subject: Bug #71907 [Com]: fwrite returns 0 instead of FALSE when feof
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-217411@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=71907&edit=1 ID: 71907 Comment by: michael at butlerpc dot net Reported by: contact at sshilko dot com Summary: fwrite returns 0 instead of FALSE when feof Status: Open Type: Bug Package: Sockets related Operating System: Ubuntu 14.04.3 LTS PHP Version: 7.0.4 Block user comment: N Private report: N New Comment: 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). Previous Comments: ------------------------------------------------------------------------ [2016-03-27 16:12:15] 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 ------------------------------------------------------------------------ [2016-03-27 15:40:30] contact at sshilko dot com Additionnaly stream_get_meta_data()['eof'] != feof() ------------------------------------------------------------------------ [2016-03-27 15:38:17] 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 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=71907&edit=1

« previous php.bugs (#217411) next »