From 0da228c57cab11fe36a8101f24c39a0120a6f226 Mon Sep 17 00:00:00 2001 From: Robert Lubos Date: Mon, 4 Oct 2021 13:16:19 +0200 Subject: [PATCH] net: ip: Verify that in_addr/in6_addr struct sizes are correct Make sure that in_addr/in6_addr structure size match the respective binary IP address size with BUILD_ASSERT. Signed-off-by: Robert Lubos --- include/net/net_ip.h | 6 ++++++ subsys/net/ip/ipv4.c | 2 ++ subsys/net/ip/ipv6.c | 2 ++ 3 files changed, 10 insertions(+) diff --git a/include/net/net_ip.h b/include/net/net_ip.h index d07e02880b4..66805675c71 100644 --- a/include/net/net_ip.h +++ b/include/net/net_ip.h @@ -144,6 +144,9 @@ struct in6_addr { }; }; +/* Binary size of the IPv6 address */ +#define NET_IPV6_ADDR_SIZE 16 + /** IPv4 address struct */ struct in_addr { union { @@ -154,6 +157,9 @@ struct in_addr { }; }; +/* Binary size of the IPv4 address */ +#define NET_IPV4_ADDR_SIZE 4 + /** Socket address family type */ typedef unsigned short int sa_family_t; diff --git a/subsys/net/ip/ipv4.c b/subsys/net/ip/ipv4.c index c978253ecae..308cc50e043 100644 --- a/subsys/net/ip/ipv4.c +++ b/subsys/net/ip/ipv4.c @@ -25,6 +25,8 @@ LOG_MODULE_REGISTER(net_ipv4, CONFIG_NET_IPV4_LOG_LEVEL); #include "tcp_internal.h" #include "ipv4.h" +BUILD_ASSERT(sizeof(struct in_addr) == NET_IPV4_ADDR_SIZE); + /* Timeout for various buffer allocations in this file. */ #define NET_BUF_TIMEOUT K_MSEC(50) diff --git a/subsys/net/ip/ipv6.c b/subsys/net/ip/ipv6.c index 7afc293c969..b9e32a43b50 100644 --- a/subsys/net/ip/ipv6.c +++ b/subsys/net/ip/ipv6.c @@ -35,6 +35,8 @@ LOG_MODULE_REGISTER(net_ipv6, CONFIG_NET_IPV6_LOG_LEVEL); #include "route.h" #include "net_stats.h" +BUILD_ASSERT(sizeof(struct in6_addr) == NET_IPV6_ADDR_SIZE); + /* Timeout value to be used when allocating net buffer during various * neighbor discovery procedures. */