Don't invoke undefined behavior when getting the pthread_t out of std::thread::id
[folly.git] / 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);
 }