Skip to content

Commit 6fe5ea5

Browse files
author
Arto Kinnunen
authored
Fix Thread DHCPv6-client memory leak (ARMmbed#1808)
Thread DHCPv6-client is leaking memory if DHCP message sending fails. Release allocated server and message payload to fix the memory leak.
1 parent 7dbad5e commit 6fe5ea5

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

source/6LoWPAN/Thread/thread_dhcpv6_client.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,16 +190,19 @@ int thread_dhcp_client_get_global_address(int8_t interface, uint8_t dhcp_addr[st
190190
}
191191

192192
srv_data_ptr = libdhcvp6_nontemporalAddress_server_data_allocate(interface, dhcp_client.libDhcp_instance, mac64, DHCPV6_DUID_HARDWARE_EUI64_TYPE, prefix, dhcp_addr);
193+
if (!srv_data_ptr) {
194+
tr_error("OOM srv_data_ptr");
195+
return -1;
196+
}
193197

194198
payload_len = libdhcpv6_solication_message_length(DHCPV6_DUID_HARDWARE_EUI64_TYPE, true, 0);
195199
payload_ptr = ns_dyn_mem_temporary_alloc(payload_len);
196-
197-
if (payload_ptr == NULL || srv_data_ptr == NULL) {
198-
ns_dyn_mem_free(payload_ptr);
200+
if (!payload_ptr) {
199201
libdhcvp6_nontemporalAddress_server_data_free(srv_data_ptr);
200-
tr_error("Out of memory");
202+
tr_error("OOM payload_ptr");
201203
return -1;
202204
}
205+
203206
dhcp_client.global_address_cb = error_cb; //TODO Only supporting one instance globaly if we need more for different instances needs code.
204207
srv_data_ptr->GlobalAddress = true;
205208
// Build solicit
@@ -214,7 +217,13 @@ int thread_dhcp_client_get_global_address(int8_t interface, uint8_t dhcp_addr[st
214217

215218
// send solicit
216219
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);
217-
return srv_data_ptr->transActionId ? 0 : -1;
220+
if (srv_data_ptr->transActionId == 0) {
221+
ns_dyn_mem_free(payload_ptr);
222+
libdhcvp6_nontemporalAddress_server_data_free(srv_data_ptr);
223+
return -1;
224+
}
225+
226+
return 0;
218227
}
219228

220229
void thread_dhcp_client_global_address_renew(int8_t interface)

source/libDHCPv6/dhcp_service_api.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ uint32_t dhcp_service_send_req(uint16_t instance_id, uint8_t options, void *ptr,
458458
msg_tr_ptr = dhcp_tr_create();
459459

460460
if (msg_tr_ptr == NULL || srv_ptr == NULL || msg_ptr == NULL || receive_resp_cb == NULL || msg_len < 5) {
461-
tr_error("request sending failed");
461+
tr_error("Request sending failed");
462462
return 0;
463463
}
464464

0 commit comments

Comments
 (0)