Skip to content

Commit

Permalink
Fix Thread DHCPv6-client memory leak (ARMmbed#1808)
Browse files Browse the repository at this point in the history
Thread DHCPv6-client is leaking memory if DHCP message sending fails.
Release allocated server and message payload to fix the memory leak.
  • Loading branch information
Arto Kinnunen authored Sep 5, 2018
1 parent 7dbad5e commit 6fe5ea5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
19 changes: 14 additions & 5 deletions source/6LoWPAN/Thread/thread_dhcpv6_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,16 +190,19 @@ int thread_dhcp_client_get_global_address(int8_t interface, uint8_t dhcp_addr[st
}

srv_data_ptr = libdhcvp6_nontemporalAddress_server_data_allocate(interface, dhcp_client.libDhcp_instance, mac64, DHCPV6_DUID_HARDWARE_EUI64_TYPE, prefix, dhcp_addr);
if (!srv_data_ptr) {
tr_error("OOM srv_data_ptr");
return -1;
}

payload_len = libdhcpv6_solication_message_length(DHCPV6_DUID_HARDWARE_EUI64_TYPE, true, 0);
payload_ptr = ns_dyn_mem_temporary_alloc(payload_len);

if (payload_ptr == NULL || srv_data_ptr == NULL) {
ns_dyn_mem_free(payload_ptr);
if (!payload_ptr) {
libdhcvp6_nontemporalAddress_server_data_free(srv_data_ptr);
tr_error("Out of memory");
tr_error("OOM payload_ptr");
return -1;
}

dhcp_client.global_address_cb = error_cb; //TODO Only supporting one instance globaly if we need more for different instances needs code.
srv_data_ptr->GlobalAddress = true;
// Build solicit
Expand All @@ -214,7 +217,13 @@ int thread_dhcp_client_get_global_address(int8_t interface, uint8_t dhcp_addr[st

// send solicit
srv_data_ptr->transActionId = dhcp_service_send_req(dhcp_client.service_instance, 0, srv_data_ptr , dhcp_addr, payload_ptr, payload_len, dhcp_solicit_resp_cb);
return srv_data_ptr->transActionId ? 0 : -1;
if (srv_data_ptr->transActionId == 0) {
ns_dyn_mem_free(payload_ptr);
libdhcvp6_nontemporalAddress_server_data_free(srv_data_ptr);
return -1;
}

return 0;
}

void thread_dhcp_client_global_address_renew(int8_t interface)
Expand Down
2 changes: 1 addition & 1 deletion source/libDHCPv6/dhcp_service_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ uint32_t dhcp_service_send_req(uint16_t instance_id, uint8_t options, void *ptr,
msg_tr_ptr = dhcp_tr_create();

if (msg_tr_ptr == NULL || srv_ptr == NULL || msg_ptr == NULL || receive_resp_cb == NULL || msg_len < 5) {
tr_error("request sending failed");
tr_error("Request sending failed");
return 0;
}

Expand Down

0 comments on commit 6fe5ea5

Please sign in to comment.