From 3091ed099f2f6a3d16dbdae7d0406f54dfc3031f Mon Sep 17 00:00:00 2001 From: Shilei Tian Date: Thu, 22 Oct 2020 21:21:41 -0400 Subject: [PATCH] [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 --- openmp/libomptarget/src/MemoryManager.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/openmp/libomptarget/src/MemoryManager.cpp b/openmp/libomptarget/src/MemoryManager.cpp index 886c5be..f297959 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; } -- 2.7.4