Skip to content

Commit f12a205

Browse files
committed
fix(sockutls): Fix gai_strerror() impl to return const string
Previous gai_strerror() returned numeric representation of the code, but used TLS storage, which might cause issues with stack sizes on all tasks in the system. Alternatively we can leave the storage to static only (which wouldn't be thread-safe) or we could one-time allocate and never free (which is wrong). This option uses hardcoded strings for common error codes used in lwip. The disadvantage is that we might need to update the impl in the future when lwip adds more codes.
1 parent b4cb8f8 commit f12a205

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

components/sock_utils/include/gai_strerror.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -18,13 +18,14 @@ extern "C" {
1818
#endif
1919

2020
/**
21-
* @brief Returns a numeric string representing of `getaddrinfo()` error code.
21+
* @brief Returns a string representing of `getaddrinfo()` error code.
2222
*
23-
* @param[in] ecode Error code returned by `getaddrinfo()`.
23+
* @param[in] errcode Error code returned by `getaddrinfo()`.
2424
*
25-
* @return A pointer to a string describing the error.
25+
* @return A pointer to a string containing the error code, for example "EAI_NONAME"
26+
* for EAI_NONAME error type.
2627
*/
27-
const char *gai_strerror(int ecode);
28+
const char *gai_strerror(int errcode);
2829

2930
#ifdef __cplusplus
3031
}
Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,36 @@
11
/*
2-
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66
#include <stdio.h>
77
#include "gai_strerror.h"
8+
#include "lwip/netdb.h"
89

9-
_Thread_local char gai_strerror_string[32];
10+
#define HANDLE_GAI_ERROR(code) \
11+
case code: return #code;
1012

11-
const char *gai_strerror(int ecode)
13+
const char *gai_strerror(int errcode)
1214
{
13-
if (snprintf(gai_strerror_string, sizeof(gai_strerror_string), "EAI error:%d", ecode) < 0) {
14-
return "gai_strerror() failed";
15+
switch (errcode) {
16+
/* lwip defined DNS codes */
17+
HANDLE_GAI_ERROR(EAI_BADFLAGS)
18+
HANDLE_GAI_ERROR(EAI_FAIL)
19+
HANDLE_GAI_ERROR(EAI_FAMILY)
20+
HANDLE_GAI_ERROR(EAI_MEMORY)
21+
HANDLE_GAI_ERROR(EAI_NONAME)
22+
HANDLE_GAI_ERROR(EAI_SERVICE)
23+
/* other error codes optionally defined in platform/newlib or toolchain */
24+
#ifdef EAI_AGAIN
25+
HANDLE_GAI_ERROR(EAI_AGAIN)
26+
#endif
27+
#ifdef EAI_SOCKTYPE
28+
HANDLE_GAI_ERROR(EAI_SOCKTYPE)
29+
#endif
30+
#ifdef EAI_SYSTEM
31+
HANDLE_GAI_ERROR(EAI_SYSTEM)
32+
#endif
33+
default:
34+
return "Unknown error";
1535
}
16-
return gai_strerror_string;
1736
}

0 commit comments

Comments
 (0)