[PATCH] D22445: Avoid UB in maxIntN(64).

Justin Lebar via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 17 11:27:01 PDT 2016


This revision was automatically updated to reflect the committed changes.
Closed by commit rL275722: Avoid UB in maxIntN(64). (authored by jlebar).

Changed prior to commit:
  https://reviews.llvm.org/D22445?vs=64242&id=64261#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D22445

Files:
  llvm/trunk/include/llvm/Support/MathExtras.h

Index: llvm/trunk/include/llvm/Support/MathExtras.h
===================================================================
--- llvm/trunk/include/llvm/Support/MathExtras.h
+++ llvm/trunk/include/llvm/Support/MathExtras.h
@@ -344,7 +344,9 @@
 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)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D22445.64261.patch
Type: text/x-patch
Size: 621 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160717/d73f311a/attachment.bin>


More information about the llvm-commits mailing list