From:             polone at townnews dot com
Operating system: Any
PHP version:      4.3.3
PHP Bug Type:     *PDF functions
Bug description:  _php_stream_write() gets stuck in infinite loop on any error to 
send()

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.

-- 
Edit bug report at http://bugs.php.net/?id=25316&edit=1
-- 
Try a CVS snapshot (php4):  http://bugs.php.net/fix.php?id=25316&r=trysnapshot4
Try a CVS snapshot (php5):  http://bugs.php.net/fix.php?id=25316&r=trysnapshot5
Fixed in CVS:               http://bugs.php.net/fix.php?id=25316&r=fixedcvs
Fixed in release:           http://bugs.php.net/fix.php?id=25316&r=alreadyfixed
Need backtrace:             http://bugs.php.net/fix.php?id=25316&r=needtrace
Try newer version:          http://bugs.php.net/fix.php?id=25316&r=oldversion
Not developer issue:        http://bugs.php.net/fix.php?id=25316&r=support
Expected behavior:          http://bugs.php.net/fix.php?id=25316&r=notwrong
Not enough info:            http://bugs.php.net/fix.php?id=25316&r=notenoughinfo
Submitted twice:            http://bugs.php.net/fix.php?id=25316&r=submittedtwice
register_globals:           http://bugs.php.net/fix.php?id=25316&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=25316&r=php3
Daylight Savings:           http://bugs.php.net/fix.php?id=25316&r=dst
IIS Stability:              http://bugs.php.net/fix.php?id=25316&r=isapi
Install GNU Sed:            http://bugs.php.net/fix.php?id=25316&r=gnused

Reply via email to