Fix memory leak setting transports

This commit is contained in:
Willem Toorop 2015-07-13 16:39:43 +02:00
parent 5c61954427
commit a8adf662d1
1 changed files with 79 additions and 65 deletions

View File

@ -1190,15 +1190,14 @@ getdns_context_set_namespaces(struct getdns_context *context,
} /* getdns_context_set_namespaces */ } /* getdns_context_set_namespaces */
static getdns_return_t static getdns_return_t
getdns_set_base_dns_transports(struct getdns_context *context, getdns_set_base_dns_transports(
size_t transport_count, getdns_transport_list_t *transports) getdns_context *context, size_t transport_count, getdns_transport_list_t *transports)
{ {
size_t i; size_t i;
getdns_transport_list_t *new_transports;
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER); if (!context || transport_count == 0 || transports == NULL)
if (transport_count == 0 || transports == NULL) { return GETDNS_RETURN_INVALID_PARAMETER;
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
}
for(i=0; i<transport_count; i++) for(i=0; i<transport_count; i++)
{ {
@ -1209,11 +1208,16 @@ getdns_set_base_dns_transports(struct getdns_context *context,
return GETDNS_RETURN_INVALID_PARAMETER; return GETDNS_RETURN_INVALID_PARAMETER;
} }
if (!(new_transports = GETDNS_XMALLOC(context->my_mf,
getdns_transport_list_t, transport_count)))
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
if (context->dns_transports)
GETDNS_FREE(context->my_mf, context->dns_transports); GETDNS_FREE(context->my_mf, context->dns_transports);
/** duplicate **/ /** duplicate **/
context->dns_transports = GETDNS_XMALLOC(context->my_mf, context->dns_transports = new_transports;
getdns_transport_list_t, transport_count);
memcpy(context->dns_transports, transports, memcpy(context->dns_transports, transports,
transport_count * sizeof(getdns_transport_list_t)); transport_count * sizeof(getdns_transport_list_t));
context->dns_transport_count = transport_count; context->dns_transport_count = transport_count;
@ -1278,20 +1282,30 @@ set_ub_dns_transport(struct getdns_context* context) {
* *
*/ */
getdns_return_t getdns_return_t
getdns_context_set_dns_transport(struct getdns_context *context, getdns_context_set_dns_transport(
getdns_transport_t value) getdns_context *context, getdns_transport_t value)
{ {
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
size_t count = 2; size_t count = 2;
getdns_transport_list_t *new_transports;
if (value == GETDNS_TRANSPORT_UDP_ONLY || if (value == GETDNS_TRANSPORT_UDP_ONLY ||
value == GETDNS_TRANSPORT_TCP_ONLY || value == GETDNS_TRANSPORT_TCP_ONLY ||
value == GETDNS_TRANSPORT_TCP_ONLY_KEEP_CONNECTIONS_OPEN || value == GETDNS_TRANSPORT_TCP_ONLY_KEEP_CONNECTIONS_OPEN ||
value == GETDNS_TRANSPORT_TLS_ONLY_KEEP_CONNECTIONS_OPEN) value == GETDNS_TRANSPORT_TLS_ONLY_KEEP_CONNECTIONS_OPEN)
count = 1; count = 1;
if (!context)
return GETDNS_RETURN_INVALID_PARAMETER;
if (!(new_transports = GETDNS_XMALLOC(
context->my_mf, getdns_transport_list_t, count)))
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
if (context->dns_transports)
GETDNS_FREE(context->my_mf, context->dns_transports);
context->dns_transport_count = count; context->dns_transport_count = count;
context->dns_transports = GETDNS_XMALLOC(context->my_mf, context->dns_transports = new_transports;
getdns_transport_list_t, count);
switch (value) { switch (value) {
case GETDNS_TRANSPORT_UDP_FIRST_AND_FALL_BACK_TO_TCP: case GETDNS_TRANSPORT_UDP_FIRST_AND_FALL_BACK_TO_TCP:
@ -1333,7 +1347,7 @@ getdns_context_set_dns_transport(struct getdns_context *context,
} }
/* /*
* getdns_context_set_dns_transport * getdns_context_set_dns_transport_list
* *
*/ */
getdns_return_t getdns_return_t
@ -1354,7 +1368,7 @@ getdns_context_set_dns_transport_list(getdns_context *context,
} }
dispatch_updated(context, GETDNS_CONTEXT_CODE_DNS_TRANSPORT); dispatch_updated(context, GETDNS_CONTEXT_CODE_DNS_TRANSPORT);
return GETDNS_RETURN_GOOD; return GETDNS_RETURN_GOOD;
} /* getdns_context_set_dns_transport */ } /* getdns_context_set_dns_transport_list */
static void static void
set_ub_limit_outstanding_queries(struct getdns_context* context, uint16_t value) { set_ub_limit_outstanding_queries(struct getdns_context* context, uint16_t value) {