casaroli opened a new pull request, #21656: URL: https://github.com/apache/nuttx/pull/21656
## Summary `inet_pton(AF_INET6)` has two problems. It returns 0 for an address that ends in the IPv4 dotted-decimal form, for example `"::ffff:127.0.0.1"`, `"::1.2.3.4"` or `"1:2:3:4:5:6:1.2.3.4"`: the parser takes `'.'` as an illegal character. POSIX and RFC 4291 section 2.2 (form 3) define this form, and the comment of `inet_pton()` describes it. And it returns 1 for many strings that are not IPv6 addresses: `"1:2:3"` (fewer than eight fields and no `"::"`), `"1:2:3:4::5:6:7:8"` (eight fields and `"::"`), `"1::2::3"`, `":::"`, `":1::2"`, `"1::2:"`. A ninth field (`"1:2:3:4:5:6:7:8:9"`) is written after the end of a local array of 16 bytes before the count check rejects the string. The parser counted the fields before and after `"::"` in two arrays, took any empty field as the `"::"`, and checked the count only at the end of the string. A local fix of each case would touch most lines of the function, so the first fix replaces the loop of `inet_ipv6_pton()` with a one-pass parser: 1. `libs/libc/net: Fix the indentation of the switch in inet_pton().` Whitespace only: nxstyle reports the case labels of `inet_pton()`; the error is older than this PR. 2. `libs/libc/net: Fix the field count check in inet_pton(AF_INET6).` Each field is checked before it is stored, one `"::"` only, no single `':'` at the start or the end; at the end, eight fields without `"::"`, or fewer than eight with it. 3. `libs/libc/net: Accept an IPv4 tail in inet_pton(AF_INET6).` When a field contains a `'.'`, the rest of the string from the start of that field goes to `inet_ipv4_pton()` and gives the last 32 bits. The IPv4 part must end the string and fit in the address. `inet_ipv4_pton()` is now also built when only IPv6 is enabled. ## Impact - `inet_pton(AF_INET6)` now accepts the RFC 4291 form 3, and `getaddrinfo()`, `gethostbyname()` and the hosts file parser, which call it, accept these addresses too. - Strings that are not IPv6 addresses now return 0. A program that passed such a string got a wrong address before. - The result is the same as glibc 2.43 (Fedora) for all cases, except the IPv4 part with a leading zero (`"::ffff:01.2.3.4"`): glibc rejects it, NuttX accepts it, as `inet_pton(AF_INET)` of NuttX and the macOS libc do. The macOS libc also accepts fields of more than four digits with leading zeros (`"::07552"`); NuttX and glibc reject them. - `inet_ntop()` does not change. It prints a v4-mapped address as `::ffff:7f00:1`, and glibc prints `::ffff:127.0.0.1`. That is a different change, not in this PR. - libc code: the same for flat, protected and kernel builds and for every architecture. No new Kconfig option. - Size of lib_inetpton.o (text) with IPv6 only (`LIBC_IPv6_ADDRCONV=y`), where `inet_ipv4_pton()` is now also built: 665 to 825 bytes on mps2-an500 (Cortex-M7, arm-none-eabi-gcc 15.3), 515 to 691 bytes on qemu-armv8a (aarch64-none-elf-gcc 14.2). - Documentation/: the comment of `inet_pton()` already describes the three forms; Documentation/ lists only that `inet_pton()` is supported. No documentation change. ## Testing New test apache/nuttx-apps#NNNN (`testing/libc/inetpton`): 13 valid addresses (forms 1, 2 and 3) with the expected bytes, and 35 strings that must return 0. Host check first: the test passes 48 of 48 against glibc 2.43 (Fedora) and the macOS libc. A host harness also compared 2,000,000 random strings (random characters, and random addresses built from valid fields, `"::"` and IPv4 parts) between this `inet_pton()` and glibc 2.43: no difference. On NuttX, master 80a052ff9cc vs this PR, apps master ee3d67104 + the test, `CONFIG_LIBC_IPv6_ADDRCONV=y`, `EXTRAFLAGS="-Wno-cpp -Werror"`: | Configuration | master | this PR | ostest | |---|---|---|---| | qemu-armv8a:knsh (kernel, arm64, QEMU HVF) | 19 of 48 fail | 48 pass | status 0 (both) | | qemu-armv8a:nsh (flat, arm64, QEMU HVF) | 19 of 48 fail | 48 pass | status 0 (both) | | qemu-armv8a:nsh (flat), CMake | not run | 48 pass | status 0 | | mps2-an500:nsh (flat, Cortex-M7, 32-bit, QEMU TCG) | 19 of 48 fail | 48 pass | stops at the same assertion in both (perf_gettime.c:132, QEMU has no DWT) | | qemu-armv8a:netnsh + `NET_IPv6`, `NET_ICMPv6_SOCKET` (IPv4 and IPv6) | not run | builds, no warnings | not run | On master the test fails 19 cases: the 6 form-3 addresses return 0, and 13 invalid strings return 1. `"1:2:3:4:5:6:7:8:9"` returns 0 on master too, after the write past the end of the array. All builds have no warnings with `-Werror`. `tools/checkpatch.sh -c -u -m -g origin/master..HEAD` passes. Not run: a protected build, SMP, other architectures. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
