aboutsummaryrefslogtreecommitdiff
path: root/src/serialize.h
diff options
context:
space:
mode:
authorSamer Afach <info@afach.de>2020-02-17 20:53:50 +0100
committerSamer Afach <info@afach.de>2020-02-17 20:53:50 +0100
commitbe94096dfb0c4862e2314cbae4120d7360b08ef2 (patch)
tree5532c111e5a75a44b0ac404f0b582af0d669cb8b /src/serialize.h
parent179504ccb6f4e5a4418b064a184ba37b04491a31 (diff)
downloadbitcoin-be94096dfb0c4862e2314cbae4120d7360b08ef2.tar.xz
Fix a violation of C++ standard rules that unions cannot be switched.
Diffstat (limited to 'src/serialize.h')
-rw-r--r--src/serialize.h25
1 files changed, 13 insertions, 12 deletions
diff --git a/src/serialize.h b/src/serialize.h
index cee7225bcb..4a6c5e26e4 100644
--- a/src/serialize.h
+++ b/src/serialize.h
@@ -9,6 +9,7 @@
#include <compat/endian.h>
#include <algorithm>
+#include <cstring>
#include <ios>
#include <limits>
#include <map>
@@ -139,27 +140,27 @@ 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));
+ 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));
+ 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));
+ 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));
+ return tmp;
}