[libc-commits] [libc] [libc] Add inet_ntop (PR #204143)

Pavel Labath via libc-commits libc-commits at lists.llvm.org
Tue Jun 23 06:46:04 PDT 2026


================
@@ -0,0 +1,255 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 inet_ntop.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "hdr/sys_socket_macros.h"
+#include "hdr/types/struct_in6_addr.h"
+#include "hdr/types/struct_in_addr.h"
+#include "src/__support/endian_internal.h"
+#include "src/arpa/inet/inet_ntop.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcInetNtopTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcInetNtopTest, InvalidFamily) {
+  char buf[64];
+  struct in_addr addr = {0};
+  ASSERT_EQ(static_cast<const char *>(nullptr),
+            LIBC_NAMESPACE::inet_ntop(AF_INET + AF_INET6 + 1, &addr, buf,
+                                      sizeof(buf)));
+  ASSERT_ERRNO_EQ(EAFNOSUPPORT);
+}
+
+static void *ipv4(uint8_t a, uint8_t b, uint8_t c, uint8_t d) {
+  static struct in_addr addr;
+  addr.s_addr = LIBC_NAMESPACE::Endian::to_big_endian(
+      static_cast<uint32_t>(a) << 24 | static_cast<uint32_t>(b) << 16 |
+      static_cast<uint32_t>(c) << 8 | static_cast<uint32_t>(d));
+  return &addr;
+}
----------------
labath wrote:

Unfortunately that triggers an error about taking the address of a temporary (technically, it seems to be a warning, but it's a strange one that's -Werror by default and not disabled by `-w`).

I can work around it with something like `template<typename T> T* as_ptr(T&& obj) { return &obj; }`, but I'd rather not do that since the purpose of this function was to be concise.

https://github.com/llvm/llvm-project/pull/204143


More information about the libc-commits mailing list