Skip to content

Commit

Permalink
Fix two memory leaks in encode_assertion_control
Browse files Browse the repository at this point in the history
encode_assertion_control was leaking a LDAP* struct and BER data values
on every call. Both data structures are now properly freed and the GIL
is released around ldap_create() and ldap_unbind_s().

Closes: https://github.com/python-ldap/python-ldap/issues/79
Signed-off-by: Christian Heimes <cheimes@redhat.com>
  • Loading branch information
Christian Heimes authored and Petr Viktorin committed Dec 4, 2017
1 parent c86b8e9 commit 9f86c06
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Modules/
* Fix several compiler warnings
* Fix memory leak in whoami
* Fix internal error handling of LDAPControl_to_List()
* Fix two memory leaks and release GIL in encode_assertion_control
and, thanks to Michael Ströder:
* removed unused code schema.c
* moved code from version.c to ldapmodule.c
Expand Down
25 changes: 20 additions & 5 deletions Modules/ldapcontrol.c
Original file line number Diff line number Diff line change
Expand Up @@ -343,16 +343,33 @@ encode_assertion_control(PyObject *self, PyObject *args)
goto endlbl;
}

/* XXX: ldap_create() is a nasty and slow hack. It's creating a full blown
* LDAP object just to encode assertion controls.
*/
Py_BEGIN_ALLOW_THREADS
err = ldap_create(&ld);
Py_END_ALLOW_THREADS

if (err != LDAP_SUCCESS)
return LDAPerror(ld, "ldap_create");

err = ldap_create_assertion_control_value(ld,assertion_filterstr,&ctrl_val);
if (err != LDAP_SUCCESS)
return LDAPerror(ld, "ldap_create_assertion_control_value");

res = LDAPberval_to_object(&ctrl_val);
if (err != LDAP_SUCCESS) {
LDAPerror(ld, "ldap_create_assertion_control_value");
Py_BEGIN_ALLOW_THREADS
ldap_unbind_ext(ld, NULL, NULL);
Py_END_ALLOW_THREADS
return NULL;
}
Py_BEGIN_ALLOW_THREADS
ldap_unbind_ext(ld, NULL, NULL);
Py_END_ALLOW_THREADS

res = LDAPberval_to_object(&ctrl_val);
if (ctrl_val.bv_val != NULL) {
ber_memfree(ctrl_val.bv_val);
}
endlbl:

return res;
Expand All @@ -371,5 +388,3 @@ LDAPinit_control(PyObject *d)
{
LDAPadd_methods(d, methods);
}


0 comments on commit 9f86c06

Please sign in to comment.