From 3f51425f3c9446c75a28d634b4d47e0b4b65c5ea Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Thu, 6 Mar 2014 10:32:23 +0400 Subject: [PATCH] Fixed possible UB --- include/boost/type_index/ctti_type_index.hpp | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/include/boost/type_index/ctti_type_index.hpp b/include/boost/type_index/ctti_type_index.hpp index 36eb595..b181854 100644 --- a/include/boost/type_index/ctti_type_index.hpp +++ b/include/boost/type_index/ctti_type_index.hpp @@ -42,17 +42,27 @@ namespace detail { // 4) we need a compile-time control to make shure that user does not copy or // default construct `struct-that-represents-type` // -// Solution would be a forward declared structure. -struct ctti_data; +// Solution would be a standard-layout class with private constructors and assignment operators. +class ctti_data { +private: + ctti_data(); + ctti_data(const ctti_data&); + ctti_data& operator=(const ctti_data&); +}; } // namespace detail /// Helper method for getting detail::ctti_data of a tempalte patameter T. template inline const detail::ctti_data& ctti_construct() BOOST_NOEXCEPT { - // Standard C++11, 5.2.10 Reinterpret cast: Converting a prvalue of type “pointer to T1” to the - // type “pointer to T2” (where T1 and T2 are object types and where the alignment requirements of T2 are no - // stricter than those of T1) and back to its original type yields the original pointer value. + // Standard C++11, 5.2.10 Reinterpret cast: + // An object pointer can be explicitly converted to an object pointer of a different type. When a prvalue + // v of type “pointer to T1” is converted to the type “pointer to cv T2”, the result is static_cast(static_cast(v)) if both T1 and T2 are standard-layout types (3.9) and the alignment + // requirements of T2 are no stricter than those of T1, or if either type is void. Converting a prvalue of type + // “pointer to T1” to the type “pointer to T2” (where T1 and T2 are object types and where the alignment + // requirements of T2 are no stricter than those of T1) and back to its original type yields the original pointer + // value. return *reinterpret_cast(boost::detail::ctti::n()); }