[llvm] 7a2dfe2 - Avoid unintended integer overflow in bitstream handling

Andy Kaylor via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 15 12:28:46 PDT 2023


Author: Andy Kaylor
Date: 2023-08-15T11:15:58-07:00
New Revision: 7a2dfe2237ec4433cfd8eb057631d1859513d54f

URL: https://github.com/llvm/llvm-project/commit/7a2dfe2237ec4433cfd8eb057631d1859513d54f
DIFF: https://github.com/llvm/llvm-project/commit/7a2dfe2237ec4433cfd8eb057631d1859513d54f.diff

LOG: Avoid unintended integer overflow in bitstream handling

This change fixes two cases detected by static analysis where an operation is being performed on a 32-bit integer which may overflow and the result is assigned to a 64-bit integer.

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

Added: 
    

Modified: 
    llvm/include/llvm/Bitstream/BitstreamReader.h
    llvm/include/llvm/Bitstream/BitstreamWriter.h

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Bitstream/BitstreamReader.h b/llvm/include/llvm/Bitstream/BitstreamReader.h
index 3a838309fb1ca6f..d271fed1375e4db 100644
--- a/llvm/include/llvm/Bitstream/BitstreamReader.h
+++ b/llvm/include/llvm/Bitstream/BitstreamReader.h
@@ -115,7 +115,7 @@ class SimpleBitstreamCursor {
 
   /// Return the bit # of the bit we are reading.
   uint64_t GetCurrentBitNo() const {
-    return NextChar*CHAR_BIT - BitsInCurWord;
+    return uint64_t(NextChar)*CHAR_BIT - BitsInCurWord;
   }
 
   // Return the byte # of the current bit.

diff  --git a/llvm/include/llvm/Bitstream/BitstreamWriter.h b/llvm/include/llvm/Bitstream/BitstreamWriter.h
index 37c9bc905397018..d802b73e97e0f5d 100644
--- a/llvm/include/llvm/Bitstream/BitstreamWriter.h
+++ b/llvm/include/llvm/Bitstream/BitstreamWriter.h
@@ -111,7 +111,7 @@ class BitstreamWriter {
   /// valid. Flushing only occurs at (sub)block boundaries.
   BitstreamWriter(SmallVectorImpl<char> &O, raw_fd_stream *FS = nullptr,
                   uint32_t FlushThreshold = 512)
-      : Out(O), FS(FS), FlushThreshold(FlushThreshold << 20), CurBit(0),
+      : Out(O), FS(FS), FlushThreshold(uint64_t(FlushThreshold) << 20), CurBit(0),
         CurValue(0), CurCodeSize(2) {}
 
   ~BitstreamWriter() {


        


More information about the llvm-commits mailing list