Skip to content

Commit

Permalink
Merge pull request #727 from david-cermak/fix/sockutls_gai_error
Browse files Browse the repository at this point in the history
fix(sockutls): Fix gai_strerror() impl to return const string
  • Loading branch information
david-cermak authored Jan 7, 2025
2 parents b4cb8f8 + 9ed835b commit f3f3e23
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 13 deletions.
2 changes: 1 addition & 1 deletion components/sock_utils/.cz.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions components/sock_utils/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion components/sock_utils/idf_component.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
11 changes: 6 additions & 5 deletions components/sock_utils/include/gai_strerror.h
Original file line number Diff line number Diff line change
@@ -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
*/
Expand All @@ -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
}
Expand Down
31 changes: 25 additions & 6 deletions components/sock_utils/src/gai_strerror.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
#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;
}

0 comments on commit f3f3e23

Please sign in to comment.