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:
parent
40e2d94bc8
commit
897a177232
1 changed files with 30 additions and 18 deletions
|
@ -146,6 +146,22 @@ is_compatible_hardware(struct resp_probe_some_boards *metadata_some_boards)
|
|||
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)
|
||||
{
|
||||
struct addrinfo *addr;
|
||||
|
@ -185,49 +201,45 @@ static bool start_coap_client(void)
|
|||
return false;
|
||||
}
|
||||
|
||||
ret = 1;
|
||||
|
||||
ctx.sock = socket(addr->ai_family, SOCK_DGRAM, protocol);
|
||||
if (ctx.sock < 0) {
|
||||
LOG_ERR("Failed to create UDP socket");
|
||||
return false;
|
||||
goto error;
|
||||
}
|
||||
|
||||
ret = -1;
|
||||
|
||||
#if defined(CONFIG_UPDATEHUB_DTLS)
|
||||
if (setsockopt(ctx.sock, SOL_TLS, TLS_SEC_TAG_LIST,
|
||||
sec_list, sizeof(sec_list)) < 0) {
|
||||
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) {
|
||||
LOG_ERR("Failed to set TLS_PEER_VERIFY option");
|
||||
return false;
|
||||
goto error;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (connect(ctx.sock, addr->ai_addr, addr->ai_addrlen) < 0) {
|
||||
LOG_ERR("Cannot connect to UDP remote");
|
||||
return false;
|
||||
goto error;
|
||||
}
|
||||
|
||||
prepare_fds();
|
||||
|
||||
return true;
|
||||
}
|
||||
ret = 0;
|
||||
error:
|
||||
freeaddrinfo(addr);
|
||||
|
||||
static void cleanup_connection(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (close(ctx.sock) < 0) {
|
||||
LOG_ERR("Could not close the socket");
|
||||
if (ret > 0) {
|
||||
cleanup_connection();
|
||||
}
|
||||
|
||||
for (i = 0; i < ctx.nfds; i++) {
|
||||
memset(&ctx.fds[i], 0, sizeof(ctx.fds[i]));
|
||||
}
|
||||
|
||||
ctx.nfds = 0;
|
||||
ctx.sock = 0;
|
||||
return (ret == 0) ? true : false;
|
||||
}
|
||||
|
||||
static int send_request(enum coap_msgtype msgtype, enum coap_method method,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue