From d9dc86b640301116fba81483fd1a50edfe270085 Mon Sep 17 00:00:00 2001 From: Nicolas Brunie <82109999+nibrunieAtSi5@users.noreply.github.com> Date: Thu, 21 Dec 2023 10:36:16 -0800 Subject: [PATCH] UB in negate in mulh/mulhsu Ensuring No negation on -2^63(int64_t) is performed during mulh/mulhsu Signed-off-by: Nicolas Brunie <82109999+nibrunieAtSi5@users.noreply.github.com> --- riscv/arith.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/riscv/arith.h b/riscv/arith.h index c60fcbea5f..7e471b2cb6 100644 --- a/riscv/arith.h +++ b/riscv/arith.h @@ -32,14 +32,14 @@ inline uint64_t mulhu(uint64_t a, uint64_t b) inline int64_t mulh(int64_t a, int64_t b) { int negate = (a < 0) != (b < 0); - uint64_t res = mulhu(a < 0 ? -a : a, b < 0 ? -b : b); + uint64_t res = mulhu(a < 0 ? -(uint64_t)a : a, b < 0 ? -(uint64_t)b : b); return negate ? ~res + ((uint64_t)a * (uint64_t)b == 0) : res; } inline int64_t mulhsu(int64_t a, uint64_t b) { int negate = a < 0; - uint64_t res = mulhu(a < 0 ? -a : a, b); + uint64_t res = mulhu(a < 0 ? -(uint64_t)a : a, b); return negate ? ~res + (a * b == 0) : res; }