From 3b2b18dc06da3895f0b54a0660ab561f2b305c92 Mon Sep 17 00:00:00 2001 From: JF Bastien Date: Wed, 27 Apr 2016 13:46:31 -0700 Subject: 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. --- src/wasm-binary.h | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'src/wasm-binary.h') 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 #include +#include #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::value ? (temp != 0 && int32_t(temp) != -1) || (value >= 0 && (byte & 64)) || (value < 0 && !(byte & 64)): (temp != 0); } void write(std::vector* 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::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); } } -- cgit v1.2.3