From: Christopher Dykes Date: Mon, 17 Apr 2017 22:40:29 +0000 (-0700) Subject: Don't invoke undefined behavior when getting the pthread_t out of std::thread::id X-Git-Tag: v2017.04.24.00~15 X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=43c0c2c5bf5288e64e15edba105d864986c76add;p=folly.git Don't invoke undefined behavior when getting the pthread_t out of std::thread::id Summary: This assumes I understand strict-aliasing rules correctly. Reviewed By: yfeldblum Differential Revision: D4900118 fbshipit-source-id: edba535d3ba799ac665d3f859dc4154b2c1b22cb --- diff --git a/folly/ThreadName.h b/folly/ThreadName.h index ad773033..0a292a53 100644 --- a/folly/ThreadName.h +++ b/folly/ThreadName.h @@ -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(&id); + pthread_t ptid; + std::memcpy(&ptid, &id, sizeof(pthread_t)); return setThreadName(ptid, name); }