From ff2c2ec6b225c690234056a16fd1bc07e203ca0e Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Wed, 2 Nov 2016 00:39:11 +0000 Subject: [PATCH] Bitcode: Check file size before reading bitcode header. Should unbreak ocaml binding tests. Also added an llvm-dis test that checks for the same thing. llvm-svn: 285777 --- llvm/lib/Bitcode/Reader/BitcodeReader.cpp | 3 ++- llvm/test/Bitcode/Inputs/invalid-empty.bc | 0 llvm/test/Bitcode/invalid.test | 3 +++ 3 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 llvm/test/Bitcode/Inputs/invalid-empty.bc diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp index 710187a..954d482 100644 --- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -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/llvm/test/Bitcode/Inputs/invalid-empty.bc b/llvm/test/Bitcode/Inputs/invalid-empty.bc new file mode 100644 index 0000000..e69de29 diff --git a/llvm/test/Bitcode/invalid.test b/llvm/test/Bitcode/invalid.test index e7e78b3..d9381b7 100644 --- a/llvm/test/Bitcode/invalid.test +++ b/llvm/test/Bitcode/invalid.test @@ -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 -- 2.7.4