Skip to content

Commit be94096

Browse files
Fix a violation of C++ standard rules that unions cannot be switched.
1 parent 179504c commit be94096

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/serialize.h

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <compat/endian.h>
1010

1111
#include <algorithm>
12+
#include <cstring>
1213
#include <ios>
1314
#include <limits>
1415
#include <map>
@@ -139,27 +140,27 @@ template<typename Stream> inline uint64_t ser_readdata64(Stream &s)
139140
}
140141
inline uint64_t ser_double_to_uint64(double x)
141142
{
142-
union { double x; uint64_t y; } tmp;
143-
tmp.x = x;
144-
return tmp.y;
143+
uint64_t tmp;
144+
std::memcpy(&tmp, &x, sizeof(x));
145+
return tmp;
145146
}
146147
inline uint32_t ser_float_to_uint32(float x)
147148
{
148-
union { float x; uint32_t y; } tmp;
149-
tmp.x = x;
150-
return tmp.y;
149+
uint32_t tmp;
150+
std::memcpy(&tmp, &x, sizeof(x));
151+
return tmp;
151152
}
152153
inline double ser_uint64_to_double(uint64_t y)
153154
{
154-
union { double x; uint64_t y; } tmp;
155-
tmp.y = y;
156-
return tmp.x;
155+
double tmp;
156+
std::memcpy(&tmp, &y, sizeof(y));
157+
return tmp;
157158
}
158159
inline float ser_uint32_to_float(uint32_t y)
159160
{
160-
union { float x; uint32_t y; } tmp;
161-
tmp.y = y;
162-
return tmp.x;
161+
float tmp;
162+
std::memcpy(&tmp, &y, sizeof(y));
163+
return tmp;
163164
}
164165

165166

0 commit comments

Comments
 (0)