[libc-commits] [libc] 54f7d98 - [libc][netdb] Implement gai_strerror POSIX function. (#221138)

via libc-commits libc-commits at lists.llvm.org
Fri Sep 4 07:54:26 PDT 2026


Author: Alexey Samsonov
Date: 2026-09-04T07:54:21-07:00
New Revision: 54f7d98d52413399a14111bfede1c9e388ac62c7

URL: https://github.com/llvm/llvm-project/commit/54f7d98d52413399a14111bfede1c9e388ac62c7
DIFF: https://github.com/llvm/llvm-project/commit/54f7d98d52413399a14111bfede1c9e388ac62c7.diff

LOG: [libc][netdb] Implement gai_strerror POSIX function. (#221138)

`gai_strerror` is defined in POSIX and used to provide human-readable
strings for `EAI_*` error codes returned by
`getaddrinfo` and `getnameinfo`.

Provide our implementation via simple switch. Provide string values
based on the POSIX documentation for `EAI_`
values in
https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/netdb.h.html,
but shorten them for brevity.

Added: 
    libc/src/netdb/gai_strerror.cpp
    libc/src/netdb/gai_strerror.h
    libc/test/src/netdb/gai_strerror_test.cpp

Modified: 
    libc/config/linux/aarch64/entrypoints.txt
    libc/config/linux/x86_64/entrypoints.txt
    libc/include/netdb.yaml
    libc/src/netdb/CMakeLists.txt
    libc/test/src/netdb/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 554513e686eab..cf6a6c2815a7b 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1404,6 +1404,7 @@ if(LLVM_LIBC_ENABLE_EXPERIMENTAL_ENTRYPOINTS)
 
       # netdb.h entrypoints
       libc.src.netdb.freeaddrinfo
+      libc.src.netdb.gai_strerror
       libc.src.netdb.getaddrinfo
 
       # regex.h entrypoints

diff  --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 43c04275ef8e4..28f2b9a480ef3 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1635,6 +1635,7 @@ if(LLVM_LIBC_ENABLE_EXPERIMENTAL_ENTRYPOINTS)
 
       # netdb.h entrypoints
       libc.src.netdb.freeaddrinfo
+      libc.src.netdb.gai_strerror
       libc.src.netdb.getaddrinfo
 
       # regex.h entrypoints

diff  --git a/libc/include/netdb.yaml b/libc/include/netdb.yaml
index b98f30fd1a2f2..6dc873ae13f11 100644
--- a/libc/include/netdb.yaml
+++ b/libc/include/netdb.yaml
@@ -33,6 +33,12 @@ functions:
     return_type: void
     arguments:
       - type: struct addrinfo *
+  - name: gai_strerror
+    standards:
+      - posix
+    return_type: const char *
+    arguments:
+      - type: int
   - name: getaddrinfo
     standards:
       - posix

diff  --git a/libc/src/netdb/CMakeLists.txt b/libc/src/netdb/CMakeLists.txt
index 5533695158291..f2a8ccb4da896 100644
--- a/libc/src/netdb/CMakeLists.txt
+++ b/libc/src/netdb/CMakeLists.txt
@@ -10,6 +10,18 @@ add_entrypoint_object(
     libc.src.__support.macros.config
 )
 
+add_entrypoint_object(
+  gai_strerror
+  SRCS
+    gai_strerror.cpp
+  HDRS
+    gai_strerror.h
+  DEPENDS
+    libc.hdr.netdb_macros
+    libc.src.__support.common
+    libc.src.__support.macros.config
+)
+
 add_entrypoint_object(
   getaddrinfo
   SRCS

diff  --git a/libc/src/netdb/gai_strerror.cpp b/libc/src/netdb/gai_strerror.cpp
new file mode 100644
index 0000000000000..378917707ceee
--- /dev/null
+++ b/libc/src/netdb/gai_strerror.cpp
@@ -0,0 +1,48 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation of gai_strerror.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/netdb/gai_strerror.h"
+#include "hdr/netdb_macros.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(const char *, gai_strerror, (int errcode)) {
+  switch (errcode) {
+  case EAI_AGAIN:
+    return "Name could not be resolved at this time";
+  case EAI_BADFLAGS:
+    return "Flags had an invalid value";
+  case EAI_FAIL:
+    return "Non-recoverable error occurred";
+  case EAI_FAMILY:
+    return "Address family not recognized";
+  case EAI_MEMORY:
+    return "Memory allocation failure";
+  case EAI_NONAME:
+    return "Name does not resolve for the supplied parameters";
+  case EAI_OVERFLOW:
+    return "Argument buffer overflowed";
+  case EAI_SERVICE:
+    return "Service not recognized for specified socket type";
+  case EAI_SOCKTYPE:
+    return "Intended socket type not recognized";
+  case EAI_SYSTEM:
+    return "System error";
+  default:
+    return "Unknown error";
+  }
+}
+
+} // namespace LIBC_NAMESPACE_DECL

diff  --git a/libc/src/netdb/gai_strerror.h b/libc/src/netdb/gai_strerror.h
new file mode 100644
index 0000000000000..8ef8b352ff582
--- /dev/null
+++ b/libc/src/netdb/gai_strerror.h
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for gai_strerror.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_NETDB_GAI_STRERROR_H
+#define LLVM_LIBC_SRC_NETDB_GAI_STRERROR_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+const char *gai_strerror(int errcode);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_NETDB_GAI_STRERROR_H

diff  --git a/libc/test/src/netdb/CMakeLists.txt b/libc/test/src/netdb/CMakeLists.txt
index bb2113840d993..608d7f38acabe 100644
--- a/libc/test/src/netdb/CMakeLists.txt
+++ b/libc/test/src/netdb/CMakeLists.txt
@@ -1,5 +1,16 @@
 add_custom_target(libc-netdb-tests)
 
+add_libc_test(
+  gai_strerror_test
+  SUITE
+    libc-netdb-tests
+  SRCS
+    gai_strerror_test.cpp
+  DEPENDS
+    libc.hdr.netdb_macros
+    libc.src.netdb.gai_strerror
+)
+
 add_libc_test(
   netdb_test
   SUITE

diff  --git a/libc/test/src/netdb/gai_strerror_test.cpp b/libc/test/src/netdb/gai_strerror_test.cpp
new file mode 100644
index 0000000000000..13ba07a2b0270
--- /dev/null
+++ b/libc/test/src/netdb/gai_strerror_test.cpp
@@ -0,0 +1,43 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for gai_strerror.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/netdb_macros.h"
+#include "src/netdb/gai_strerror.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcGaiStrerrorTest, AllKnownErrors) {
+  EXPECT_STREQ(LIBC_NAMESPACE::gai_strerror(EAI_AGAIN),
+               "Name could not be resolved at this time");
+  EXPECT_STREQ(LIBC_NAMESPACE::gai_strerror(EAI_BADFLAGS),
+               "Flags had an invalid value");
+  EXPECT_STREQ(LIBC_NAMESPACE::gai_strerror(EAI_FAIL),
+               "Non-recoverable error occurred");
+  EXPECT_STREQ(LIBC_NAMESPACE::gai_strerror(EAI_FAMILY),
+               "Address family not recognized");
+  EXPECT_STREQ(LIBC_NAMESPACE::gai_strerror(EAI_MEMORY),
+               "Memory allocation failure");
+  EXPECT_STREQ(LIBC_NAMESPACE::gai_strerror(EAI_NONAME),
+               "Name does not resolve for the supplied parameters");
+  EXPECT_STREQ(LIBC_NAMESPACE::gai_strerror(EAI_OVERFLOW),
+               "Argument buffer overflowed");
+  EXPECT_STREQ(LIBC_NAMESPACE::gai_strerror(EAI_SERVICE),
+               "Service not recognized for specified socket type");
+  EXPECT_STREQ(LIBC_NAMESPACE::gai_strerror(EAI_SOCKTYPE),
+               "Intended socket type not recognized");
+  EXPECT_STREQ(LIBC_NAMESPACE::gai_strerror(EAI_SYSTEM), "System error");
+}
+
+TEST(LlvmLibcGaiStrerrorTest, UnknownErrors) {
+  EXPECT_STREQ(LIBC_NAMESPACE::gai_strerror(100), "Unknown error");
+  EXPECT_STREQ(LIBC_NAMESPACE::gai_strerror(-1000), "Unknown error");
+}


        


More information about the libc-commits mailing list