From f4a673ff695bb65c879adfdc7f1a1c196bccc219 Mon Sep 17 00:00:00 2001 From: Kamil Cudnik Date: Sun, 13 Dec 2020 12:14:22 +0100 Subject: [PATCH] [meta] Enable strict cast-align warning (#738) Required on ARM architecture where strict alignment is required. Also fixes cast to std::size_t which size is 4 bytes Signed-off-by: kcudnik --- configure.ac | 2 +- meta/MetaKeyHasher.cpp | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/configure.ac b/configure.ac index dbaaa95ac070..e8e373a4cfdc 100644 --- a/configure.ac +++ b/configure.ac @@ -81,7 +81,7 @@ CFLAGS_COMMON+=" -ansi" CFLAGS_COMMON+=" -fPIC" CFLAGS_COMMON+=" -std=c++11" CFLAGS_COMMON+=" -Wall" -CFLAGS_COMMON+=" -Wcast-align" +CFLAGS_COMMON+=" -Wcast-align=strict" CFLAGS_COMMON+=" -Wcast-qual" CFLAGS_COMMON+=" -Wconversion" CFLAGS_COMMON+=" -Wdisabled-optimization" diff --git a/meta/MetaKeyHasher.cpp b/meta/MetaKeyHasher.cpp index b9b6f76b9b08..3628acebca62 100644 --- a/meta/MetaKeyHasher.cpp +++ b/meta/MetaKeyHasher.cpp @@ -132,7 +132,9 @@ static inline std::size_t sai_get_hash( if (re.destination.addr_family == SAI_IP_ADDR_FAMILY_IPV6) { - const uint32_t* ip6 = (const uint32_t*)re.destination.addr.ip6; + // cast is not good enough for arm (cast align) + uint32_t ip6[4]; + memcpy(ip6, re.destination.addr.ip6, sizeof(ip6)); return ip6[0] ^ ip6[1] ^ ip6[2] ^ ip6[3]; } @@ -152,7 +154,9 @@ static inline std::size_t sai_get_hash( if (ne.ip_address.addr_family == SAI_IP_ADDR_FAMILY_IPV6) { - const uint32_t* ip6 = (const uint32_t*)ne.ip_address.addr.ip6; + // cast is not good enough for arm (cast align) + uint32_t ip6[4]; + memcpy(ip6, ne.ip_address.addr.ip6, sizeof(ip6)); return ip6[0] ^ ip6[1] ^ ip6[2] ^ ip6[3]; } @@ -195,7 +199,8 @@ std::size_t MetaKeyHasher::operator()( if (meta && meta->isobjectid) { - return k.objectkey.key.object_id; + // cast is required in case size_t is 4 bytes (arm) + return (std::size_t)k.objectkey.key.object_id; } switch (k.objecttype)