From f12a20565788974e289d5bdce3587dfff5d3c6de Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 7 Jan 2025 12:17:35 +0100 Subject: [PATCH 1/2] 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. --- components/sock_utils/include/gai_strerror.h | 11 +++---- components/sock_utils/src/gai_strerror.c | 31 ++++++++++++++++---- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/components/sock_utils/include/gai_strerror.h b/components/sock_utils/include/gai_strerror.h index ad872b982..f65601d01 100644 --- a/components/sock_utils/include/gai_strerror.h +++ b/components/sock_utils/include/gai_strerror.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -18,13 +18,14 @@ extern "C" { #endif /** -* @brief Returns a numeric string representing of `getaddrinfo()` error code. +* @brief Returns a string representing of `getaddrinfo()` error code. * -* @param[in] ecode Error code returned by `getaddrinfo()`. +* @param[in] errcode Error code returned by `getaddrinfo()`. * -* @return A pointer to a string describing the error. +* @return A pointer to a string containing the error code, for example "EAI_NONAME" +* for EAI_NONAME error type. */ -const char *gai_strerror(int ecode); +const char *gai_strerror(int errcode); #ifdef __cplusplus } diff --git a/components/sock_utils/src/gai_strerror.c b/components/sock_utils/src/gai_strerror.c index a4060cf2f..a4ff73767 100644 --- a/components/sock_utils/src/gai_strerror.c +++ b/components/sock_utils/src/gai_strerror.c @@ -1,17 +1,36 @@ /* - * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ #include #include "gai_strerror.h" +#include "lwip/netdb.h" -_Thread_local char gai_strerror_string[32]; +#define HANDLE_GAI_ERROR(code) \ + case code: return #code; -const char *gai_strerror(int ecode) +const char *gai_strerror(int errcode) { - if (snprintf(gai_strerror_string, sizeof(gai_strerror_string), "EAI error:%d", ecode) < 0) { - return "gai_strerror() failed"; + switch (errcode) { + /* lwip defined DNS codes */ + HANDLE_GAI_ERROR(EAI_BADFLAGS) + HANDLE_GAI_ERROR(EAI_FAIL) + HANDLE_GAI_ERROR(EAI_FAMILY) + HANDLE_GAI_ERROR(EAI_MEMORY) + HANDLE_GAI_ERROR(EAI_NONAME) + HANDLE_GAI_ERROR(EAI_SERVICE) + /* other error codes optionally defined in platform/newlib or toolchain */ +#ifdef EAI_AGAIN + HANDLE_GAI_ERROR(EAI_AGAIN) +#endif +#ifdef EAI_SOCKTYPE + HANDLE_GAI_ERROR(EAI_SOCKTYPE) +#endif +#ifdef EAI_SYSTEM + HANDLE_GAI_ERROR(EAI_SYSTEM) +#endif + default: + return "Unknown error"; } - return gai_strerror_string; } From 9ed835ba3f8d2795332866bed95f49ff2b032b79 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 7 Jan 2025 14:06:42 +0100 Subject: [PATCH 2/2] bump(sockutls): 0.2.1 -> 0.2.2 0.2.2 Bug Fixes - Fix gai_strerror() impl to return const string (f12a2056) --- components/sock_utils/.cz.yaml | 2 +- components/sock_utils/CHANGELOG.md | 6 ++++++ components/sock_utils/idf_component.yml | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/components/sock_utils/.cz.yaml b/components/sock_utils/.cz.yaml index ed20caec5..f134fe03e 100644 --- a/components/sock_utils/.cz.yaml +++ b/components/sock_utils/.cz.yaml @@ -3,6 +3,6 @@ commitizen: bump_message: 'bump(sockutls): $current_version -> $new_version' pre_bump_hooks: python ../../ci/changelog.py sock_utils tag_format: sock_utils-v$version - version: 0.2.1 + version: 0.2.2 version_files: - idf_component.yml diff --git a/components/sock_utils/CHANGELOG.md b/components/sock_utils/CHANGELOG.md index fcaa12cf3..20adc55fd 100644 --- a/components/sock_utils/CHANGELOG.md +++ b/components/sock_utils/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## [0.2.2](https://github.com/espressif/esp-protocols/commits/sock_utils-v0.2.2) + +### Bug Fixes + +- Fix gai_strerror() impl to return const string ([f12a2056](https://github.com/espressif/esp-protocols/commit/f12a2056)) + ## [0.2.1](https://github.com/espressif/esp-protocols/commits/sock_utils-v0.2.1) ### Bug Fixes diff --git a/components/sock_utils/idf_component.yml b/components/sock_utils/idf_component.yml index 171a8c8b3..0956f7104 100644 --- a/components/sock_utils/idf_component.yml +++ b/components/sock_utils/idf_component.yml @@ -1,4 +1,4 @@ -version: 0.2.1 +version: 0.2.2 description: The component provides helper implementation of common system/socket utilities url: https://github.com/espressif/esp-protocols/tree/master/components/sock_utils dependencies: