From 38ad963cc9998c5b60bde8253670237eebc8ca74 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Wed, 8 Jun 2022 14:19:07 +0200 Subject: [PATCH] [sanitizer] Fix shift UB in LEB128 test If u64 and uptr have the same size, then this will perform a shift by the bitwidth, which is UB. We only need this code if uptr is smaller than u64. --- .../sanitizer_common/tests/sanitizer_leb128_test.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/compiler-rt/lib/sanitizer_common/tests/sanitizer_leb128_test.cpp b/compiler-rt/lib/sanitizer_common/tests/sanitizer_leb128_test.cpp index c0c75c470504..ae4c8b5d8b21 100644 --- a/compiler-rt/lib/sanitizer_common/tests/sanitizer_leb128_test.cpp +++ b/compiler-rt/lib/sanitizer_common/tests/sanitizer_leb128_test.cpp @@ -25,10 +25,12 @@ static uptr BitsNeeded(u64 v) { if (!v) return 1; uptr r = 0; - uptr uptr_bits = 8 * sizeof(uptr); - while (v >> uptr_bits) { - r += uptr_bits; - v >>= uptr_bits; + if (sizeof(uptr) != sizeof(u64)) { + uptr uptr_bits = 8 * sizeof(uptr); + while (v >> uptr_bits) { + r += uptr_bits; + v >>= uptr_bits; + } } return r + MostSignificantSetBitIndex(v) + 1; }