Skip to content

Commit 852a1e8

Browse files
rluboskartben
authored andcommitted
net: sockets: getnameinfo: Cast to sockaddr_in6 instead of sockaddr_in
As explained in the comment in the code, both structs have the same offsets for it's fields, but sockaddr_in is smaller, hence it can confuse static analyzer, giving warnings about potential out-of-bound access. Therefore, cast to sockaddr_in6 instead to avoid the warning. Signed-off-by: Robert Lubos <[email protected]>
1 parent 487386e commit 852a1e8

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

subsys/net/lib/sockets/getnameinfo.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ int zsock_getnameinfo(const struct net_sockaddr *addr, net_socklen_t addrlen,
1313
char *serv, net_socklen_t servlen, int flags)
1414
{
1515
/* Both net_sockaddr_in & _in6 have same offsets for family and address. */
16-
struct net_sockaddr_in *a = (struct net_sockaddr_in *)addr;
16+
const struct net_sockaddr_in6 *a = (const struct net_sockaddr_in6 *)addr;
1717

1818
if (host != NULL) {
19-
void *res = zsock_inet_ntop(a->sin_family, &a->sin_addr,
19+
void *res = zsock_inet_ntop(a->sin6_family, &a->sin6_addr,
2020
host, hostlen);
2121

2222
if (res == NULL) {
@@ -25,7 +25,7 @@ int zsock_getnameinfo(const struct net_sockaddr *addr, net_socklen_t addrlen,
2525
}
2626

2727
if (serv != NULL) {
28-
snprintk(serv, servlen, "%hu", net_ntohs(a->sin_port));
28+
snprintk(serv, servlen, "%hu", net_ntohs(a->sin6_port));
2929
}
3030

3131
return 0;

0 commit comments

Comments
 (0)