[libc-commits] [libc] Implement inet_pton (PR #224644)

Pavel Labath via libc-commits libc-commits at lists.llvm.org
Mon Sep 21 01:28:12 PDT 2026


================
@@ -0,0 +1,72 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// This file contains the implementation for inet_pton
+///
+//===----------------------------------------------------------------------===//
+
+#include "inet_pton.h"
+
+#include "hdr/errno_macros.h"
+#include "hdr/stdint_proxy.h"
+#include "hdr/sys_socket_macros.h"
+#include "hdr/types/struct_in6_addr.h"
+#include "hdr/types/struct_in_addr.h"
+#include "src/__support/CPP/string_view.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/null_check.h"
+#include "src/__support/str_to_integer.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, inet_pton,
+                   (int af, const char *__restrict src, void *__restrict dst)) {
+  LIBC_CRASH_ON_NULLPTR(src);
+  LIBC_CRASH_ON_NULLPTR(dst);
+  if (af == AF_INET6) {
+    return 0;
+  } else if (af != AF_INET) {
+    libc_errno = EAFNOSUPPORT;
+    return -1;
+  }
+  uint8_t bytes[4];
+  size_t start = 0;
+  cpp::string_view str(src);
+  size_t i{0};
+  for (; i < 4; ++i) {
+    size_t end = str.find_first_of('.', start);
+    if (i < 3 && start == cpp::string_view::npos)
----------------
labath wrote:

Did you mean `end`? Are we missing a test that would catch this?

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


More information about the libc-commits mailing list