From: Mike Stepanek (mstepane) Date: Tue, 12 Apr 2022 16:43:53 +0000 (+0000) Subject: Pull request #3369: SfIp: Follow up for warning suppression X-Git-Tag: 3.1.28.0~19 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=816f7d11f6f44e796e6b8881530b7ecef8fc9c96;p=thirdparty%2Fsnort3.git Pull request #3369: SfIp: Follow up for warning suppression Merge in SNORT/snort3 from ~ASERBENI/snort3:sfip_warn_suppress to master Squashed commit of the following: commit f036849106353c02ceabf795e655cb298664a4fb Author: Andrii Serbeniuk Date: Thu Apr 7 13:49:28 2022 +0300 sfip: improve warning suppression --- diff --git a/src/sfip/sf_ip.cc b/src/sfip/sf_ip.cc index ae7802bba..04c960cbf 100644 --- a/src/sfip/sf_ip.cc +++ b/src/sfip/sf_ip.cc @@ -42,11 +42,8 @@ bool SfIp::test_features{ false }; /* Masks off 'val' bits from the IP contained within 'ip' */ inline int SfIp::cidr_mask(int val) { - uint32_t* p; int index, bits; - p = ip32; - if (val < 0 || val > 128) return SFIP_ARG_ERR; @@ -64,14 +61,14 @@ inline int SfIp::cidr_mask(int val) mask = ~0; mask >>= bits; mask <<= bits; - p[index] &= htonl(mask); + ip32[index] &= htonl(mask); } index++; /* 0 off the rest of the IP */ for (; index < 4; index++) - p[index] = 0; + ip32[index] = 0; return SFIP_SUCCESS; } diff --git a/src/sfip/sf_ip.h b/src/sfip/sf_ip.h index 7a2aed2a8..19e9fac96 100644 --- a/src/sfip/sf_ip.h +++ b/src/sfip/sf_ip.h @@ -31,6 +31,7 @@ #include "main/snort_types.h" #include "sfip/sf_returns.h" +#include "utils/cpp_macros.h" namespace snort { @@ -145,8 +146,7 @@ inline uint32_t SfIp::get_ip4_value() const } /* Safe to ignore because ip32 is at the offset of 0 in SfIp */ -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Waddress-of-packed-member" +PACKED_MEMBER_ADDR_BEGIN inline const uint32_t* SfIp::get_ip4_ptr() const { @@ -165,7 +165,7 @@ inline const uint32_t* SfIp::get_ptr() const return ip32; } -#pragma GCC diagnostic pop +PACKED_MEMBER_ADDR_END inline bool SfIp::is_set() const { diff --git a/src/utils/cpp_macros.h b/src/utils/cpp_macros.h index 7d723bb99..f77314a55 100644 --- a/src/utils/cpp_macros.h +++ b/src/utils/cpp_macros.h @@ -75,4 +75,23 @@ # define PADDING_GUARD_END #endif +// Pair of macros to temporarily disable and then enable warnings for code +// that is accessing members of packed types with pointers +#if defined(__clang__) && __clang_major__ >= 4 && !defined(__ICC) +# define PACKED_MEMBER_ADDR_BEGIN \ + _Pragma(STRINGIFY( clang diagnostic push )) \ + _Pragma(STRINGIFY( clang diagnostic ignored "-Waddress-of-packed-member" )) +# define PACKED_MEMBER_ADDR_END \ + _Pragma(STRINGIFY( clang diagnostic pop )) +#elif defined(__GNUC__) && __GNUC__ >= 9 && !defined(__ICC) +# define PACKED_MEMBER_ADDR_BEGIN \ + _Pragma(STRINGIFY( GCC diagnostic push )) \ + _Pragma(STRINGIFY( GCC diagnostic ignored "-Waddress-of-packed-member" )) +# define PACKED_MEMBER_ADDR_END \ + _Pragma(STRINGIFY( GCC diagnostic pop )) +#else +# define PACKED_MEMBER_ADDR_BEGIN +# define PACKED_MEMBER_ADDR_END +#endif + #endif