[compiler-rt] 38ad963 - [sanitizer] Fix shift UB in LEB128 test

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 8 05:21:36 PDT 2022


Author: Nikita Popov
Date: 2022-06-08T14:21:25+02:00
New Revision: 38ad963cc9998c5b60bde8253670237eebc8ca74

URL: https://github.com/llvm/llvm-project/commit/38ad963cc9998c5b60bde8253670237eebc8ca74
DIFF: https://github.com/llvm/llvm-project/commit/38ad963cc9998c5b60bde8253670237eebc8ca74.diff

LOG: [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.

Added: 
    

Modified: 
    compiler-rt/lib/sanitizer_common/tests/sanitizer_leb128_test.cpp

Removed: 
    


################################################################################
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 c0c75c4705045..ae4c8b5d8b218 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;
 }


        


More information about the llvm-commits mailing list