From ed4b777b298be21709a45f263979819bf49a66bf Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Wed, 27 Dec 2023 21:24:25 +0100 Subject: [PATCH] examples/dtls-sock: fix error handling in client Not closing a stack allocated socket before going out of scope of the allocation is something to avoid. This fixes crashes at runtime when adding the credentials fails. --- examples/dtls-sock/dtls-client.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/examples/dtls-sock/dtls-client.c b/examples/dtls-sock/dtls-client.c index 4a913cee95..c8dc3a2ce1 100644 --- a/examples/dtls-sock/dtls-client.c +++ b/examples/dtls-sock/dtls-client.c @@ -155,6 +155,7 @@ static int client_send(char *addr_str, char *data, size_t datalen) if (res < 0 && res != CREDMAN_EXIST) { /* ignore duplicate credentials */ printf("Error cannot add credential to system: %" PRIdSIZE "\n", res); + sock_udp_close(&udp_sock); return -1; } @@ -171,12 +172,14 @@ static int client_send(char *addr_str, char *data, size_t datalen) if (res < 0 && res != CREDMAN_EXIST) { /* ignore duplicate credentials */ printf("Error cannot add credential to system: %" PRIdSIZE "\n", res); + sock_udp_close(&udp_sock); return -1; } /* make the new credential available to the sock */ if (sock_dtls_add_credential(&dtls_sock, SOCK_DTLS_CLIENT_TAG_1) < 0) { printf("Error cannot add credential to the sock: %" PRIdSIZE "\n", res); + sock_udp_close(&udp_sock); return -1; } @@ -186,6 +189,7 @@ static int client_send(char *addr_str, char *data, size_t datalen) res = sock_dtls_session_init(&dtls_sock, &remote, &session); if (res <= 0) { + sock_udp_close(&udp_sock); return res; }