[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.
This commit is contained in:
Nikita Popov 2022-06-08 14:19:07 +02:00
parent bf21cda7f2
commit 38ad963cc9
1 changed files with 6 additions and 4 deletions

View File

@ -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;
}