diff options
author | Alon Zakai <alonzakai@gmail.com> | 2016-01-11 17:16:57 -0500 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2016-01-11 17:16:57 -0500 |
commit | 4e98f922cb2d727a319f62d32e51afabb2e46d24 (patch) | |
tree | 507db7035fe0557431ff49f55016df1bedff8000 /src/s2wasm.h | |
parent | 75a562190a9f4588c8ffb19b8304f76c15a850c6 (diff) | |
parent | 873e5b0500a3edb07a09ed285ba8421be375cd25 (diff) | |
download | binaryen-4e98f922cb2d727a319f62d32e51afabb2e46d24.tar.gz binaryen-4e98f922cb2d727a319f62d32e51afabb2e46d24.tar.bz2 binaryen-4e98f922cb2d727a319f62d32e51afabb2e46d24.zip |
Merge pull request #91 from WebAssembly/sanitizers
Add Travis builds with sanitizers
Diffstat (limited to 'src/s2wasm.h')
-rw-r--r-- | src/s2wasm.h | 72 |
1 files changed, 53 insertions, 19 deletions
diff --git a/src/s2wasm.h b/src/s2wasm.h index bd3f12e73..a0ecfd710 100644 --- a/src/s2wasm.h +++ b/src/s2wasm.h @@ -160,19 +160,36 @@ private: } int32_t getInt() { - int32_t ret = 0; + const char* loc = s; + uint32_t value = 0; bool neg = false; - if (*s == '-') { + if (*loc == '-') { neg = true; - s++; + loc++; } - while (isdigit(*s)) { - ret *= 10; - ret += (*s - '0'); - s++; + while (isdigit(*loc)) { + uint32_t digit = *loc - '0'; + if (value > std::numeric_limits<uint32_t>::max() / 10) { + abort_on("uint32_t overflow"); + } + value *= 10; + if (value > std::numeric_limits<uint32_t>::max() - digit) { + abort_on("uint32_t overflow"); + } + value += digit; + loc++; + } + if (neg) { + uint32_t positive_int_min = + (uint32_t) - (1 + std::numeric_limits<int32_t>::min()) + (uint32_t)1; + if (value > positive_int_min) { + abort_on("negative int32_t overflow"); + } + s = loc; + return -value; } - if (neg) ret = -ret; - return ret; + s = loc; + return value; } // gets a constant, which may be a relocation for later. @@ -198,19 +215,36 @@ private: } int64_t getInt64() { - int64_t ret = 0; + const char* loc = s; + uint64_t value = 0; bool neg = false; - if (*s == '-') { + if (*loc == '-') { neg = true; - s++; + loc++; } - while (isdigit(*s)) { - ret *= 10; - ret += (*s - '0'); - s++; + while (isdigit(*loc)) { + uint64_t digit = *loc - '0'; + if (value > std::numeric_limits<uint64_t>::max() / 10) { + abort_on("uint64_t overflow"); + } + value *= 10; + if (value > std::numeric_limits<uint64_t>::max() - digit) { + abort_on("uint64_t overflow"); + } + value += digit; + loc++; + } + if (neg) { + uint64_t positive_int_min = + (uint64_t) - (1 + std::numeric_limits<int64_t>::min()) + (uint64_t)1; + if (value > positive_int_min) { + abort_on("negative int64_t overflow"); + } + s = loc; + return -value; } - if (neg) ret = -ret; - return ret; + s = loc; + return value; } Name getCommaSeparated() { @@ -933,7 +967,7 @@ private: } else if (match(".int64")) { size_t size = raw->size(); raw->resize(size + 8); - (*(int64_t*)(&(*raw)[size])) = getInt(); + (*(int64_t*)(&(*raw)[size])) = getInt64(); zero = false; } else { break; |