Index: lib/ubsan/ubsan_value.cc =================================================================== --- lib/ubsan/ubsan_value.cc +++ lib/ubsan/ubsan_value.cc @@ -12,6 +12,7 @@ // //===----------------------------------------------------------------------===// +#include #include "ubsan_platform.h" #if CAN_SANITIZE_UB #include "ubsan_value.h" @@ -27,7 +28,16 @@ // to SIntMax. const unsigned ExtraBits = sizeof(SIntMax) * 8 - getType().getIntegerBitWidth(); - return SIntMax(Val) << ExtraBits >> ExtraBits; + UIntMax Value; + if(getType().getIntegerBitWidth() == 32) + Value = UIntMax(abs(Val)); + else + Value = UIntMax(llabs(Val)); + Value = Value << ExtraBits >> ExtraBits; + uptr Mask = 1UL << (getType().getIntegerBitWidth() - 1); + if(Mask & Val) + return -(SIntMax(Value)); + return SIntMax(Value); } if (getType().getIntegerBitWidth() == 64) return *reinterpret_cast(Val);