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

Pavel Labath via libc-commits libc-commits at lists.llvm.org
Tue Sep 22 02:32:32 PDT 2026


================
@@ -26,8 +27,48 @@
 #include "src/string/memory_utils/inline_memcpy.h"
 
 namespace LIBC_NAMESPACE_DECL {
+
 namespace net {
 
+int inet_pton_v4(cpp::string_view src, void *dst) {
+  uint8_t bytes[4];
+  size_t idx = 0;
+  uint32_t current_val = 0;
+  size_t digits_in_octet = 0;
+
+  for (char c : src) {
+    if (c >= '0' && c <= '9') {
+      // Reject octals and leading zeros
+      if (digits_in_octet > 0 && current_val == 0)
+        return 0;
+
+      current_val = current_val * 10 + static_cast<uint32_t>(c - '0');
+      if (current_val > 255)
+        return 0;
+
+      ++digits_in_octet;
+    } else if (c == '.') {
+      if (digits_in_octet == 0 || idx == 3)
+        return 0; // Empty part or too many dots
+
+      bytes[idx++] = static_cast<uint8_t>(current_val);
+      current_val = 0;
+      digits_in_octet = 0;
+    } else {
+      return 0; // Not ASCII
----------------
labath wrote:

```suggestion
      return 0; // Invalid character
```

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


More information about the libc-commits mailing list