Skip to content

Commit

Permalink
openldap: move the alloc of ldapconninfo to *connect()
Browse files Browse the repository at this point in the history
Fixes a minor memory leak on LDAP connection reuse.

Doing the allocation already in *setup_connection() is wrong since that
connect struct might get discarded early when an existing connection is
reused instead.

Closes #12166
  • Loading branch information
bagder committed Oct 21, 2023
1 parent 3afbe06 commit 526779a
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions lib/openldap.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,31 +319,12 @@ static CURLcode oldap_setup_connection(struct Curl_easy *data,
{
CURLcode result;
LDAPURLDesc *lud;
struct ldapconninfo *li;
(void)conn;

/* Early URL syntax check. */
result = oldap_url_parse(data, &lud);
ldap_free_urldesc(lud);

if(!result) {
li = calloc(1, sizeof(struct ldapconninfo));
if(!li)
result = CURLE_OUT_OF_MEMORY;
else {
li->proto = ldap_pvt_url_scheme2proto(data->state.up.scheme);
conn->proto.ldapc = li;
connkeep(conn, "OpenLDAP default");

/* Initialize the SASL storage */
Curl_sasl_init(&li->sasl, data, &saslldap);

/* Clear the TLS upgraded flag */
conn->bits.tls_upgraded = FALSE;

result = oldap_parse_login_options(conn);
}
}

return result;
}

Expand Down Expand Up @@ -537,7 +518,7 @@ static CURLcode oldap_perform_starttls(struct Curl_easy *data)
static CURLcode oldap_connect(struct Curl_easy *data, bool *done)
{
struct connectdata *conn = data->conn;
struct ldapconninfo *li = conn->proto.ldapc;
struct ldapconninfo *li;
static const int version = LDAP_VERSION3;
int rc;
char *hosturl;
Expand All @@ -547,6 +528,26 @@ static CURLcode oldap_connect(struct Curl_easy *data, bool *done)

(void)done;

DEBUGASSERT(!conn->proto.ldapc);
li = calloc(1, sizeof(struct ldapconninfo));
if(!li)
return CURLE_OUT_OF_MEMORY;
else {
CURLcode result;
li->proto = ldap_pvt_url_scheme2proto(data->state.up.scheme);
conn->proto.ldapc = li;

/* Initialize the SASL storage */
Curl_sasl_init(&li->sasl, data, &saslldap);

/* Clear the TLS upgraded flag */
conn->bits.tls_upgraded = FALSE;

result = oldap_parse_login_options(conn);
if(result)
return result;
}

hosturl = aprintf("ldap%s://%s:%d",
conn->handler->flags & PROTOPT_SSL? "s": "",
conn->host.name, conn->remote_port);
Expand Down

0 comments on commit 526779a

Please sign in to comment.