[PATCH] D158117: Fix integer overflow in ConcurrentHashtTableByPtr

Andy Kaylor via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 16 14:12:57 PDT 2023


andrew.w.kaylor created this revision.
andrew.w.kaylor added a reviewer: avl.
Herald added a project: All.
andrew.w.kaylor requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This fixes a problem with potential integer overflow when multiplying two 32-bit values and assigning the result to a 64-bit value.

The potential overflow has been present since D132455 <https://reviews.llvm.org/D132455> was re-landed. The initial version of the patch defined MaxBucketSize and NumberOfBuckets as size_t (which I guess would still be a problem for some targets). When the patch was re-landed they were defined as uint32_t, making the calculation of ExtHashMask subject to overflow.


https://reviews.llvm.org/D158117

Files:
  llvm/include/llvm/ADT/ConcurrentHashtable.h


Index: llvm/include/llvm/ADT/ConcurrentHashtable.h
===================================================================
--- llvm/include/llvm/ADT/ConcurrentHashtable.h
+++ llvm/include/llvm/ADT/ConcurrentHashtable.h
@@ -154,7 +154,7 @@
     MaxBucketSize = 1Ull << (std::min((size_t)31, LeadingZerosNumber));
 
     // Calculate mask for extended hash bits.
-    ExtHashMask = (NumberOfBuckets * MaxBucketSize) - 1;
+    ExtHashMask = ((uint64_t)NumberOfBuckets * MaxBucketSize) - 1;
   }
 
   virtual ~ConcurrentHashTableByPtr() {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D158117.550879.patch
Type: text/x-patch
Size: 531 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230816/1aec30c9/attachment.bin>


More information about the llvm-commits mailing list