From: David Majnemer <david.majnemer@gmail.com>
Date: Mon, 18 Jul 2016 17:03:09 +0000 (+0000)
Subject: [MathExtras] Fix UB in minIntN
X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a9c9423e52c4dccb7846c9c37ec22fb642021a5a;p=llvm

[MathExtras] Fix UB in minIntN

We negated a value with a signed type which invited problems when that
value was the most negative signed number.  Use an unsigned type
for the value instead.  It will compute the same twos complement
result without the UB.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@275815 91177308-0d34-0410-b5e6-96231b3b80d8
---

diff --git a/include/llvm/Support/MathExtras.h b/include/llvm/Support/MathExtras.h
index cf033e30aac..5c816ac9df9 100644
--- a/include/llvm/Support/MathExtras.h
+++ b/include/llvm/Support/MathExtras.h
@@ -337,7 +337,7 @@ inline uint64_t maxUIntN(uint64_t N) {
 inline int64_t minIntN(int64_t N) {
   assert(N > 0 && N <= 64 && "integer width out of range");
 
-  return -(INT64_C(1)<<(N-1));
+  return -(UINT64_C(1)<<(N-1));
 }
 
 /// Gets the maximum value for a N-bit signed integer.