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 cb8f6a7f58e3719eca747e6763218a8bca87286e Author: Marco Casaroli <[email protected]> AuthorDate: Thu Nov 26 17:03:39 2026 +0100 net/can: Truncate the address of getsockname() of a CAN_RAW socket. getsockname() of a CAN_RAW socket failed with EINVAL when the buffer of the caller was shorter than struct sockaddr_can. A comment said that the truncation was not implemented. POSIX says that the stored address is truncated in this case, and Linux does so. Now the function makes the address in a local structure, copies as much of it as the buffer holds, and sets *addrlen to the length of the whole address, as Linux does. So the caller sees the truncation. CAN ISO-TP sockets already truncate the address. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]> --- net/can/can_raw.c | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/net/can/can_raw.c b/net/can/can_raw.c index c33984c2c69..18673d5edfc 100644 --- a/net/can/can_raw.c +++ b/net/can/can_raw.c @@ -35,6 +35,7 @@ #include <stdbool.h> #include <stdint.h> #include <string.h> +#include <sys/param.h> #include <sys/socket.h> #include <sys/time.h> #include <sys/types.h> @@ -2231,32 +2232,23 @@ static int can_raw_getsockname(FAR struct socket *psock, FAR struct sockaddr *addr, FAR socklen_t *addrlen) { - FAR struct sockaddr_can *canaddr; FAR struct can_conn_s *conn; - - /* Check if enough space has been provided for the full address */ - - if (*addrlen < sizeof(struct sockaddr_can)) - { - /* This function is supposed to return the partial address if - * a smaller buffer has been provided. This support has not - * been implemented. - */ - - return -EINVAL; - } + struct sockaddr_can canaddr; conn = psock->s_conn; /* Return the address information in the address structure */ - canaddr = (FAR struct sockaddr_can *)addr; - memset(canaddr, 0, sizeof(struct sockaddr_can)); + memset(&canaddr, 0, sizeof(canaddr)); + canaddr.can_family = AF_CAN; + canaddr.can_ifindex = can_get_ifindex(conn); - canaddr->can_family = AF_CAN; - canaddr->can_ifindex = can_get_ifindex(conn); + /* Copy as much of the address as the buffer holds. *addrlen gives the + * length of the whole address, so the caller sees a truncation. + */ - *addrlen = sizeof(struct sockaddr_can); + memcpy(addr, &canaddr, MIN(*addrlen, sizeof(canaddr))); + *addrlen = sizeof(canaddr); return OK; }
