php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #25316 _php_stream_write() gets stuck in infinite loop on any error to send()
Submitted: 2003-08-29 13:49 UTC Modified: 2003-09-11 01:06 UTC
Votes:3
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: polone at townnews dot com Assigned: iliaa
Status: Closed Package: Reproducible crash
PHP Version: 4.3.3 OS: *
Private report: No CVE-ID:
 [2003-08-29 13:49 UTC] polone at townnews dot com
Description:
------------
(Please note, this looks like a similiar bug as #22753, but it is not - it's in a function layer much more abstract than the main/network.c bug, this one is in main/streams.c)

A problem that occurs quite often with PHP scripts when remote hosts disconnect applications in PHP using the streams API is infinite looping with SIGPIPE. It appears an early attempt to remedy the situation was to ignore SIGPIPE, but this is not where the problem is. After reviewing the _php_stream_write() code and testing the loop in an error condition of -1, it became obvious why the looping occurs.

The while() loop will never exit if an error occurs in the underlying send() call. This is because it returns a negative value (-1), but the type assigned in _php_stream_write() is size_t for the variable justwrote. For reference, "size_t" IS AN UNSIGNED INT, which means the condition:

if (justwrote > 0) {

   // Buffering code

} else {
   break;
}

will never execute the "else" condition. To fix this, change the following:

size_t didwrite = 0, towrite;
int justwrote;

This bug has been present (as far as I can tell) since PHP 4.3.0. In addition, another change I've made is too main/network.c, in the php_sockop_write() function. Instead of ignoring SIGPIPE as the default handler, it would be better to set:

didwrite = send(sock->socket, buf, count, MSG_NOSIGNAL);

This will still work correctly when SIGPIPE would have been issued as EPIPE is still returned.

Reproduce code:
---------------
<?php

$fp = fsockopen ("localhost", 80);
while(fwrite($fp, "GET /doesntmatter HTTP/1.0\n\n")) {

    sleep(1);

}

?>

Expected result:
----------------
To end eventually. Instead, the script will eventually issue a SIGPIPE and create an infinite loop.

Actual result:
--------------
It never ends.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-08-30 06:02 UTC] sniper@php.net
Wez, fix your stuff. :)

 [2003-09-11 01:06 UTC] iliaa@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at http://snaps.php.net/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Sat Apr 19 22:02:16 2014 UTC