[llvm] r275722 - Avoid UB in maxIntN(64).
Justin Lebar via llvm-commits
llvm-commits at lists.llvm.org
Sun Jul 17 11:19:26 PDT 2016
Author: jlebar
Date: Sun Jul 17 13:19:26 2016
New Revision: 275722
URL: http://llvm.org/viewvc/llvm-project?rev=275722&view=rev
Log:
Avoid UB in maxIntN(64).
Summary:
Previously we were relying on 2's complement underflow in an int64_t.
Now we cast to a uint64_t so we explicitly get the behavior we want.
Reviewers: rnk
Subscribers: dylanmckay, llvm-commits
Differential Revision: https://reviews.llvm.org/D22445
Modified:
llvm/trunk/include/llvm/Support/MathExtras.h
Modified: llvm/trunk/include/llvm/Support/MathExtras.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/MathExtras.h?rev=275722&r1=275721&r2=275722&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/MathExtras.h (original)
+++ llvm/trunk/include/llvm/Support/MathExtras.h Sun Jul 17 13:19:26 2016
@@ -344,7 +344,9 @@ inline int64_t minIntN(int64_t N) {
inline int64_t maxIntN(int64_t N) {
assert(N > 0 && N <= 64 && "integer width out of range");
- return (INT64_C(1)<<(N-1)) - 1;
+ // This relies on two's complement wraparound when N == 64, so we convert to
+ // int64_t only at the very end to avoid UB.
+ return (UINT64_C(1) << (N - 1)) - 1;
}
/// isUIntN - Checks if an unsigned integer fits into the given (dynamic)
More information about the llvm-commits
mailing list