Make sure the CastInst is valid before trying to create it
authorFilipe Cabecinhas <me@filcab.net>
Tue, 6 Oct 2015 12:37:54 +0000 (12:37 +0000)
committerFilipe Cabecinhas <me@filcab.net>
Tue, 6 Oct 2015 12:37:54 +0000 (12:37 +0000)
Bug found with afl-fuzz.

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

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

index 2893eaef0fb14982c0de7b172460969d67e20c42..ce6790be71373b23133d5d6d135a14cc2747dbc5 100644 (file)
@@ -3863,7 +3863,10 @@ std::error_code BitcodeReader::parseFunctionBody(Function *F) {
           CurBB->getInstList().push_back(Temp);
         }
       } else {
-        I = CastInst::Create((Instruction::CastOps)Opc, Op, ResTy);
+        auto CastOp = (Instruction::CastOps)Opc;
+        if (!CastInst::castIsValid(CastOp, Op, ResTy))
+          return error("Invalid cast");
+        I = CastInst::Create(CastOp, Op, ResTy);
       }
       InstructionList.push_back(I);
       break;
diff --git a/test/Bitcode/Inputs/invalid-cast.bc b/test/Bitcode/Inputs/invalid-cast.bc
new file mode 100644 (file)
index 0000000..a8b82f3
Binary files /dev/null and b/test/Bitcode/Inputs/invalid-cast.bc differ
index 0aab553bb61557d69b9ce0c549006f105c84fe3f..69104046df20256fa7cafdde5c1d1f3f0d75590c 100644 (file)
@@ -113,6 +113,11 @@ RUN:   FileCheck --check-prefix=ELEMENT-TYPE %s
 
 ELEMENT-TYPE: Invalid type
 
+RUN: not llvm-dis -disable-output %p/Inputs/invalid-cast.bc 2>&1 | \
+RUN:   FileCheck --check-prefix=INVALID-CAST %s
+
+INVALID-CAST: Invalid cast
+
 RUN: not llvm-dis -disable-output %p/Inputs/invalid-array-op-not-2nd-to-last.bc 2>&1 | \
 RUN:   FileCheck --check-prefix=ARRAY-NOT-2LAST %s