From 0530bf8704f92e9741d969d6576bf8e2bbf026be Mon Sep 17 00:00:00 2001 From: Kirill Isakov Date: Mon, 28 Mar 2022 13:09:07 +0600 Subject: [PATCH] Add check for __Static_assert() and asserts on struct sizes --- src/ethernet.h | 6 ++++++ src/have.h | 6 ++++++ src/ipv4.h | 4 ++++ src/ipv6.h | 8 ++++++++ src/meson.build | 8 ++++++++ 5 files changed, 32 insertions(+) diff --git a/src/ethernet.h b/src/ethernet.h index 4ef9dab9..ddb0a7c6 100644 --- a/src/ethernet.h +++ b/src/ethernet.h @@ -68,6 +68,8 @@ struct ether_header { }; #endif +STATIC_ASSERT(sizeof(struct ether_header) == 14, "ether_header has incorrect size"); + #ifndef HAVE_STRUCT_ARPHDR struct arphdr { uint16_t ar_hrd; @@ -86,6 +88,8 @@ struct arphdr { #define ARPOP_NAK 10 #endif +STATIC_ASSERT(sizeof(struct arphdr) == 8, "arphdr has incorrect size"); + #ifndef HAVE_STRUCT_ETHER_ARP struct ether_arp { struct arphdr ea_hdr; @@ -101,4 +105,6 @@ struct ether_arp { #define arp_op ea_hdr.ar_op #endif +STATIC_ASSERT(sizeof(struct ether_arp) == 28, "ether_arp has incorrect size"); + #endif diff --git a/src/have.h b/src/have.h index 36d17b60..2428151a 100644 --- a/src/have.h +++ b/src/have.h @@ -45,6 +45,12 @@ #include #include +#ifdef HAVE_STATIC_ASSERT +#define STATIC_ASSERT(expr, msg) _Static_assert((expr), msg) +#else +#define STATIC_ASSERT(check, msg) +#endif + #ifdef HAVE_ALLOCA_H #include #elif defined(HAVE_NETBSD) diff --git a/src/ipv4.h b/src/ipv4.h index a412c605..004f4e5d 100644 --- a/src/ipv4.h +++ b/src/ipv4.h @@ -86,6 +86,8 @@ struct ip { }; #endif +STATIC_ASSERT(sizeof(struct ip) == 20, "ip has incorrect size"); + #ifndef IP_OFFMASK #define IP_OFFMASK 0x1fff #endif @@ -148,4 +150,6 @@ struct icmp { }; #endif +STATIC_ASSERT(sizeof(struct icmp) == 28, "icmp has incorrect size"); + #endif diff --git a/src/ipv6.h b/src/ipv6.h index 7c06de3d..6f1e2211 100644 --- a/src/ipv6.h +++ b/src/ipv6.h @@ -60,6 +60,8 @@ struct ip6_hdr { #define ip6_hops ip6_ctlun.ip6_un1.ip6_un1_hlim #endif +STATIC_ASSERT(sizeof(struct ip6_hdr) == 40, "ip6_hdr has incorrect size"); + #ifndef HAVE_STRUCT_ICMP6_HDR struct icmp6_hdr { uint8_t icmp6_type; @@ -86,6 +88,8 @@ struct icmp6_hdr { #define icmp6_mtu icmp6_data32[0] #endif +STATIC_ASSERT(sizeof(struct icmp6_hdr) == 8, "icmp6_hdr has incorrect size"); + #ifndef HAVE_STRUCT_ND_NEIGHBOR_SOLICIT struct nd_neighbor_solicit { struct icmp6_hdr nd_ns_hdr; @@ -99,6 +103,8 @@ struct nd_neighbor_solicit { #define nd_ns_reserved nd_ns_hdr.icmp6_data32[0] #endif +STATIC_ASSERT(sizeof(struct nd_neighbor_solicit) == 24, "nd_neighbor_solicit has incorrect size"); + #ifndef HAVE_STRUCT_ND_OPT_HDR struct nd_opt_hdr { uint8_t nd_opt_type; @@ -106,4 +112,6 @@ struct nd_opt_hdr { }; #endif +STATIC_ASSERT(sizeof(struct nd_opt_hdr) == 2, "nd_opt_hdr has incorrect size"); + #endif diff --git a/src/meson.build b/src/meson.build index e37c2db0..b1796af2 100644 --- a/src/meson.build +++ b/src/meson.build @@ -15,6 +15,14 @@ foreach attr : ['malloc', 'nonnull', 'warn_unused_result'] cc.has_function_attribute(attr) endforeach +if cc.compiles(''' + _Static_assert(1, "ok"); + int main(void) { return 0; } +''') + cdata.set('HAVE_STATIC_ASSERT', 1, + description: 'C11 _Static_assert()') +endif + check_headers = [ 'alloca.h', 'arpa/inet.h', -- 2.20.1