It would be a bug in getsockname(2) if it returns a result that is smaller than the returned socket address family. For example, if it indicates that the result is less than sizeof(sockaddr_in6) and sets sa_family equal to AF_INET6 in the output.
In other words, the name->sa_family in the output should be consistent with the returned *name_len.
The current code could fail to do that if:
- the caller provides sockaddr_in6and an input value of*name_len=28
- ConsumeRandomLengthByteVector()returns a vector of- 20bytes. Then the code would only set the first- 20bytes in- name.
- name->sa_familyfrom the fuzz data ends up being- AF_INET6.
To produce consistent *name_len and name->sa_family, return one of AF_INET, AF_INET6 or AF_UNIX for family with the corresponding *name_len.
For reference:
0sizeof(sockaddr) = 16
1sizeof(sockaddr_in) = 16
2sizeof(sockaddr_in6) = 28
3sizeof(sockaddr_un) = 110 on Linux, 106 on FreeBSD (unix socket)
4sizeof(sockaddr_storage) = 128
https://www.man7.org/linux/man-pages/man3/sockaddr.3type.html