Cast to uint32_t before left-shifting in sext8() to avoid undefined
behavior when shifting a negative value. The subsequent arithmetic
right shift on the int32_t cast correctly sign-extends the result.
Found with UBSan.

Signed-off-by: Brian Cain <[email protected]>
---
 tests/tcg/hexagon/brev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tests/tcg/hexagon/brev.c b/tests/tcg/hexagon/brev.c
index 6c7b1340849..1473e309ee0 100644
--- a/tests/tcg/hexagon/brev.c
+++ b/tests/tcg/hexagon/brev.c
@@ -106,7 +106,7 @@ uint32_t bitreverse(uint32_t x)
 
 int32_t sext8(int32_t x)
 {
-    return (x << 24) >> 24;
+    return (int32_t)((uint32_t)x << 24) >> 24;
 }
 
 #define TEST_BREV_LOAD(SZ, TYPE, BUF, SHIFT, EXP) \
-- 
2.34.1

Reply via email to