Skip to content

Commit f5b3021

Browse files
nitnelaveRalfJung
authored andcommitted
Handle null from LLVMRustGetSectionName
1 parent 4393768 commit f5b3021

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/librustc_codegen_llvm/llvm/ffi.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1736,7 +1736,9 @@ extern "C" {
17361736
pub fn LLVMRustArchiveIteratorFree(AIR: &'a mut ArchiveIterator<'a>);
17371737
pub fn LLVMRustDestroyArchive(AR: &'static mut Archive);
17381738

1739-
pub fn LLVMRustGetSectionName(SI: &SectionIterator<'_>, data: &mut *const c_char) -> size_t;
1739+
#[allow(improper_ctypes)]
1740+
pub fn LLVMRustGetSectionName(SI: &SectionIterator<'_>,
1741+
data: &mut Option<std::ptr::NonNull<c_char>>) -> size_t;
17401742

17411743
#[allow(improper_ctypes)]
17421744
pub fn LLVMRustWriteTwineToString(T: &Twine, s: &RustString);

src/librustc_codegen_llvm/metadata.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use rustc_data_structures::owning_ref::OwningRef;
88
use rustc_codegen_ssa::METADATA_FILENAME;
99

1010
use std::path::Path;
11-
use std::ptr;
1211
use std::slice;
1312
use rustc_fs_util::path_to_c_string;
1413

@@ -67,10 +66,14 @@ fn search_meta_section<'a>(of: &'a ObjectFile,
6766
unsafe {
6867
let si = mk_section_iter(of.llof);
6968
while llvm::LLVMIsSectionIteratorAtEnd(of.llof, si.llsi) == False {
70-
let mut name_buf = ptr::null();
69+
let mut name_buf = None;
7170
let name_len = llvm::LLVMRustGetSectionName(si.llsi, &mut name_buf);
72-
let name = slice::from_raw_parts(name_buf as *const u8, name_len as usize).to_vec();
73-
let name = String::from_utf8(name).unwrap();
71+
let name = name_buf.map_or(
72+
"".to_string(),
73+
|buf| String::from_utf8(
74+
slice::from_raw_parts(buf.as_ptr() as *const u8,
75+
name_len as usize)
76+
.to_vec()).unwrap());
7477
debug!("get_metadata_section: name {}", name);
7578
if read_metadata_section_name(target) == name {
7679
let cbuf = llvm::LLVMGetSectionContents(si.llsi);

0 commit comments

Comments
 (0)