llvm.org GIT mirror llvm / f36e869
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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@275722 91177308-0d34-0410-b5e6-96231b3b80d8 Justin Lebar 4 years ago
1 changed file(s) with 3 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
343343 inline int64_t maxIntN(int64_t N) {
344344 assert(N > 0 && N <= 64 && "integer width out of range");
345345
346 return (INT64_C(1)<<(N-1)) - 1;
346 // This relies on two's complement wraparound when N == 64, so we convert to
347 // int64_t only at the very end to avoid UB.
348 return (UINT64_C(1) << (N - 1)) - 1;
347349 }
348350
349351 /// isUIntN - Checks if an unsigned integer fits into the given (dynamic)