Skip to content

Commit

Permalink
[meta] Enable strict cast-align warning (sonic-net#738)
Browse files Browse the repository at this point in the history
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 <kcudnik@gmail.com>
  • Loading branch information
kcudnik authored Dec 13, 2020
1 parent 791b761 commit f4a673f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 8 additions & 3 deletions meta/MetaKeyHasher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand All @@ -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];
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f4a673f

Please sign in to comment.