Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[meta] Use memcpy instead of cast to prevent strict-aliasing error #723

Merged
merged 1 commit into from
Dec 1, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static_assert [](start = 0, length = 13)

This assertion seems not needed. Do you anticipate some failure case?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a surprise for some wired platforms :D


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