diff options
author | JF Bastien <github@jfbastien.com> | 2016-04-27 13:46:31 -0700 |
---|---|---|
committer | JF Bastien <github@jfbastien.com> | 2016-04-27 13:46:31 -0700 |
commit | 3b2b18dc06da3895f0b54a0660ab561f2b305c92 (patch) | |
tree | a1fa1a58ffefaf561bf37c1527d0e6f9cab457cd /src | |
parent | d63486d0ef085f905dafdb14a76cfb2ca1fb452e (diff) | |
download | binaryen-3b2b18dc06da3895f0b54a0660ab561f2b305c92.tar.gz binaryen-3b2b18dc06da3895f0b54a0660ab561f2b305c92.tar.bz2 binaryen-3b2b18dc06da3895f0b54a0660ab561f2b305c92.zip |
Remove UB (#405)
ubsan fails with: wasm-binary.h:97:32: runtime error: left shift of negative value -1
Also use type_traits for is_signed.
Diffstat (limited to 'src')
-rw-r--r-- | src/wasm-binary.h | 14 |
1 files changed, 6 insertions, 8 deletions
diff --git a/src/wasm-binary.h b/src/wasm-binary.h index 0cf85a50f..3f7ad44d4 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -23,6 +23,7 @@ #include <istream> #include <ostream> +#include <type_traits> #include "wasm.h" #include "wasm-traversal.h" @@ -40,13 +41,9 @@ struct LEB { LEB() {} LEB(T value) : value(value) {} - bool isSigned() { - return int(MiniT(-1)) < 0; - } - bool hasMore(T temp, MiniT byte) { // for signed, we must ensure the last bit has the right sign, as it will zero extend - return isSigned() ? (temp != 0 && int32_t(temp) != -1) || (value >= 0 && (byte & 64)) || (value < 0 && !(byte & 64)): (temp != 0); + return std::is_signed<T>::value ? (temp != 0 && int32_t(temp) != -1) || (value >= 0 && (byte & 64)) || (value < 0 && !(byte & 64)): (temp != 0); } void write(std::vector<uint8_t>* out) { @@ -90,11 +87,12 @@ struct LEB { shift += 7; } // if signed LEB, then we might need to sign-extend. (compile should optimize this out if not needed) - if (isSigned()) { + if (std::is_signed<T>::value) { shift += 7; if (byte & 64 && size_t(shift) < 8*sizeof(T)) { - // the highest bit we received was a 1, sign-extend all the rest - value = value | (T(-1) << shift); + size_t sext_bits = 8*sizeof(T) - size_t(shift); + value <<= sext_bits; + value >>= sext_bits; assert(value < 0); } } |