mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-03-05 14:06:27 -05:00
Merge #18167: Fix a violation of C++ standard rules where unions are used for type-punning
0653939ac1
Add static_asserts to ser_X_to_Y() methods (Samer Afach)be94096dfb
Fix a violation of C++ standard rules that unions cannot be switched. (Samer Afach) Pull request description: Type punning in C++ is not like C. As per the C++ standard, one cannot use unions to convert the bit type. A discussion about this can be found [here](https://stackoverflow.com/questions/25664848/unions-and-type-punning). In C++, a union is supposed to only hold one type at a time. It's intended to be used only as `std::variant`. Switching types is undefined behavior. In fact, C++20 has a special casting function, called [`bit_cast`](https://en.cppreference.com/w/cpp/numeric/bit_cast) that solved this problem. Why has it been working so far? Because some compilers tolerate using unions and switching types, like gcc. More information [here](https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#Type-punning). One important thing to mention is that performance is generally not affected by that memcpy. Compilers are smart enough to convert that to a memory cast when possible. But we have to do it the right way, otherwise, it's jut undefined behavior that depends on the compiler. ACKs for top commit: practicalswift: ACK0653939ac1
elichai: ACK0653939ac1
laanwj: Code review ACK0653939ac1
kristapsk: ACK0653939ac1
Tree-SHA512: f6e89de39fc964750429139bab6b5a1346f7060334b7afa020e315bdad8f8c195bce2b8a9e343f06e7fff175e2dfb1cdabfcb6fe405bea0febe4962f0cc62557
This commit is contained in:
commit
e5d47ed8fd
1 changed files with 17 additions and 12 deletions
|
@ -9,6 +9,7 @@
|
|||
#include <compat/endian.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <ios>
|
||||
#include <limits>
|
||||
#include <map>
|
||||
|
@ -139,27 +140,31 @@ template<typename Stream> inline uint64_t ser_readdata64(Stream &s)
|
|||
}
|
||||
inline uint64_t ser_double_to_uint64(double x)
|
||||
{
|
||||
union { double x; uint64_t y; } tmp;
|
||||
tmp.x = x;
|
||||
return tmp.y;
|
||||
uint64_t tmp;
|
||||
std::memcpy(&tmp, &x, sizeof(x));
|
||||
static_assert(sizeof(tmp) == sizeof(x), "double and uint64_t assumed to have the same size");
|
||||
return tmp;
|
||||
}
|
||||
inline uint32_t ser_float_to_uint32(float x)
|
||||
{
|
||||
union { float x; uint32_t y; } tmp;
|
||||
tmp.x = x;
|
||||
return tmp.y;
|
||||
uint32_t tmp;
|
||||
std::memcpy(&tmp, &x, sizeof(x));
|
||||
static_assert(sizeof(tmp) == sizeof(x), "float and uint32_t assumed to have the same size");
|
||||
return tmp;
|
||||
}
|
||||
inline double ser_uint64_to_double(uint64_t y)
|
||||
{
|
||||
union { double x; uint64_t y; } tmp;
|
||||
tmp.y = y;
|
||||
return tmp.x;
|
||||
double tmp;
|
||||
std::memcpy(&tmp, &y, sizeof(y));
|
||||
static_assert(sizeof(tmp) == sizeof(y), "double and uint64_t assumed to have the same size");
|
||||
return tmp;
|
||||
}
|
||||
inline float ser_uint32_to_float(uint32_t y)
|
||||
{
|
||||
union { float x; uint32_t y; } tmp;
|
||||
tmp.y = y;
|
||||
return tmp.x;
|
||||
float tmp;
|
||||
std::memcpy(&tmp, &y, sizeof(y));
|
||||
static_assert(sizeof(tmp) == sizeof(y), "float and uint32_t assumed to have the same size");
|
||||
return tmp;
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue