This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit c892f3d13fcc6eed4c1e505d80afe1fba695bb67
Author: Marco Casaroli <[email protected]>
AuthorDate: Tue Nov 10 23:53:32 2026 +0100

    libs/libc/net: Accept an IPv4 tail in inet_pton(AF_INET6).
    
    inet_pton(AF_INET6) returned 0 for an address that ends in the IPv4
    dotted-decimal form, for example "::ffff:127.0.0.1" or
    "1:2:3:4:5:6:1.2.3.4": the parser took '.' as an illegal character.
    POSIX and RFC 4291 section 2.2 (form 3) define this form, and the
    comment of inet_pton() describes it.
    
    When a field contains a '.', convert the rest of the string from the
    start of that field with inet_ipv4_pton() into the last 32 bits.  The
    IPv4 part must end the string and it must fit: "::1.2.3.4:1",
    "1.2.3.4::", "::1.2.3", "::256.1.1.1" and "1:2:3:4:5:6:7:1.2.3.4"
    stay invalid.  inet_ipv4_pton() is now also built when only IPv6 is
    enabled.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 libs/libc/net/lib_inetpton.c | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/libs/libc/net/lib_inetpton.c b/libs/libc/net/lib_inetpton.c
index d45609d413a..f33444f3926 100644
--- a/libs/libc/net/lib_inetpton.c
+++ b/libs/libc/net/lib_inetpton.c
@@ -92,7 +92,8 @@
  *
  ****************************************************************************/
 
-#if defined(CONFIG_NET_IPv4) || defined(CONFIG_LIBC_IPv4_ADDRCONV)
+#if defined(CONFIG_NET_IPv4) || defined(CONFIG_LIBC_IPv4_ADDRCONV) || \
+    defined(CONFIG_NET_IPv6) || defined(CONFIG_LIBC_IPv6_ADDRCONV)
 static int inet_ipv4_pton(FAR const char *src, FAR void *dest)
 {
   size_t srcoffset;
@@ -205,6 +206,7 @@ static int inet_ipv4_pton(FAR const char *src, FAR void 
*dest)
 #if defined(CONFIG_NET_IPv6) || defined(CONFIG_LIBC_IPv6_ADDRCONV)
 static int inet_ipv6_pton(FAR const char *src, FAR void *dest)
 {
+  FAR const char *field;
   FAR uint8_t *addr = dest;
   uint8_t ip[sizeof(struct in6_addr)];
   unsigned int value;
@@ -227,6 +229,7 @@ static int inet_ipv6_pton(FAR const char *src, FAR void 
*dest)
       src++;
     }
 
+  field   = src;
   value   = 0;
   nbytes  = 0;
   ndigits = 0;
@@ -280,6 +283,23 @@ static int inet_ipv6_pton(FAR const char *src, FAR void 
*dest)
             {
               break;
             }
+
+          field = src;
+        }
+      else if (ch == '.')
+        {
+          /* The last 32 bits in the IPv4 dotted-decimal form: the field is
+           * the start of it, and it must end the string.
+           */
+
+          if (nbytes + 4 > sizeof(ip) ||
+              inet_ipv4_pton(field, &ip[nbytes]) != 1)
+            {
+              return 0;
+            }
+
+          nbytes += 4;
+          break;
         }
       else
         {

Reply via email to