From 3ee23a9ec85474100dd1fdaad9afa32118c4a590 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. llvm-svn: 227903 --- llvm/lib/Bitcode/Reader/BitReader.cpp | 13 +++++++++++-- llvm/test/Bindings/llvm-c/Inputs/invalid.ll.bc | Bin 0 -> 332 bytes llvm/test/Bindings/llvm-c/invalid-bitcode.test | 3 +++ 3 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 llvm/test/Bindings/llvm-c/Inputs/invalid.ll.bc create mode 100644 llvm/test/Bindings/llvm-c/invalid-bitcode.test diff --git a/llvm/lib/Bitcode/Reader/BitReader.cpp b/llvm/lib/Bitcode/Reader/BitReader.cpp index 9b3acb5c..8641cd1 100644 --- a/llvm/lib/Bitcode/Reader/BitReader.cpp +++ b/llvm/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/llvm/test/Bindings/llvm-c/Inputs/invalid.ll.bc b/llvm/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/llvm/test/Bindings/llvm-c/invalid-bitcode.test b/llvm/test/Bindings/llvm-c/invalid-bitcode.test new file mode 100644 index 0000000..997f7b5 --- /dev/null +++ b/llvm/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 -- 2.7.4