From d8761780b0386c53ab144173a5fdc536ee2e1da8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Thu, 16 Jan 2020 16:10:03 +0100 Subject: [PATCH] Fixes #46: ("UB due to union based type punning") --- doc/intrusive.qbk | 7 +++++++ include/boost/intrusive/detail/math.hpp | 15 +++++---------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/doc/intrusive.qbk b/doc/intrusive.qbk index c03b63e..888c25b 100644 --- a/doc/intrusive.qbk +++ b/doc/intrusive.qbk @@ -3886,6 +3886,13 @@ to be inserted in intrusive containers are allocated using `std::vector` or `std [section:release_notes Release Notes] +[section:release_notes_boost_1_73_00 Boost 1.73 Release] + +* Fixed bugs: + * [@https://github.com/boostorg/intrusive/issues/46 GitHub #46: ['UB due to union based type punning]] + +[endsect] + [section:release_notes_boost_1_71_00 Boost 1.71 Release] * Fixed bugs: diff --git a/include/boost/intrusive/detail/math.hpp b/include/boost/intrusive/detail/math.hpp index 200f8b8..0748f1d 100644 --- a/include/boost/intrusive/detail/math.hpp +++ b/include/boost/intrusive/detail/math.hpp @@ -24,6 +24,7 @@ #include #include #include +#include namespace boost { namespace intrusive { @@ -208,19 +209,13 @@ namespace detail { //http://www.flipcode.com/archives/Fast_log_Function.shtml inline float fast_log2 (float val) { - union caster_t - { - unsigned x; - float val; - } caster; - - caster.val = val; - unsigned x = caster.x; + float f = val; + unsigned x; + std::memcpy(&x, &val, sizeof(f)); const int log_2 = int((x >> 23) & 255) - 128; x &= ~(unsigned(255u) << 23u); x += unsigned(127) << 23u; - caster.x = x; - val = caster.val; + std::memcpy(&val, &x, sizeof(f)); //1+log2(m), m ranging from 1 to 2 //3rd degree polynomial keeping first derivate continuity. //For less precision the line can be commented out