This is an archive of the discontinued LLVM Phabricator instance.

Fix strict-aliasing violation in typeinfo::hash_code()
ClosedPublic

Authored by EricWF on Aug 29 2016, 4:14 PM.

Details

Summary

The current implementation of hash_code() for uniqued RTTI strings violates strict aliasing by dereferencing a type-punned pointer. Specifically it generates a const char** pointer from the address of the __name member before casting it to const size_t* and dereferencing it to get the hash. This is really just a complex and incorrect way of writing reinterpret_cast<size_t>(__name).

This patch changes the conversion sequence so that it no longer contains UB.

Diff Detail

Event Timeline

EricWF updated this revision to Diff 69629.Aug 29 2016, 4:14 PM
EricWF retitled this revision from to Fix strict-aliasing violation in typeinfo::hash_code().
EricWF updated this object.
EricWF added reviewers: mclow.lists, howard.hinnant.
EricWF added a subscriber: cfe-commits.

LGTM, but I'm not a code owner here.

mclow.lists accepted this revision.Oct 5 2016, 3:47 PM
mclow.lists edited edge metadata.

LGTM.

This revision is now accepted and ready to land.Oct 5 2016, 3:47 PM
EricWF closed this revision.Oct 5 2016, 4:04 PM