From d0d95dbf3c8ca94bdd3c6d8afc1aca60b12325a4 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Tue, 3 Feb 2015 00:49:57 +0000 Subject: [PATCH] Use a non-fatal diag handler in the C API. FIxes PR22368. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@227903 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Bitcode/Reader/BitReader.cpp | 13 +++++++++++-- test/Bindings/llvm-c/Inputs/invalid.ll.bc | Bin 0 -> 332 bytes test/Bindings/llvm-c/invalid-bitcode.test | 3 +++ 3 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 test/Bindings/llvm-c/Inputs/invalid.ll.bc create mode 100644 test/Bindings/llvm-c/invalid-bitcode.test diff --git a/lib/Bitcode/Reader/BitReader.cpp b/lib/Bitcode/Reader/BitReader.cpp index 9b3acb5ca0a..8641cd1487c 100644 --- a/lib/Bitcode/Reader/BitReader.cpp +++ b/lib/Bitcode/Reader/BitReader.cpp @@ -9,9 +9,11 @@ #include "llvm-c/BitReader.h" #include "llvm/Bitcode/ReaderWriter.h" +#include "llvm/IR/DiagnosticPrinter.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/Module.h" #include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/raw_ostream.h" #include #include @@ -30,8 +32,15 @@ LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef, LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutModule, char **OutMessage) { - ErrorOr ModuleOrErr = - parseBitcodeFile(unwrap(MemBuf)->getMemBufferRef(), *unwrap(ContextRef)); + MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef(); + LLVMContext &Ctx = *unwrap(ContextRef); + + std::string Message; + raw_string_ostream Stream(Message); + DiagnosticPrinterRawOStream DP(Stream); + + ErrorOr ModuleOrErr = parseBitcodeFile( + Buf, Ctx, [&](const DiagnosticInfo &DI) { DI.print(DP); }); if (std::error_code EC = ModuleOrErr.getError()) { if (OutMessage) *OutMessage = strdup(EC.message().c_str()); diff --git a/test/Bindings/llvm-c/Inputs/invalid.ll.bc b/test/Bindings/llvm-c/Inputs/invalid.ll.bc new file mode 100644 index 0000000000000000000000000000000000000000..a85c3644b3abb93a450b18dcfa20b5fbf01ed143 GIT binary patch literal 332 zcmZ>AK5$Qwhk+r0fq{X$Nr8b0NDBcmd!zD1#}h1`Yyw7>lNeigR9QJB%ZP4|hw7 zkAOfiOF{rsfE!Sqvlt^o1CVB5_+%gsB#k*5WR5(PNfTkT?QpheIo#vaz+PFwUOc0a z=NSY4TLHc=4SdEtPI8A1%AQ*&lV-pQR@N+ihS~OnvuzKv?U{<6k^=T>4)#iq_JRWT yssiS+iuM8pcA(jX3Jl=zU~@2FxCG=U0{N^!TA0OAM*v8J{Lr$9i9r%bg8%^2mrtbt literal 0 HcmV?d00001 diff --git a/test/Bindings/llvm-c/invalid-bitcode.test b/test/Bindings/llvm-c/invalid-bitcode.test new file mode 100644 index 00000000000..997f7b5d5b7 --- /dev/null +++ b/test/Bindings/llvm-c/invalid-bitcode.test @@ -0,0 +1,3 @@ +; RUN: not llvm-c-test --module-dump < %S/Inputs/invalid.ll.bc 2>&1 | FileCheck %s + +CHECK: Error parsing bitcode: Corrupted bitcode