From:             jwozniak23 at poczta dot onet dot pl
Operating system: Windows XP
PHP version:      4.3.11
PHP Bug Type:     Sockets related
Bug description:  Socket errors cause memory leaks

Description:
------------
It seems that there is a memory leak in sockets extension (at least in
Windows version). It happens when a socket operation causes an error. For
example, in the included code socket_accept returns an error, because it's
non-blocking and there is no new connection on port 1234.
We've done some research in PHP sources and discovered, that memory leak
occurs every time the function php_strerror (in sockets.c) is called. 

We've resolved this bug by releasing error message buffer in this
function.

Previous version:
(php_strerror in sockets.c, line 366)

LPTSTR tmp = NULL;
buf = NULL;
if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | 
 FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
 NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &tmp, 0,
NULL)) {
     SOCKETS_G(strerror_buf) = estrdup(tmp);
     LocalFree(tmp);    
     buf = SOCKETS_G(strerror_buf);
}

After our fix:

LPTSTR tmp = NULL;
buf = NULL;
if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | 
 FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_IGNORE_INSERTS,
 NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 
  (LPTSTR) &tmp, 0, NULL)) {

    if (SOCKETS_G(strerror_buf)) {
        efree(SOCKETS_G(strerror_buf));
        SOCKETS_G(strerror_buf) = NULL;
    } 
    SOCKETS_G(strerror_buf) = estrdup(tmp);
    LocalFree(tmp);             
    buf = SOCKETS_G(strerror_buf);
}


After applying our fix there are no memory
leaks when running attached example.




Reproduce code:
---------------
<?php
set_time_limit (0);
$address = "localhost";
$port = 1234;
$sock = &socket_create (AF_INET, SOCK_STREAM, 0);
socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind ($sock, $address, $port);
socket_listen ($sock, 10);
socket_set_nonblock($sock);
while(true) {
        @$asock = &socket_accept($sock);
        if ($asock) socket_close($asock);
}
?>

Expected result:
----------------
Every second the script takes additional 3MB of memory.


-- 
Edit bug report at http://bugs.php.net/?id=33019&edit=1
-- 
Try a CVS snapshot (php4):   http://bugs.php.net/fix.php?id=33019&r=trysnapshot4
Try a CVS snapshot (php5.0): 
http://bugs.php.net/fix.php?id=33019&r=trysnapshot50
Try a CVS snapshot (php5.1): 
http://bugs.php.net/fix.php?id=33019&r=trysnapshot51
Fixed in CVS:                http://bugs.php.net/fix.php?id=33019&r=fixedcvs
Fixed in release:            http://bugs.php.net/fix.php?id=33019&r=alreadyfixed
Need backtrace:              http://bugs.php.net/fix.php?id=33019&r=needtrace
Need Reproduce Script:       http://bugs.php.net/fix.php?id=33019&r=needscript
Try newer version:           http://bugs.php.net/fix.php?id=33019&r=oldversion
Not developer issue:         http://bugs.php.net/fix.php?id=33019&r=support
Expected behavior:           http://bugs.php.net/fix.php?id=33019&r=notwrong
Not enough info:             
http://bugs.php.net/fix.php?id=33019&r=notenoughinfo
Submitted twice:             
http://bugs.php.net/fix.php?id=33019&r=submittedtwice
register_globals:            http://bugs.php.net/fix.php?id=33019&r=globals
PHP 3 support discontinued:  http://bugs.php.net/fix.php?id=33019&r=php3
Daylight Savings:            http://bugs.php.net/fix.php?id=33019&r=dst
IIS Stability:               http://bugs.php.net/fix.php?id=33019&r=isapi
Install GNU Sed:             http://bugs.php.net/fix.php?id=33019&r=gnused
Floating point limitations:  http://bugs.php.net/fix.php?id=33019&r=float
No Zend Extensions:          http://bugs.php.net/fix.php?id=33019&r=nozend
MySQL Configuration Error:   http://bugs.php.net/fix.php?id=33019&r=mysqlcfg

Reply via email to