summaryrefslogtreecommitdiff
path: root/src/parsing.h
diff options
context:
space:
mode:
authorJF Bastien <jfb@chromium.org>2016-01-28 10:48:34 -0800
committerJF Bastien <jfb@chromium.org>2016-01-28 10:48:34 -0800
commita6198ed82965e40145bc7e14096e7333a2590be6 (patch)
tree67b923fd9ce416e921f1d4f4e52a3e7c55096075 /src/parsing.h
parent876c24812ae7660553b47797bb45461fcea1cc20 (diff)
downloadbinaryen-a6198ed82965e40145bc7e14096e7333a2590be6.tar.gz
binaryen-a6198ed82965e40145bc7e14096e7333a2590be6.tar.bz2
binaryen-a6198ed82965e40145bc7e14096e7333a2590be6.zip
Use bit_cast in parseConst, avoid UB
Diffstat (limited to 'src/parsing.h')
-rw-r--r--src/parsing.h43
1 files changed, 19 insertions, 24 deletions
diff --git a/src/parsing.h b/src/parsing.h
index 31099571b..817355e92 100644
--- a/src/parsing.h
+++ b/src/parsing.h
@@ -19,9 +19,10 @@
#include <sstream>
-#include "wasm.h"
-#include "shared-constants.h"
#include "mixed_arena.h"
+#include "shared-constants.h"
+#include "support/utilities.h"
+#include "wasm.h"
namespace wasm {
@@ -65,39 +66,33 @@ Expression* parseConst(cashew::IString s, WasmType type, MixedArena& allocator)
assert(modifier ? positive[4] == '0' && positive[5] == 'x' : 1);
switch (type) {
case f32: {
- union {
- uint32_t pattern;
- float f;
- } u;
+ uint32_t pattern;
if (modifier) {
std::istringstream istr(modifier);
- istr >> std::hex >> u.pattern;
- u.pattern |= 0x7f800000;
+ istr >> std::hex >> pattern;
+ pattern |= 0x7f800000U;
} else {
- u.pattern = 0x7fc00000;
+ pattern = 0x7fc00000U;
}
- if (negative) u.pattern |= 0x80000000;
- if (!isnan(u.f)) u.pattern |= 1;
- assert(isnan(u.f));
- ret->value.f32 = u.f;
+ if (negative) pattern |= 0x80000000U;
+ if (!isnan(bit_cast<float>(pattern))) pattern |= 1U;
+ ret->value.f32 = bit_cast<float>(pattern);
+ assert(isnan(ret->value.f32));
break;
}
case f64: {
- union {
- uint64_t pattern;
- double d;
- } u;
+ uint64_t pattern;
if (modifier) {
std::istringstream istr(modifier);
- istr >> std::hex >> u.pattern;
- u.pattern |= 0x7ff0000000000000LL;
+ istr >> std::hex >> pattern;
+ pattern |= 0x7ff0000000000000ULL;
} else {
- u.pattern = 0x7ff8000000000000L;
+ pattern = 0x7ff8000000000000UL;
}
- if (negative) u.pattern |= 0x8000000000000000LL;
- if (!isnan(u.d)) u.pattern |= 1;
- assert(isnan(u.d));
- ret->value.f64 = u.d;
+ if (negative) pattern |= 0x8000000000000000ULL;
+ if (!isnan(bit_cast<double>(pattern))) pattern |= 1ULL;
+ ret->value.f64 = bit_cast<double>(pattern);
+ assert(isnan(ret->value.f64));
break;
}
default: return nullptr;