[Date Prev][Date Next] [Chronological] [Thread] [Top]

RE: Memory (Socket/Handle) leak in libldap

> This sure looks like a bug to me.  Can someone confirm this -
> and/or show me
> a better way to fix it?

I found a better way to fix this, but it still requires a change to libldap.

In the situation that I've stated (the OpenLDAP server daemon is stopped) a
call to ldap_open fails.  In ldap_open (file: open.c) the ldap_init call is
successful, but the ldap_delayed_open call fails.  When ldap_delayed_open
fails there should be a call to ldap_unbind to free resources.

> application attempts to do cleanup by calling WSACleanup() - this is
> supposed to close any remaining open sockets.  This doesn't seem to work,

The call to ldap_unbind fixes some memory leaks I was seeing as well as the
handle leak (it calls WSACleanup() - and this time it works!).

This is what I did......in open.c

ldap_open( char *host, int port )
	LDAP		*ld;
	LDAPServer	*srv;
#endif /* LDAP_REFERRALS */

	Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );

	if (( ld = ldap_init( host, port )) == NULL ) {
		return( NULL );

	if ( ldap_delayed_open( ld ) < 0 ) {
		ldap_unbind( ld );	// Free Resources...
		return( NULL );

	Debug( LDAP_DEBUG_TRACE, "ldap_open successful, ld_host is %s\n",
		( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );

	return( ld );