]> granicus.if.org Git - llvm/commitdiff
Bitcode: Check file size before reading bitcode header.
authorPeter Collingbourne <peter@pcc.me.uk>
Wed, 2 Nov 2016 00:39:11 +0000 (00:39 +0000)
committerPeter Collingbourne <peter@pcc.me.uk>
Wed, 2 Nov 2016 00:39:11 +0000 (00:39 +0000)
Should unbreak ocaml binding tests.

Also added an llvm-dis test that checks for the same thing.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@285777 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Bitcode/Reader/BitcodeReader.cpp
test/Bitcode/Inputs/invalid-empty.bc [new file with mode: 0644]
test/Bitcode/invalid.test

index 710187af95184bc76d4352a3d8a5d765b3accd26..954d482c5a9989715636f7cf4a619d0729437761 100644 (file)
@@ -4144,7 +4144,8 @@ std::error_code BitcodeReader::parseModule(uint64_t ResumeBit,
 /// Helper to read the header common to all bitcode files.
 static bool hasValidBitcodeHeader(BitstreamCursor &Stream) {
   // Sniff for the signature.
-  if (Stream.Read(8) != 'B' ||
+  if (!Stream.canSkipToPos(4) ||
+      Stream.Read(8) != 'B' ||
       Stream.Read(8) != 'C' ||
       Stream.Read(4) != 0x0 ||
       Stream.Read(4) != 0xC ||
diff --git a/test/Bitcode/Inputs/invalid-empty.bc b/test/Bitcode/Inputs/invalid-empty.bc
new file mode 100644 (file)
index 0000000..e69de29
index e7e78b3af5acadb8c34bb7a2c8b53380624899d1..d9381b7729c364c18fa4b000bb42498a82a3dcd8 100644 (file)
@@ -1,3 +1,5 @@
+RUN: not llvm-dis -disable-output %p/Inputs/invalid-empty.bc 2>&1 | \
+RUN:   FileCheck --check-prefix=INVALID-EMPTY %s
 RUN: not llvm-dis -disable-output %p/Inputs/invalid-pr20485.bc 2>&1 | \
 RUN:   FileCheck --check-prefix=INVALID-ENCODING %s
 RUN: not llvm-dis -disable-output %p/Inputs/invalid-abbrev.bc 2>&1 | \
@@ -27,6 +29,7 @@ RUN:   FileCheck --check-prefix=MISMATCHED-EXPLICIT-INVOKE %s
 RUN: not llvm-dis -disable-output %p/Inputs/invalid-invoke-non-function-explicit-type.bc 2>&1 | \
 RUN:   FileCheck --check-prefix=NON-FUNCTION-EXPLICIT-INVOKE %s
 
+INVALID-EMPTY: Invalid bitcode signature
 INVALID-ENCODING: Invalid encoding
 BAD-ABBREV: Abbreviation starts with an Array or a Blob
 UNEXPECTED-EOF: Unexpected end of file