mirror of
https://github.com/boostorg/intrusive.git
synced 2025-08-03 06:24:44 +02:00
Fixes #46: ("UB due to union based type punning")
This commit is contained in:
@@ -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:
|
||||
|
@@ -24,6 +24,7 @@
|
||||
#include <cstddef>
|
||||
#include <climits>
|
||||
#include <boost/intrusive/detail/mpl.hpp>
|
||||
#include <cstring>
|
||||
|
||||
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
|
||||
|
Reference in New Issue
Block a user