Hi,
the following patch fixes memory leaks in the snmp module.

Diff against PHP 5.2 CVS branch.

I'd also like to supply a patch later adding support for multiple set/get operations in one PDU. I'd like not to use php_snmp_internal() function for my new operations because I think that function is to complex and hard to audit. Is that acceptable?

The new function definitions would be something like:

array snmp_mget(string $version, string $hostname, mixed $authparameters, array $variables, [int $timeout, [int $retries]])

Where $authparameters may be (possibly) one of
string <community>
array (<community>)
array (snmpv3param1, snmpv3param2,....)

$variables = array('var1','var2',...)

Result will be returned in same fashion as realwalk().

and

boolean snmp_mset(string $version, string $hostname, mixed $authparameters, array $variables, [int $timeout, [int $retries]])

$variables = array(
        array('oid','type','value'),
        array('oid2','type2','value2')
);

//Gustaf
Index: snmp.c
===================================================================
RCS file: /repository/php-src/ext/snmp/snmp.c,v
retrieving revision 1.106.2.2.2.4
diff -u -r1.106.2.2.2.4 snmp.c
--- snmp.c      1 Jan 2007 09:36:06 -0000       1.106.2.2.2.4
+++ snmp.c      20 Jun 2007 09:18:49 -0000
@@ -421,6 +421,7 @@
                        name_length = MAX_OID_LEN;
                        if (!snmp_parse_oid(objid, name, &name_length)) {
                                php_error_docref(NULL TSRMLS_CC, E_WARNING, 
"Invalid object identifier: %s", objid);
+                               snmp_free_pdu(pdu);
                                snmp_close(ss);
                                RETURN_FALSE;
                        }
@@ -434,6 +435,7 @@
                                sprint_objid(buf, name, name_length);
 #endif
                                php_error_docref(NULL TSRMLS_CC, E_WARNING, 
"Could not add variable: %s %c %s", buf, type, value);
+                               snmp_free_pdu(pdu);
                                snmp_close(ss);
                                RETURN_FALSE;
                        }
@@ -467,11 +469,13 @@
                                                *return_value = *snmpval;
                                                zval_copy_ctor(return_value);
                                                zval_ptr_dtor(&snmpval);
+                                               snmp_free_pdu(response);
                                                snmp_close(ss);
                                                return;
                                        } else if (st == SNMP_CMD_GETNEXT) {
                                                *return_value = *snmpval;
                                                zval_copy_ctor(return_value);
+                                               snmp_free_pdu(response);
                                                snmp_close(ss);
                                                return;
                                        } else if (st == SNMP_CMD_WALK) {
@@ -510,23 +514,28 @@
                                        }
                                        if (st == SNMP_CMD_GET) {
                                                if ((pdu = 
snmp_fix_pdu(response, SNMP_MSG_GET)) != NULL) {
+                                                       snmp_free_pdu(response);
                                                        goto retry;
                                                }
                                        } else if (st == SNMP_CMD_SET) {
                                                if ((pdu = 
snmp_fix_pdu(response, SNMP_MSG_SET)) != NULL) {
+                                                       snmp_free_pdu(response);
                                                        goto retry;
                                                }
                                        } else if (st == SNMP_CMD_GETNEXT) {
                                                if ((pdu = 
snmp_fix_pdu(response, SNMP_MSG_GETNEXT)) != NULL) {
+                                                       snmp_free_pdu(response);
                                                        goto retry;
                                                }
                                        } else if (st >= SNMP_CMD_WALK) { /* 
Here we do walks. */
                                                if ((pdu = 
snmp_fix_pdu(response, ((session->version == SNMP_VERSION_1)
                                                                                
? SNMP_MSG_GETNEXT
                                                                                
: SNMP_MSG_GETBULK))) != NULL) {
+                                                       snmp_free_pdu(response);
                                                        goto retry;
                                                }
                                        }
+                                       snmp_free_pdu(response);
                                        snmp_close(ss);
                                        if (st == SNMP_CMD_WALK || st == 
SNMP_CMD_REALWALK) {
                                                zval_dtor(return_value);

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to