[Openmp-commits] [openmp] 3091ed0 - [OpenMP] Fixed a potential integer overflow

Shilei Tian via Openmp-commits openmp-commits at lists.llvm.org
Thu Oct 22 18:22:26 PDT 2020


Author: Shilei Tian
Date: 2020-10-22T21:22:19-04:00
New Revision: 3091ed099f2f6a3d16dbdae7d0406f54dfc3031f

URL: https://github.com/llvm/llvm-project/commit/3091ed099f2f6a3d16dbdae7d0406f54dfc3031f
DIFF: https://github.com/llvm/llvm-project/commit/3091ed099f2f6a3d16dbdae7d0406f54dfc3031f.diff

LOG: [OpenMP] Fixed a potential integer overflow

`size_t` has different width on 32- and 64-bit architecture, but the
computation to floor to power of two assumed it is 64-bit, which can cause an
integer overflow. In this patch, architecture detection is added so that the
operation for 64-bit `size_t`. Thank Luke for reporting the issue.

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D89878

Added: 
    

Modified: 
    openmp/libomptarget/src/MemoryManager.cpp

Removed: 
    


################################################################################
diff  --git a/openmp/libomptarget/src/MemoryManager.cpp b/openmp/libomptarget/src/MemoryManager.cpp
index 886c5be3c03a..f2979590b0c6 100644
--- a/openmp/libomptarget/src/MemoryManager.cpp
+++ b/openmp/libomptarget/src/MemoryManager.cpp
@@ -49,7 +49,13 @@ size_t floorToPowerOfTwo(size_t Num) {
   Num |= Num >> 4;
   Num |= Num >> 8;
   Num |= Num >> 16;
+#if INTPTR_MAX == INT64_MAX
   Num |= Num >> 32;
+#elif INTPTR_MAX == INT32_MAX
+  // Do nothing with 32-bit
+#else
+#error Unsupported architecture
+#endif
   Num += 1;
   return Num >> 1;
 }


        


More information about the Openmp-commits mailing list