lib: updatehub: Fix getaddrinfo resource leak

Add missing freeaddrinfo to fix memory leak.

Fixes #26994.

Signed-off-by: Gerson Fernando Budke <gerson.budke@ossystems.com.br>
This commit is contained in:
Gerson Fernando Budke 2020-07-28 22:50:02 -03:00 committed by Anas Nashif
commit 897a177232

View file

@ -146,6 +146,22 @@ is_compatible_hardware(struct resp_probe_some_boards *metadata_some_boards)
return false; return false;
} }
static void cleanup_connection(void)
{
int i;
if (close(ctx.sock) < 0) {
LOG_ERR("Could not close the socket");
}
for (i = 0; i < ctx.nfds; i++) {
memset(&ctx.fds[i], 0, sizeof(ctx.fds[i]));
}
ctx.nfds = 0;
ctx.sock = 0;
}
static bool start_coap_client(void) static bool start_coap_client(void)
{ {
struct addrinfo *addr; struct addrinfo *addr;
@ -185,49 +201,45 @@ static bool start_coap_client(void)
return false; return false;
} }
ret = 1;
ctx.sock = socket(addr->ai_family, SOCK_DGRAM, protocol); ctx.sock = socket(addr->ai_family, SOCK_DGRAM, protocol);
if (ctx.sock < 0) { if (ctx.sock < 0) {
LOG_ERR("Failed to create UDP socket"); LOG_ERR("Failed to create UDP socket");
return false; goto error;
} }
ret = -1;
#if defined(CONFIG_UPDATEHUB_DTLS) #if defined(CONFIG_UPDATEHUB_DTLS)
if (setsockopt(ctx.sock, SOL_TLS, TLS_SEC_TAG_LIST, if (setsockopt(ctx.sock, SOL_TLS, TLS_SEC_TAG_LIST,
sec_list, sizeof(sec_list)) < 0) { sec_list, sizeof(sec_list)) < 0) {
LOG_ERR("Failed to set TLS_TAG option"); LOG_ERR("Failed to set TLS_TAG option");
return false; goto error;
} }
if (setsockopt(ctx.sock, SOL_TLS, TLS_PEER_VERIFY, &verify, sizeof(int)) < 0) { if (setsockopt(ctx.sock, SOL_TLS, TLS_PEER_VERIFY, &verify, sizeof(int)) < 0) {
LOG_ERR("Failed to set TLS_PEER_VERIFY option"); LOG_ERR("Failed to set TLS_PEER_VERIFY option");
return false; goto error;
} }
#endif #endif
if (connect(ctx.sock, addr->ai_addr, addr->ai_addrlen) < 0) { if (connect(ctx.sock, addr->ai_addr, addr->ai_addrlen) < 0) {
LOG_ERR("Cannot connect to UDP remote"); LOG_ERR("Cannot connect to UDP remote");
return false; goto error;
} }
prepare_fds(); prepare_fds();
return true; ret = 0;
} error:
freeaddrinfo(addr);
static void cleanup_connection(void) if (ret > 0) {
{ cleanup_connection();
int i;
if (close(ctx.sock) < 0) {
LOG_ERR("Could not close the socket");
} }
for (i = 0; i < ctx.nfds; i++) { return (ret == 0) ? true : false;
memset(&ctx.fds[i], 0, sizeof(ctx.fds[i]));
}
ctx.nfds = 0;
ctx.sock = 0;
} }
static int send_request(enum coap_msgtype msgtype, enum coap_method method, static int send_request(enum coap_msgtype msgtype, enum coap_method method,