Skip to content

Commit

Permalink
[meta] Use memcpy instead of cast to prevent strict-aliasing error (s…
Browse files Browse the repository at this point in the history
  • Loading branch information
kcudnik authored Dec 1, 2020
1 parent 6dfad66 commit d814d2c
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion meta/MetaKeyHasher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,20 @@ static inline std::size_t sai_get_hash(
return ne.ip_address.addr_family;
}

static_assert(sizeof(uint32_t) == 4, "uint32_t expected to be 4 bytes");

static inline std::size_t sai_get_hash(
_In_ const sai_fdb_entry_t& fe)
{
SWSS_LOG_ENTER();

return *(const uint32_t*)(&fe.mac_address[2]);
uint32_t data;

// use low 4 bytes of mac address as hash value
// use memcpy instead of cast because of strict-aliasing rules
memcpy(&data, fe.mac_address + 2, sizeof(uint32_t));

return data;
}

static inline std::size_t sai_get_hash(
Expand Down

0 comments on commit d814d2c

Please sign in to comment.