[libc-commits] [libc] 860a07b - [libc] Add stubs for POSIX netdb.h and getaddrinfo (#219337)

via libc-commits libc-commits at lists.llvm.org
Thu Aug 27 18:48:57 PDT 2026


Author: Alexey Samsonov
Date: 2026-08-27T18:48:51-07:00
New Revision: 860a07be6da9ffe76625da2ad15e4e4e6ca6532c

URL: https://github.com/llvm/llvm-project/commit/860a07be6da9ffe76625da2ad15e4e4e6ca6532c
DIFF: https://github.com/llvm/llvm-project/commit/860a07be6da9ffe76625da2ad15e4e4e6ca6532c.diff

LOG: [libc] Add stubs for POSIX netdb.h and getaddrinfo (#219337)

* Add the `<netdb.h>` POSIX header and declare `struct addrinfo` and
`freeaddrinfo` and `getaddrinfo` methods
as defined in
https://pubs.opengroup.org/onlinepubs/9799919799/functions/getaddrinfo.html
;
* Provide Linux-specific definitions for `EAI_` macro family;
* Add header/entrypoints to the list of "experimental" (i.e. WIP)
entrypoints on Linux systems;
* Create the proxy header harness for types / Linux-specific macro.
* Provide stub implementations - no-op `freeaddrinfo` and `getaddrinfo`
that returns `EAI_SYSTEM` and sets errno to `ENOSYS`. Validate this
behavior in unit tests.

Assisted by automated tooling, human-reviewed

Added: 
    libc/hdr/netdb_macros.h
    libc/hdr/types/struct_addrinfo.h
    libc/include/llvm-libc-macros/linux/netdb-macros.h
    libc/include/llvm-libc-macros/netdb-macros.h
    libc/include/llvm-libc-types/struct_addrinfo.h
    libc/include/netdb.yaml
    libc/src/netdb/CMakeLists.txt
    libc/src/netdb/freeaddrinfo.cpp
    libc/src/netdb/freeaddrinfo.h
    libc/src/netdb/getaddrinfo.cpp
    libc/src/netdb/getaddrinfo.h
    libc/test/src/netdb/CMakeLists.txt
    libc/test/src/netdb/netdb_test.cpp

Modified: 
    libc/config/linux/aarch64/entrypoints.txt
    libc/config/linux/aarch64/headers.txt
    libc/config/linux/x86_64/entrypoints.txt
    libc/config/linux/x86_64/headers.txt
    libc/hdr/CMakeLists.txt
    libc/hdr/types/CMakeLists.txt
    libc/include/CMakeLists.txt
    libc/include/llvm-libc-macros/CMakeLists.txt
    libc/include/llvm-libc-macros/linux/CMakeLists.txt
    libc/include/llvm-libc-types/CMakeLists.txt
    libc/src/CMakeLists.txt
    libc/test/src/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 567025e62f43d..39d2588794d65 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1381,6 +1381,10 @@ if(LLVM_LIBC_ENABLE_EXPERIMENTAL_ENTRYPOINTS)
       libc.src.net.if_freenameindex
       libc.src.net.if_nameindex
 
+      # netdb.h entrypoints
+      libc.src.netdb.freeaddrinfo
+      libc.src.netdb.getaddrinfo
+
       # regex.h entrypoints
       libc.src.regex.regcomp
       libc.src.regex.regexec

diff  --git a/libc/config/linux/aarch64/headers.txt b/libc/config/linux/aarch64/headers.txt
index b80ed913c5007..848ce7b257eac 100644
--- a/libc/config/linux/aarch64/headers.txt
+++ b/libc/config/linux/aarch64/headers.txt
@@ -90,6 +90,7 @@ set(TARGET_PUBLIC_HEADERS
 
 if(LLVM_LIBC_FULL_BUILD AND LLVM_LIBC_ENABLE_EXPERIMENTAL_ENTRYPOINTS)
   list(APPEND TARGET_PUBLIC_HEADERS
+    libc.include.netdb
     libc.include.regex
     libc.include.sys_ptrace
   )

diff  --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 6a37d8b2b48d6..53f169f18ea03 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1612,6 +1612,10 @@ if(LLVM_LIBC_ENABLE_EXPERIMENTAL_ENTRYPOINTS)
       libc.src.net.if_freenameindex
       libc.src.net.if_nameindex
 
+      # netdb.h entrypoints
+      libc.src.netdb.freeaddrinfo
+      libc.src.netdb.getaddrinfo
+
       # regex.h entrypoints
       libc.src.regex.regcomp
       libc.src.regex.regexec

diff  --git a/libc/config/linux/x86_64/headers.txt b/libc/config/linux/x86_64/headers.txt
index 06ee91983ebd5..9f2c4c568b7dc 100644
--- a/libc/config/linux/x86_64/headers.txt
+++ b/libc/config/linux/x86_64/headers.txt
@@ -95,6 +95,7 @@ set(TARGET_PUBLIC_HEADERS
 
 if(LLVM_LIBC_FULL_BUILD AND LLVM_LIBC_ENABLE_EXPERIMENTAL_ENTRYPOINTS)
   list(APPEND TARGET_PUBLIC_HEADERS
+    libc.include.netdb
     libc.include.regex
     libc.include.sys_ptrace
   )

diff  --git a/libc/hdr/CMakeLists.txt b/libc/hdr/CMakeLists.txt
index dbc0a52d6414d..7f81ffd172261 100644
--- a/libc/hdr/CMakeLists.txt
+++ b/libc/hdr/CMakeLists.txt
@@ -88,6 +88,14 @@ add_proxy_header_library(
     libc.include.llvm-libc-macros.net_if_macros
 )
 
+add_proxy_header_library(
+  netdb_macros
+  HDRS
+    netdb_macros.h
+  FULL_BUILD_DEPENDS
+    libc.include.llvm-libc-macros.netdb_macros
+)
+
 add_proxy_header_library(
   netinet_in_macros
   HDRS

diff  --git a/libc/hdr/netdb_macros.h b/libc/hdr/netdb_macros.h
new file mode 100644
index 0000000000000..031772a53ad7a
--- /dev/null
+++ b/libc/hdr/netdb_macros.h
@@ -0,0 +1,27 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Proxy header for netdb macros.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_HDR_NETDB_MACROS_H
+#define LLVM_LIBC_HDR_NETDB_MACROS_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-macros/netdb-macros.h"
+
+#else // Overlay mode
+
+#include <netdb.h>
+
+#endif // LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_NETDB_MACROS_H

diff  --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt
index 40a3c197b258a..dc15960c7a7f3 100644
--- a/libc/hdr/types/CMakeLists.txt
+++ b/libc/hdr/types/CMakeLists.txt
@@ -682,6 +682,14 @@ add_proxy_header_library(
     libc.include.sys_socket
 )
 
+add_proxy_header_library(
+  struct_addrinfo
+  HDRS
+    struct_addrinfo.h
+  FULL_BUILD_DEPENDS
+    libc.include.llvm-libc-types.struct_addrinfo
+)
+
 add_proxy_header_library(
   struct_sockaddr_storage
   HDRS

diff  --git a/libc/hdr/types/struct_addrinfo.h b/libc/hdr/types/struct_addrinfo.h
new file mode 100644
index 0000000000000..7ec1a65edbfb0
--- /dev/null
+++ b/libc/hdr/types/struct_addrinfo.h
@@ -0,0 +1,27 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Proxy header for struct addrinfo.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_HDR_TYPES_STRUCT_ADDRINFO_H
+#define LLVM_LIBC_HDR_TYPES_STRUCT_ADDRINFO_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-types/struct_addrinfo.h"
+
+#else
+
+#include <netdb.h>
+
+#endif // LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_TYPES_STRUCT_ADDRINFO_H

diff  --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index dc310b7ada387..3b423c41a7cf6 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -355,6 +355,16 @@ add_header_macro(
     .llvm_libc_common_h
 )
 
+add_header_macro(
+  netdb
+  ../libc/include/netdb.yaml
+  netdb.h
+  DEPENDS
+    .llvm_libc_common_h
+    .llvm-libc-macros.netdb_macros
+    .llvm-libc-types.struct_addrinfo
+)
+
 add_header_macro(
   regex
   ../libc/include/regex.yaml

diff  --git a/libc/include/llvm-libc-macros/CMakeLists.txt b/libc/include/llvm-libc-macros/CMakeLists.txt
index b23b9626557b8..848aa2683a947 100644
--- a/libc/include/llvm-libc-macros/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/CMakeLists.txt
@@ -340,6 +340,12 @@ add_macro_header(
     net-if-macros.h
 )
 
+add_macro_header(
+  netdb_macros
+  HDR
+    netdb-macros.h
+)
+
 add_macro_header(
   sys_time_macros
   HDR

diff  --git a/libc/include/llvm-libc-macros/linux/CMakeLists.txt b/libc/include/llvm-libc-macros/linux/CMakeLists.txt
index 8fcab45d13017..477c3fd112786 100644
--- a/libc/include/llvm-libc-macros/linux/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/linux/CMakeLists.txt
@@ -76,6 +76,12 @@ add_header(
     net-if-macros.h
 )
 
+add_header(
+  netdb_macros
+  HDR
+    netdb-macros.h
+)
+
 add_header(
   sys_wait_macros
   HDR

diff  --git a/libc/include/llvm-libc-macros/linux/netdb-macros.h b/libc/include/llvm-libc-macros/linux/netdb-macros.h
new file mode 100644
index 0000000000000..7bc06982f4553
--- /dev/null
+++ b/libc/include/llvm-libc-macros/linux/netdb-macros.h
@@ -0,0 +1,28 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Macros defined in netdb.h header file for Linux.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_MACROS_LINUX_NETDB_MACROS_H
+#define LLVM_LIBC_MACROS_LINUX_NETDB_MACROS_H
+
+#define EAI_BADFLAGS -1
+#define EAI_NONAME -2
+#define EAI_AGAIN -3
+#define EAI_FAIL -4
+#define EAI_FAMILY -6
+#define EAI_SOCKTYPE -7
+#define EAI_SERVICE -8
+#define EAI_MEMORY -10
+#define EAI_SYSTEM -11
+#define EAI_OVERFLOW -12
+
+#endif // LLVM_LIBC_MACROS_LINUX_NETDB_MACROS_H

diff  --git a/libc/include/llvm-libc-macros/netdb-macros.h b/libc/include/llvm-libc-macros/netdb-macros.h
new file mode 100644
index 0000000000000..46804d0eaec65
--- /dev/null
+++ b/libc/include/llvm-libc-macros/netdb-macros.h
@@ -0,0 +1,21 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Macros defined in netdb.h header file.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_MACROS_NETDB_MACROS_H
+#define LLVM_LIBC_MACROS_NETDB_MACROS_H
+
+#ifdef __linux__
+#include "linux/netdb-macros.h"
+#endif
+
+#endif // LLVM_LIBC_MACROS_NETDB_MACROS_H

diff  --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index 6d9733b124e02..02b9adf88bcac 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -256,6 +256,7 @@ add_header(wctype_t HDR wctype_t.h)
 add_header(sa_family_t HDR sa_family_t.h)
 add_header(socklen_t HDR socklen_t.h)
 add_header(struct_sockaddr HDR struct_sockaddr.h DEPENDS .sa_family_t)
+add_header(struct_addrinfo HDR struct_addrinfo.h DEPENDS .socklen_t .struct_sockaddr)
 add_header(struct_sockaddr_in HDR struct_sockaddr_in.h DEPENDS .in_port_t .sa_family_t .struct_in_addr)
 add_header(
   struct_sockaddr_in6

diff  --git a/libc/include/llvm-libc-types/struct_addrinfo.h b/libc/include/llvm-libc-types/struct_addrinfo.h
new file mode 100644
index 0000000000000..0c0233982b19e
--- /dev/null
+++ b/libc/include/llvm-libc-types/struct_addrinfo.h
@@ -0,0 +1,31 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Definition of struct addrinfo.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_TYPES_STRUCT_ADDRINFO_H
+#define LLVM_LIBC_TYPES_STRUCT_ADDRINFO_H
+
+#include "socklen_t.h"
+#include "struct_sockaddr.h"
+
+struct addrinfo {
+  int ai_flags;
+  int ai_family;
+  int ai_socktype;
+  int ai_protocol;
+  socklen_t ai_addrlen;
+  struct sockaddr *ai_addr;
+  char *ai_canonname;
+  struct addrinfo *ai_next;
+};
+
+#endif // LLVM_LIBC_TYPES_STRUCT_ADDRINFO_H

diff  --git a/libc/include/netdb.yaml b/libc/include/netdb.yaml
new file mode 100644
index 0000000000000..b98f30fd1a2f2
--- /dev/null
+++ b/libc/include/netdb.yaml
@@ -0,0 +1,44 @@
+header: netdb.h
+standards:
+  - posix
+macros:
+  - macro_name: EAI_AGAIN
+    macro_header: netdb-macros.h
+  - macro_name: EAI_BADFLAGS
+    macro_header: netdb-macros.h
+  - macro_name: EAI_FAIL
+    macro_header: netdb-macros.h
+  - macro_name: EAI_FAMILY
+    macro_header: netdb-macros.h
+  - macro_name: EAI_MEMORY
+    macro_header: netdb-macros.h
+  - macro_name: EAI_NONAME
+    macro_header: netdb-macros.h
+  - macro_name: EAI_SERVICE
+    macro_header: netdb-macros.h
+  - macro_name: EAI_SOCKTYPE
+    macro_header: netdb-macros.h
+  - macro_name: EAI_SYSTEM
+    macro_header: netdb-macros.h
+  - macro_name: EAI_OVERFLOW
+    macro_header: netdb-macros.h
+types:
+  - type_name: struct_addrinfo
+enums: []
+objects: []
+functions:
+  - name: freeaddrinfo
+    standards:
+      - posix
+    return_type: void
+    arguments:
+      - type: struct addrinfo *
+  - name: getaddrinfo
+    standards:
+      - posix
+    return_type: int
+    arguments:
+      - type: const char *__restrict
+      - type: const char *__restrict
+      - type: const struct addrinfo *__restrict
+      - type: struct addrinfo **__restrict

diff  --git a/libc/src/CMakeLists.txt b/libc/src/CMakeLists.txt
index 4e7aaa1112ed7..1458658e360b7 100644
--- a/libc/src/CMakeLists.txt
+++ b/libc/src/CMakeLists.txt
@@ -49,6 +49,7 @@ endif()
 add_subdirectory(assert)
 add_subdirectory(compiler)
 add_subdirectory(locale)
+add_subdirectory(netdb)
 add_subdirectory(nl_types)
 add_subdirectory(search)
 add_subdirectory(regex)

diff  --git a/libc/src/netdb/CMakeLists.txt b/libc/src/netdb/CMakeLists.txt
new file mode 100644
index 0000000000000..5533695158291
--- /dev/null
+++ b/libc/src/netdb/CMakeLists.txt
@@ -0,0 +1,26 @@
+add_entrypoint_object(
+  freeaddrinfo
+  SRCS
+    freeaddrinfo.cpp
+  HDRS
+    freeaddrinfo.h
+  DEPENDS
+    libc.hdr.types.struct_addrinfo
+    libc.src.__support.common
+    libc.src.__support.macros.config
+)
+
+add_entrypoint_object(
+  getaddrinfo
+  SRCS
+    getaddrinfo.cpp
+  HDRS
+    getaddrinfo.h
+  DEPENDS
+    libc.hdr.errno_macros
+    libc.hdr.netdb_macros
+    libc.hdr.types.struct_addrinfo
+    libc.src.__support.common
+    libc.src.__support.macros.config
+    libc.src.errno.errno
+)

diff  --git a/libc/src/netdb/freeaddrinfo.cpp b/libc/src/netdb/freeaddrinfo.cpp
new file mode 100644
index 0000000000000..9a0c2b060fdbb
--- /dev/null
+++ b/libc/src/netdb/freeaddrinfo.cpp
@@ -0,0 +1,27 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 freeaddrinfo.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/netdb/freeaddrinfo.h"
+#include "hdr/types/struct_addrinfo.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(void, freeaddrinfo,
+                   ([[maybe_unused]] struct addrinfo * res)) {
+  // TODO: Implement freeaddrinfo.
+  return;
+}
+
+} // namespace LIBC_NAMESPACE_DECL

diff  --git a/libc/src/netdb/freeaddrinfo.h b/libc/src/netdb/freeaddrinfo.h
new file mode 100644
index 0000000000000..b5c6fff778404
--- /dev/null
+++ b/libc/src/netdb/freeaddrinfo.h
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 freeaddrinfo.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_NETDB_FREEADDRINFO_H
+#define LLVM_LIBC_SRC_NETDB_FREEADDRINFO_H
+
+#include "hdr/types/struct_addrinfo.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+void freeaddrinfo(struct addrinfo *res);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_NETDB_FREEADDRINFO_H

diff  --git a/libc/src/netdb/getaddrinfo.cpp b/libc/src/netdb/getaddrinfo.cpp
new file mode 100644
index 0000000000000..c044e5023ca60
--- /dev/null
+++ b/libc/src/netdb/getaddrinfo.cpp
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 getaddrinfo.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/netdb/getaddrinfo.h"
+#include "hdr/errno_macros.h"
+#include "hdr/netdb_macros.h"
+#include "hdr/types/struct_addrinfo.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, getaddrinfo,
+                   ([[maybe_unused]] const char *__restrict nodename,
+                    [[maybe_unused]] const char *__restrict servname,
+                    [[maybe_unused]] const struct addrinfo *__restrict hints,
+                    [[maybe_unused]] struct addrinfo **__restrict res)) {
+  // TODO: Implement getaddrinfo.
+  libc_errno = ENOSYS;
+  return EAI_SYSTEM;
+}
+
+} // namespace LIBC_NAMESPACE_DECL

diff  --git a/libc/src/netdb/getaddrinfo.h b/libc/src/netdb/getaddrinfo.h
new file mode 100644
index 0000000000000..713c052b91f41
--- /dev/null
+++ b/libc/src/netdb/getaddrinfo.h
@@ -0,0 +1,29 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 getaddrinfo.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_NETDB_GETADDRINFO_H
+#define LLVM_LIBC_SRC_NETDB_GETADDRINFO_H
+
+#include "hdr/types/struct_addrinfo.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int getaddrinfo(const char *__restrict nodename,
+                const char *__restrict servname,
+                const struct addrinfo *__restrict hints,
+                struct addrinfo **__restrict res);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_NETDB_GETADDRINFO_H

diff  --git a/libc/test/src/CMakeLists.txt b/libc/test/src/CMakeLists.txt
index cdd262aa36473..94a5c79bb6eb9 100644
--- a/libc/test/src/CMakeLists.txt
+++ b/libc/test/src/CMakeLists.txt
@@ -116,6 +116,7 @@ add_subdirectory(assert)
 add_subdirectory(compiler)
 add_subdirectory(dirent)
 add_subdirectory(locale)
+add_subdirectory(netdb)
 add_subdirectory(nl_types)
 add_subdirectory(signal)
 add_subdirectory(regex)

diff  --git a/libc/test/src/netdb/CMakeLists.txt b/libc/test/src/netdb/CMakeLists.txt
new file mode 100644
index 0000000000000..bb2113840d993
--- /dev/null
+++ b/libc/test/src/netdb/CMakeLists.txt
@@ -0,0 +1,18 @@
+add_custom_target(libc-netdb-tests)
+
+add_libc_test(
+  netdb_test
+  SUITE
+    libc-netdb-tests
+  SRCS
+    netdb_test.cpp
+  DEPENDS
+    libc.hdr.errno_macros
+    libc.hdr.netdb_macros
+    libc.hdr.types.struct_addrinfo
+    libc.src.errno.errno
+    libc.src.netdb.freeaddrinfo
+    libc.src.netdb.getaddrinfo
+    libc.test.UnitTest.ErrnoCheckingTest
+    libc.test.UnitTest.ErrnoSetterMatcher
+)

diff  --git a/libc/test/src/netdb/netdb_test.cpp b/libc/test/src/netdb/netdb_test.cpp
new file mode 100644
index 0000000000000..b25a3cdc2a5f0
--- /dev/null
+++ b/libc/test/src/netdb/netdb_test.cpp
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 getaddrinfo and freeaddrinfo.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "hdr/netdb_macros.h"
+#include "hdr/types/struct_addrinfo.h"
+#include "src/netdb/freeaddrinfo.h"
+#include "src/netdb/getaddrinfo.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+using LlvmLibcNetdbTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcNetdbTest, GetAddrInfoReturnsEaiSystem) {
+  struct addrinfo *res = nullptr;
+  ASSERT_THAT(LIBC_NAMESPACE::getaddrinfo("localhost", nullptr, nullptr, &res),
+              Fails(ENOSYS, EAI_SYSTEM));
+  EXPECT_EQ(res, nullptr);
+
+  // freeaddrinfo should do nothing and not crash.
+  LIBC_NAMESPACE::freeaddrinfo(res);
+}


        


More information about the libc-commits mailing list