[llvm] [BitstreamReader] Fix 32-bit overflow (PR #117363)

Pranav Kant via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 22 11:01:12 PST 2024


https://github.com/pranavk created https://github.com/llvm/llvm-project/pull/117363

This showed up when processing large LTO-generated files. Hard to come up with a test case.

>From 2319c0e2157137d73f6fe67aaa32d21057b927ff Mon Sep 17 00:00:00 2001
From: Pranav Kant <prka at google.com>
Date: Fri, 22 Nov 2024 18:59:43 +0000
Subject: [PATCH] [BitstreamReader] Fix 32-bit overflow when processing large
 LTO-generated files

---
 llvm/lib/Bitstream/Reader/BitstreamReader.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Bitstream/Reader/BitstreamReader.cpp b/llvm/lib/Bitstream/Reader/BitstreamReader.cpp
index 5b2c76350029be..fed9994db2ae85 100644
--- a/llvm/lib/Bitstream/Reader/BitstreamReader.cpp
+++ b/llvm/lib/Bitstream/Reader/BitstreamReader.cpp
@@ -334,7 +334,8 @@ Expected<unsigned> BitstreamCursor::readRecord(unsigned AbbrevID,
 
     // Figure out where the end of this blob will be including tail padding.
     size_t CurBitPos = GetCurrentBitNo();
-    const size_t NewEnd = CurBitPos + alignTo(NumElts, 4) * 8;
+    const size_t NewEnd =
+        CurBitPos + static_cast<uint64_t>(alignTo(NumElts, 4)) * 8;
 
     // Make sure the bitstream is large enough to contain the blob.
     if (!canSkipToPos(NewEnd/8))



More information about the llvm-commits mailing list