From 43c0c2c5bf5288e64e15edba105d864986c76add Mon Sep 17 00:00:00 2001 From: Christopher Dykes Date: Mon, 17 Apr 2017 15:40:29 -0700 Subject: [PATCH] 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 --- folly/ThreadName.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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); } -- 2.34.1