From 84affdb260889c24fd7348ed0cb0e463b8dbc244 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 17 Jul 2018 14:53:50 -0400 Subject: [PATCH] safe_ops: Avoid cases where shift bases are invalid with signed values For example, say the converted signed type is s64, shifting left by 63 bits would be undefined behavior. However, given an ASL is essentially the same behavior as an LSL we can just use an unsigned type instead of converting to a signed type. --- src/common/safe_ops.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/common/safe_ops.h b/src/common/safe_ops.h index eb833ebb..6cc9ea4a 100644 --- a/src/common/safe_ops.h +++ b/src/common/safe_ops.h @@ -76,8 +76,8 @@ T ArithmeticShiftLeft(T value, int shift_amount) { return ArithmeticShiftRight(value, -shift_amount); } - auto signed_value = static_cast>(value); - return static_cast(signed_value << shift_amount); + auto unsigned_value = static_cast>(value); + return static_cast(unsigned_value << shift_amount); } template