[llvm] ec18030 - [Bitstream] Check that there is enough space for blob

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 7 03:16:25 PST 2022


Author: Nikita Popov
Date: 2022-02-07T12:16:13+01:00
New Revision: ec18030f5fc1f4a08326e65fe06c6f265a829f4c

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

LOG: [Bitstream] Check that there is enough space for blob

Instead of simply assuming that it will be zero. I double checked
that the bitstream reader doesn't have any special handling for
all-zero blobs, it will always write out the full contents.

Added: 
    

Modified: 
    llvm/lib/Bitstream/Reader/BitstreamReader.cpp
    llvm/test/Bitcode/invalid.test

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Bitstream/Reader/BitstreamReader.cpp b/llvm/lib/Bitstream/Reader/BitstreamReader.cpp
index f9247909dc3e..b2da7e721e25 100644
--- a/llvm/lib/Bitstream/Reader/BitstreamReader.cpp
+++ b/llvm/lib/Bitstream/Reader/BitstreamReader.cpp
@@ -331,13 +331,9 @@ Expected<unsigned> BitstreamCursor::readRecord(unsigned AbbrevID,
     size_t CurBitPos = GetCurrentBitNo();
     const size_t NewEnd = CurBitPos + alignTo(NumElts, 4) * 8;
 
-    // If this would read off the end of the bitcode file, just set the
-    // record to empty and return.
-    if (!canSkipToPos(NewEnd/8)) {
-      Vals.append(NumElts, 0);
-      skipToEnd();
-      break;
-    }
+    // Make sure the bitstream is large enough to contain the blob.
+    if (!canSkipToPos(NewEnd/8))
+      return error("Blob ends too soon");
 
     // Otherwise, inform the streamer that we need these bytes in memory.  Skip
     // over tail padding first, in case jumping to NewEnd invalidates the Blob

diff  --git a/llvm/test/Bitcode/invalid.test b/llvm/test/Bitcode/invalid.test
index e3e2f5981cd8..7dabafdb6d0c 100644
--- a/llvm/test/Bitcode/invalid.test
+++ b/llvm/test/Bitcode/invalid.test
@@ -261,3 +261,8 @@ RUN: not llvm-dis -disable-output %p/Inputs/invalid-attribute-group-entry.bc 2>&
 RUN:   FileCheck --check-prefix=INVALID-ATTRIBUTE-GROUP-ENTRY %s
 
 INVALID-ATTRIBUTE-GROUP-ENTRY: Invalid attribute group entry
+
+RUN: not llvm-dis -disable-output %p/Inputs/unterminated-blob.bc 2>&1 | \
+RUN:   FileCheck --check-prefix=UNTERMINATED-BLOB %s
+
+UNTERMINATED-BLOB: Blob ends too soon


        


More information about the llvm-commits mailing list