Skip to content

Commit d814d2c

Browse files
authored
[meta] Use memcpy instead of cast to prevent strict-aliasing error (#723)
1 parent 6dfad66 commit d814d2c

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

meta/MetaKeyHasher.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,20 @@ static inline std::size_t sai_get_hash(
160160
return ne.ip_address.addr_family;
161161
}
162162

163+
static_assert(sizeof(uint32_t) == 4, "uint32_t expected to be 4 bytes");
164+
163165
static inline std::size_t sai_get_hash(
164166
_In_ const sai_fdb_entry_t& fe)
165167
{
166168
SWSS_LOG_ENTER();
167169

168-
return *(const uint32_t*)(&fe.mac_address[2]);
170+
uint32_t data;
171+
172+
// use low 4 bytes of mac address as hash value
173+
// use memcpy instead of cast because of strict-aliasing rules
174+
memcpy(&data, fe.mac_address + 2, sizeof(uint32_t));
175+
176+
return data;
169177
}
170178

171179
static inline std::size_t sai_get_hash(

0 commit comments

Comments
 (0)