ref: 3f6b06a358af099e495e90004d9ecc829b7f367b
parent: 34cb61711fe29934dfa82ab55ea59ed85ae62642
author: Tor Andersson <[email protected]>
date: Fri Jun 8 09:51:10 EDT 2018
Avoid type punning with unions. Use memcpy, which is guaranteed to be safe under all compiler flags.
--- a/jsdtoa.c
+++ b/jsdtoa.c
@@ -407,9 +407,9 @@
static uint64_t double_to_uint64(double d)
{
- union { double d; uint64_t n; } tmp;
- tmp.d = d;
- return tmp.n;
+ uint64_t n;
+ memcpy(&n, &d, 8);
+ return n;
}
#define DP_SIGNIFICAND_SIZE 52
--- a/jsi.h
+++ b/jsi.h
@@ -43,7 +43,7 @@
#define isnan(x) _isnan(x)
#define isinf(x) (!_finite(x))
#define isfinite(x) _finite(x)
-static __inline int signbit(double x) {union{double d;__int64 i;}u;u.d=x;return u.i>>63;}
+static __inline int signbit(double x) { __int64 i; memcpy(&i, &x, 8); return i>>63; }
#define INFINITY (DBL_MAX+DBL_MAX)
#define NAN (INFINITY-INFINITY)
#endif