]> granicus.if.org Git - php/commitdiff
MFH:- Fixed bug #41127 (Memory leak in ldap_{first|next}_attribute functions)
authorJani Taskinen <jani@php.net>
Fri, 13 Jul 2007 01:24:16 +0000 (01:24 +0000)
committerJani Taskinen <jani@php.net>
Fri, 13 Jul 2007 01:24:16 +0000 (01:24 +0000)
# This removes an useless parameter from 2 functions. Perhaps the next
# release should be 5.3.0, there are lot of other new things done already.
# Sneak in namespaces too.. ;)

NEWS
ext/ldap/ldap.c
ext/ldap/php_ldap.h

diff --git a/NEWS b/NEWS
index 95914ee4700da3620e0c62ef38fd361ccaff1fa2..dd6dbe323c72f89743e300fe9bbe46677b784afd 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -142,6 +142,8 @@ PHP                                                                        NEWS
   integer as sections). (Tony)
 - Fixed bug #41350 (my_thread_global_end() error during request shutdown
   on Windows). (Scott, Andrey)
+- Fixed bug #41127 (Memory leak in ldap_{first|next}_attribute functions).
+  (Jani)
 - Fixed bug #40419 (Trailing slash in CGI request does not work). (Dmitry)
 - Fixed bug #39330 (apache2handler does not call shutdown actions before 
   apache child die). (isk at ecommerce dot com, Gopal, Tony)
index 4d6331be1eae11bbb174f6d0e45e0f126fa1920d..77a44dd8c89b6b8a0319a290aa64dacb27f05a4a 100644 (file)
@@ -75,6 +75,7 @@ typedef struct {
 
 typedef struct {
        LDAPMessage *data;
+       BerElement *ber;
        int id;
 } ldap_resultentry;
 
@@ -91,7 +92,7 @@ static
                ZEND_ARG_PASS_INFO(1)
        ZEND_END_ARG_INFO();
 
-static int le_link, le_result, le_result_entry, le_ber_entry;
+static int le_link, le_result, le_result_entry;
 
 /*
        This is just a small subset of the functionality provided by the LDAP library. All the 
@@ -217,6 +218,10 @@ static void _free_ldap_result(zend_rsrc_list_entry *rsrc TSRMLS_DC)
 static void _free_ldap_result_entry(zend_rsrc_list_entry *rsrc TSRMLS_DC)
 {
        ldap_resultentry *entry = (ldap_resultentry *)rsrc->ptr;
+
+       if (entry->ber != NULL) {
+               ber_free(entry->ber, 0);
+       }
        zend_list_delete(entry->id);
        efree(entry);
 } 
@@ -286,10 +291,9 @@ PHP_MINIT_FUNCTION(ldap)
        REGISTER_LONG_CONSTANT("GSLC_SSL_TWOWAY_AUTH", GSLC_SSL_TWOWAY_AUTH, CONST_PERSISTENT | CONST_CS);
 #endif
 
-       le_result = zend_register_list_destructors_ex(_free_ldap_result, NULL, "ldap result", module_number);
        le_link = zend_register_list_destructors_ex(_close_ldap_link, NULL, "ldap link", module_number);
+       le_result = zend_register_list_destructors_ex(_free_ldap_result, NULL, "ldap result", module_number);
        le_result_entry = zend_register_list_destructors_ex(_free_ldap_result_entry, NULL, "ldap result entry", module_number);
-       le_ber_entry = zend_register_list_destructors_ex(NULL, NULL, "ldap ber entry", module_number);
 
        Z_TYPE(ldap_module_entry) = type;
 
@@ -993,6 +997,7 @@ PHP_FUNCTION(ldap_first_entry)
                resultentry->id = Z_LVAL_PP(result);
                zend_list_addref(resultentry->id);
                resultentry->data = entry;
+               resultentry->ber = NULL;
        }
 }
 /* }}} */
@@ -1021,6 +1026,7 @@ PHP_FUNCTION(ldap_next_entry)
                resultentry_next->id = resultentry->id;
                zend_list_addref(resultentry->id);
                resultentry_next->data = entry_next;
+               resultentry_next->ber = NULL;
        }
 }
 /* }}} */
@@ -1091,8 +1097,9 @@ PHP_FUNCTION(ldap_get_entries)
                        attribute = ldap_next_attribute(ldap, ldap_result_entry, ber);
                }
 #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP_10 || WINDOWS
-               if (ber != NULL)
+               if (ber != NULL) {
                        ber_free(ber, 0);
+               }
 #endif
 
                add_assoc_long(tmp1, "count", num_attrib);
@@ -1115,28 +1122,25 @@ PHP_FUNCTION(ldap_get_entries)
 }
 /* }}} */
 
-/* {{{ proto string ldap_first_attribute(resource link, resource result_entry, int ber)
+/* {{{ proto string ldap_first_attribute(resource link, resource result_entry)
    Return first attribute */
 PHP_FUNCTION(ldap_first_attribute)
 {
-       zval **link, **result_entry, **berp;
+       zval **link, **result_entry;
        ldap_linkdata *ld;
        ldap_resultentry *resultentry;
-       BerElement *ber;
        char *attribute;
 
-       if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &link, &result_entry, &berp) == FAILURE) {
+       if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &link, &result_entry) == FAILURE) {
                WRONG_PARAM_COUNT;
        }
 
        ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, link, -1, "ldap link", le_link);
        ZEND_FETCH_RESOURCE(resultentry, ldap_resultentry *, result_entry, -1, "ldap result entry", le_result_entry);
 
-       if ((attribute = ldap_first_attribute(ld->link, resultentry->data, &ber)) == NULL) {
+       if ((attribute = ldap_first_attribute(ld->link, resultentry->data, &resultentry->ber)) == NULL) {
                RETURN_FALSE;
        } else {
-               ZEND_REGISTER_RESOURCE(*berp, ber, le_ber_entry);
-
                RETVAL_STRING(attribute, 1);
 #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP_10 || WINDOWS
                ldap_memfree(attribute);
@@ -1145,29 +1149,31 @@ PHP_FUNCTION(ldap_first_attribute)
 }
 /* }}} */
 
-/* {{{ proto string ldap_next_attribute(resource link, resource result_entry, resource ber)
+/* {{{ proto string ldap_next_attribute(resource link, resource result_entry)
    Get the next attribute in result */
 PHP_FUNCTION(ldap_next_attribute)
 {
-       zval **link, **result_entry, **berp;
+       zval **link, **result_entry;
        ldap_linkdata *ld;
        ldap_resultentry *resultentry;
-       BerElement *ber;
        char *attribute;
 
-       if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &link, &result_entry, &berp) == FAILURE) {
+       if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &link, &result_entry) == FAILURE) {
                WRONG_PARAM_COUNT;
        }
 
        ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, link, -1, "ldap link", le_link);
        ZEND_FETCH_RESOURCE(resultentry, ldap_resultentry *, result_entry, -1, "ldap result entry", le_result_entry);
-       ZEND_FETCH_RESOURCE(ber, BerElement *, berp, -1, "ldap ber entry", le_ber_entry);
 
-       if ((attribute = ldap_next_attribute(ld->link, resultentry->data, ber)) == NULL) {
+       if ((attribute = ldap_next_attribute(ld->link, resultentry->data, resultentry->ber)) == NULL) {
+#if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP_10 || WINDOWS
+               if (resultentry->ber != NULL) {
+                       ber_free(resultentry->ber, 0);
+                       resultentry->ber = NULL;
+               }
+#endif
                RETURN_FALSE;
        } else {
-               ZEND_REGISTER_RESOURCE(*berp, ber, le_ber_entry);
-
                RETVAL_STRING(attribute, 1);
 #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP_10 || WINDOWS
                ldap_memfree(attribute);
@@ -1222,9 +1228,9 @@ PHP_FUNCTION(ldap_get_attributes)
                attribute = ldap_next_attribute(ld->link, resultentry->data, ber);
        }
 #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP_10 || WINDOWS
-               if (ber != NULL) {
-                       ber_free(ber, 0);
-               }
+       if (ber != NULL) {
+               ber_free(ber, 0);
+       }
 #endif
        
        add_assoc_long(return_value, "count", num_attrib);
index d39708d976baf6027bb197f0b3b5bedc8eeb8a91..780e4ed37362185711d86180e5485debd0122894 100644 (file)
@@ -55,7 +55,6 @@ PHP_FUNCTION(ldap_next_attribute);
 PHP_FUNCTION(ldap_get_attributes);
 PHP_FUNCTION(ldap_get_values);
 PHP_FUNCTION(ldap_get_values_len);
-PHP_FUNCTION(ber_free);
 PHP_FUNCTION(ldap_get_dn);
 PHP_FUNCTION(ldap_explode_dn);
 PHP_FUNCTION(ldap_dn2ufn);