Don't invoke undefined behavior when getting the pthread_t out of std::thread::id
authorChristopher Dykes <cdykes@fb.com>
Mon, 17 Apr 2017 22:40:29 +0000 (15:40 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Mon, 17 Apr 2017 22:57:28 +0000 (15:57 -0700)
Summary: This assumes I understand strict-aliasing rules correctly.

Reviewed By: yfeldblum

Differential Revision: D4900118

fbshipit-source-id: edba535d3ba799ac665d3f859dc4154b2c1b22cb

folly/ThreadName.h

index ad773033a6252970c69e86130c31833a5593583e..0a292a536ac27460c670c6fc433ee2fb986e25ae 100644 (file)
@@ -85,7 +85,8 @@ inline bool setThreadName(std::thread::id id, StringPiece name) {
   // In most implementations, std::thread::id is a thin wrapper around
   // std::thread::native_handle_type, which means we can do unsafe things to
   // extract it.
-  pthread_t ptid = *reinterpret_cast<pthread_t*>(&id);
+  pthread_t ptid;
+  std::memcpy(&ptid, &id, sizeof(pthread_t));
   return setThreadName(ptid, name);
 }