Commit 44fc9dca authored by Gerson Fernando Budke's avatar Gerson Fernando Budke Committed by Anas Nashif
Browse files

lib: updatehub: Fix getaddrinfo resource leak



Add missing freeaddrinfo to fix memory leak.

Fixes #26994.

Signed-off-by: default avatarGerson Fernando Budke <gerson.budke@ossystems.com.br>
parent 69c56691
Loading
Loading
Loading
Loading
+25 −13
Original line number Diff line number Diff line
@@ -146,6 +146,17 @@ is_compatible_hardware(struct resp_probe_some_boards *metadata_some_boards)
	return false;
}

static void cleanup_connection(void)
{
	if (close(ctx.sock) < 0) {
		LOG_ERR("Could not close the socket");
	}

	memset(&ctx.fds[1], 0, sizeof(ctx.fds[1]));
	ctx.nfds = 0;
	ctx.sock = 0;
}

static bool start_coap_client(void)
{
	struct addrinfo *addr;
@@ -185,44 +196,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)
{
	if (close(ctx.sock) < 0) {
		LOG_ERR("Could not close the socket");
	if (ret > 0) {
		cleanup_connection();
	}

	memset(&ctx.fds[1], 0, sizeof(ctx.fds[1]));
	ctx.nfds = 0;
	ctx.sock = 0;
	return (ret == 0) ? true : false;
}

static int send_request(enum coap_msgtype msgtype, enum coap_method method,