Skip to content

Commit 219f8e6

Browse files
committed
fixing ldap memory leaks php#5
1 parent aaca9a2 commit 219f8e6

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

ext/ldap/ldap.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ static void _php_ldap_control_to_array(LDAP *ld, LDAPControl* ctrl, zval* array,
166166
}
167167
} else if (strcmp(ctrl->ldctl_oid, LDAP_CONTROL_PAGEDRESULTS) == 0) {
168168
int lestimated, rc;
169-
struct berval lcookie = { 0, NULL };
169+
struct berval lcookie = { 0L, NULL };
170170
zval value;
171171

172172
if (ctrl->ldctl_value.bv_len) {
@@ -308,7 +308,7 @@ static int _php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, zval* arra
308308
}
309309

310310
BerElement *ber = NULL;
311-
struct berval control_value = { 0, NULL };
311+
struct berval control_value = { 0L, NULL };
312312
int control_value_alloc = 0;
313313

314314
if ((val = zend_hash_str_find(Z_ARRVAL_P(array), "value", sizeof("value") - 1)) != NULL) {
@@ -323,7 +323,7 @@ static int _php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, zval* arra
323323
} else if (strcmp(ZSTR_VAL(control_oid), LDAP_CONTROL_PAGEDRESULTS) == 0) {
324324
zval* tmp;
325325
int pagesize = 1;
326-
struct berval cookie = { 0, NULL };
326+
struct berval cookie = { 0L, NULL };
327327
if ((tmp = zend_hash_str_find(Z_ARRVAL_P(val), "size", sizeof("size") - 1)) != NULL) {
328328
pagesize = zval_get_long(tmp);
329329
}
@@ -1447,7 +1447,7 @@ static void php_ldap_do_search(INTERNAL_FUNCTION_PARAMETERS, int scope)
14471447
zend_string *ldap_filter = NULL, *ldap_base_dn = NULL;
14481448
char **ldap_attrs = NULL;
14491449
ldap_linkdata *ld = NULL;
1450-
LDAPMessage *ldap_res;
1450+
LDAPMessage *ldap_res = NULL;
14511451
LDAPControl **lserverctrls = NULL;
14521452
int ldap_attrsonly = 0, ldap_sizelimit = -1, ldap_timelimit = -1, ldap_deref = -1;
14531453
int old_ldap_sizelimit = -1, old_ldap_timelimit = -1, old_ldap_deref = -1;
@@ -1647,6 +1647,13 @@ static void php_ldap_do_search(INTERNAL_FUNCTION_PARAMETERS, int scope)
16471647
&& errno != LDAP_REFERRAL
16481648
#endif
16491649
) {
1650+
/* ldap_res should be freed with ldap_msgfree() regardless of return
1651+
* value of ldap_search_ext_s()
1652+
* see: https://linux.die.net/man/3/ldap_search_ext_s
1653+
*/
1654+
if (ldap_res != NULL) {
1655+
ldap_msgfree(ldap_res);
1656+
}
16501657
php_error_docref(NULL, E_WARNING, "Search: %s", ldap_err2string(errno));
16511658
ret = 0;
16521659
} else {
@@ -3968,7 +3975,7 @@ PHP_FUNCTION(ldap_control_paged_result)
39683975
zval *link;
39693976
char *cookie = NULL;
39703977
size_t cookie_len = 0;
3971-
struct berval lcookie = { 0, NULL };
3978+
struct berval lcookie = { 0L, NULL };
39723979
ldap_linkdata *ld;
39733980
LDAP *ldap;
39743981
BerElement *ber = NULL;

0 commit comments

Comments
 (0)