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 f0c0ebc6395fff66251520005117cc59ce320afa Author: Marco Casaroli <[email protected]> AuthorDate: Tue Nov 10 23:52:24 2026 +0100 libs/libc/net: Fix the field count check in inet_pton(AF_INET6). inet_pton(AF_INET6) accepted many strings that are not IPv6 addresses: fewer than eight fields without "::" ("1:2:3"), eight fields and "::" ("1:2:3:4::5:6:7:8"), two "::" ("1::2::3"), ":::", and a single ':' at the start or the end (":1::2", "1::2:"). A ninth field ("1:2:3:4:5:6:7:8:9") was written after the end of a local array of 16 bytes before the count check rejected it. The parser counted fields before and after "::" in two arrays, but it checked the count only at the end of the string, and it took any empty field as the "::". Parse the fields in one pass: check that each field fits before it is stored, accept one "::" only, reject a single ':' at the start or the end, and at the end require eight fields without "::", or fewer than eight with it. The fields after "::" go to the end of the address. The result now agrees with glibc and the macOS libc for each case above and for the valid forms. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]> --- libs/libc/net/lib_inetpton.c | 156 ++++++++++++++++++++----------------------- 1 file changed, 71 insertions(+), 85 deletions(-) diff --git a/libs/libc/net/lib_inetpton.c b/libs/libc/net/lib_inetpton.c index d26ef5b8c7d..d45609d413a 100644 --- a/libs/libc/net/lib_inetpton.c +++ b/libs/libc/net/lib_inetpton.c @@ -48,6 +48,7 @@ #include <stdlib.h> #include <string.h> #include <assert.h> +#include <ctype.h> #include <errno.h> #include <arpa/inet.h> @@ -204,114 +205,79 @@ 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) { - size_t srcoffset; - size_t numoffset; - long value; - int nsep; - int nrsep; - uint8_t ch; - char numstr[5]; + FAR uint8_t *addr = dest; uint8_t ip[sizeof(struct in6_addr)]; - uint8_t rip[sizeof(struct in6_addr)]; - bool rtime; + unsigned int value; + size_t nbytes; + int ndigits; + int gap; + uint8_t ch; memset(dest, 0, sizeof(struct in6_addr)); - srcoffset = 0; - numoffset = 0; - nsep = 0; - nrsep = 0; - rtime = false; + /* The address can start with "::", but not with one ':' */ - for (; ; ) + if (src[0] == ':') { - ch = (uint8_t)src[srcoffset++]; - - if (ch == ':' || ch == '\0') + if (src[1] != ':') { - if (ch == ':' && (nsep + nrsep) >= 8) - { - /* Too many separators */ - - break; - } - - if (ch != '\0' && numoffset < 1) - { - /* Empty numeric string */ - - if (rtime && nrsep > 1) - { - /* dup simple */ + return 0; + } - break; - } + src++; + } - numoffset = 0; - rtime = true; - continue; - } + value = 0; + nbytes = 0; + ndigits = 0; + gap = -1; - numstr[numoffset] = '\0'; - numoffset = 0; + for (; ; ) + { + ch = *src++; - value = strtol(numstr, NULL, 16); - if (value < 0 || value > 0xffff) + if (isxdigit(ch)) + { + if (++ndigits > 4) { - /* Out of range value */ + /* More than four digits in a field */ - break; + return 0; } - if (!rtime) - { - ip[(nsep << 1) + 0] = (uint8_t)((value >> 8) & 0xff); - ip[(nsep << 1) + 1] = (uint8_t)((value >> 0) & 0xff); - nsep++; - } - else - { - rip[(nrsep << 1) + 0] = (uint8_t)((value >> 8) & 0xff); - rip[(nrsep << 1) + 1] = (uint8_t)((value >> 0) & 0xff); - nrsep++; - } - - if (ch == '\0' /* || ch == '/' */) + value <<= 4; + value |= isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10; + } + else if (ch == ':' || ch == '\0') + { + if (ndigits > 0) { - if ((nsep <= 1 && nrsep <= 0) || - (nsep + nrsep) < 1 || - (nsep + nrsep) > 8) + if (nbytes + 2 > sizeof(ip) || (ch == ':' && *src == '\0')) { - /* Separator count problem */ + /* Too many fields, or a ':' at the end */ - break; + return 0; } - if (nsep > 0) + ip[nbytes++] = value >> 8; + ip[nbytes++] = value & 0xff; + value = 0; + ndigits = 0; + } + else if (ch == ':') + { + if (gap >= 0) { - memcpy(dest, &ip[0], nsep << 1); - } + /* More than one "::" */ - if (nrsep > 0) - { - memcpy((FAR uint8_t *)dest + - (16 - (nrsep << 1)), &rip[0], nrsep << 1); + return 0; } - /* Return 1 if the conversion succeeds */ - - return 1; + gap = nbytes; } - } - else if ((ch >= '0' && ch <= '9') || - (ch >= 'a' && ch <= 'f') || - (ch >= 'A' && ch <= 'F')) - { - numstr[numoffset++] = ch; - if (numoffset >= 5) - { - /* Numeric string is too long */ + if (ch == '\0') + { break; } } @@ -319,13 +285,33 @@ static int inet_ipv6_pton(FAR const char *src, FAR void *dest) { /* Illegal character */ - break; + return 0; } } - /* Return zero if there is any problem parsing the input */ + /* Without "::" the address must have eight fields. The "::" must stand + * for one zero field at least. + */ - return 0; + if (gap < 0) + { + if (nbytes != sizeof(ip)) + { + return 0; + } + + gap = nbytes; + } + else if (nbytes == sizeof(ip)) + { + return 0; + } + + /* The fields after "::" go to the end of the address */ + + memcpy(addr, ip, gap); + memcpy(&addr[sizeof(ip) - (nbytes - gap)], &ip[gap], nbytes - gap); + return 1; } #endif
