From 07bab929700b04ba27e034be156892f35162e3b6 Mon Sep 17 00:00:00 2001 From: Joshua Wise Date: Sun, 6 Nov 2011 21:29:16 -0500 Subject: [PATCH] llvm::mk_object_file should really return an option, since the underlying LLVM function can fail. Fixes a crash on OS X when rust has bad dylibs within eyeshot. --- src/comp/lib/llvm.rs | 7 ++++--- src/comp/metadata/creader.rs | 5 ++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/comp/lib/llvm.rs b/src/comp/lib/llvm.rs index 379926446474b..5baad8466f90b 100644 --- a/src/comp/lib/llvm.rs +++ b/src/comp/lib/llvm.rs @@ -1,4 +1,4 @@ -import std::{vec, str}; +import std::{vec, str, option}; import std::str::sbuf; import llvm::{ModuleRef, ContextRef, TypeRef, TypeHandleRef, ValueRef, @@ -1057,9 +1057,10 @@ resource object_file_res(ObjectFile: ObjectFileRef) { type object_file = {llof: ObjectFileRef, dtor: @object_file_res}; -fn mk_object_file(llmb: MemoryBufferRef) -> object_file { +fn mk_object_file(llmb: MemoryBufferRef) -> option::t { let llof = llvm::LLVMCreateObjectFile(llmb); - ret {llof: llof, dtor: @object_file_res(llof)}; + if llof as int == 0 { ret option::none::; } + ret option::some::({llof: llof, dtor: @object_file_res(llof)}); } /* Memory-managed interface to section iterators. */ diff --git a/src/comp/metadata/creader.rs b/src/comp/metadata/creader.rs index 5946b337b489f..b283c4c3e6b7c 100644 --- a/src/comp/metadata/creader.rs +++ b/src/comp/metadata/creader.rs @@ -175,7 +175,10 @@ fn get_metadata_section(sess: session::session, llvm::LLVMRustCreateMemoryBufferWithContentsOfFile(buf) }); if mb as int == 0 { ret option::none::<@[u8]>; } - let of = mk_object_file(mb); + let of = alt mk_object_file(mb) { + option::some(of) { of } + _ { ret option::none::<@[u8]>; } + }; let si = mk_section_iter(of.llof); while llvm::LLVMIsSectionIteratorAtEnd(of.llof, si.llsi) == False { let name_buf = llvm::LLVMGetSectionName(si.llsi);