[Openmp-commits] [PATCH] D89878: [OpenMP] Fixed a potential integer overflow

Shilei Tian via Phabricator via Openmp-commits openmp-commits at lists.llvm.org
Wed Oct 21 06:23:34 PDT 2020


tianshilei1992 created this revision.
Herald added subscribers: openmp-commits, guansong, yaxunl.
Herald added a project: OpenMP.
tianshilei1992 requested review of this revision.
Herald added a reviewer: jdoerfert.
Herald added a subscriber: sstefan1.

`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 Benes for reporting the issue.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D89878

Files:
  openmp/libomptarget/src/MemoryManager.cpp


Index: openmp/libomptarget/src/MemoryManager.cpp
===================================================================
--- openmp/libomptarget/src/MemoryManager.cpp
+++ openmp/libomptarget/src/MemoryManager.cpp
@@ -49,7 +49,13 @@
   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;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D89878.299663.patch
Type: text/x-patch
Size: 483 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/openmp-commits/attachments/20201021/839146e5/attachment.bin>


More information about the Openmp-commits mailing list