casaroli opened a new pull request, #22434: URL: https://github.com/apache/nuttx/pull/22434
## Summary `getsockname()` and `getpeername()` of an `AF_INET` or `AF_INET6` socket failed with `ENOSYS` when the buffer of the caller was shorter than `struct sockaddr_in` or `struct sockaddr_in6`. A comment in the code said that the truncation was not implemented. POSIX says that the stored address is truncated in this case, and Linux does so. Now `ipv4_getsockname()`, `ipv4_getpeername()`, `ipv6_getsockname()` and `ipv6_getpeername()` make the whole address in a local structure, copy as much of it as the buffer holds, and set `*addrlen` to the length of the whole address, as Linux does (getsockname(2): "addrlen will return a value greater than was supplied to the call"). So the caller sees the truncation. The local structure starts as zeros, so `sin6_flowinfo` and `sin6_scope_id` are now zero; before, they kept what the buffer of the caller held. The fix is in the four inet functions, as local, Bluetooth, IEEE 802.15.4 and CAN ISO-TP sockets already truncate in their own functions; a common copy in `net/socket` would put a `struct sockaddr_storage` on the stack of every call. The first commit only fixes the indentation of one block in `ipv4_getsockname()` (nxstyle). ## Impact A short buffer now gets the truncated address and a return value of 0 instead of `ENOSYS`. A full buffer gets the same address as before, with `sin6_flowinfo` and `sin6_scope_id` set to zero. No API, ABI or Kconfig change. The same in all build modes and on all architectures. ## Testing New cases `test_udp_name_short4` and `test_udp_name_short6` in `cmocka_net_udp` (they will follow in a separate apps PR): a connected UDP socket, `getsockname()` and `getpeername()` into 8 bytes. On qemu-armv8a:nsh (flat) with TCP, UDP, IPv4, IPv6, loopback and local sockets, CI image, QEMU TCG: | | master | this PR | |---|---|---| | test_udp_name_short4, test_udp_name_short6 | FAILED (`ENOSYS`) | PASSED | | the other cases of cmocka_net_udp and cmocka_net_local_dgram | 24 + 91 PASSED | 24 + 91 PASSED | Builds with `-Werror` in the CI image, with the same network options: qemu-armv8a:nsh (flat), qemu-armv8a:knsh (kernel, with the apps), rv-virt:netnsh64 (flat), rv-virt:knetnsh64 (kernel, with the apps). -- 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]
