From 14cdd65dd256b5734cef5c15ba645e72665ef6f3 Mon Sep 17 00:00:00 2001 From: Catena cyber <35799796+catenacyber@users.noreply.github.com> Date: Sun, 3 Jun 2018 16:26:12 +0200 Subject: [PATCH] fix undefined shift in countLeadingZeros (#1157) --- MathExtras.h | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/MathExtras.h b/MathExtras.h index db98a2f7..2be6e08a 100644 --- a/MathExtras.h +++ b/MathExtras.h @@ -425,14 +425,17 @@ static inline int64_t SignExtend64(uint64_t X, unsigned B) { /// valid arguments. static inline unsigned int countLeadingZeros(int x) { - unsigned count = 0; int i; const unsigned bits = sizeof(x) * 8; + unsigned count = bits; + if (x < 0) { + return 0; + } for (i = bits; --i; ) { - if (x < 0) break; - count++; - x <<= 1; + if (x == 0) break; + count--; + x >>= 1; } return count;