Skip to content

Commit 8895384

Browse files
committed
Auto merge of #62460 - RalfJung:llvm-null, r=eddyb
Handle null from LLVMRustGetSectionName As part of #58783 and #62103, this incorrect use of a NULL pointer was found in the interface to LLVM. That PR is stuck with some linker issues, but there is no reason the soundness fix should have to wait for that.
2 parents 909f5a0 + 076a5cd commit 8895384

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-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

+9-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,16 @@ 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+
String::new(), // We got a NULL ptr, ignore `name_len`.
73+
|buf| String::from_utf8(
74+
slice::from_raw_parts(buf.as_ptr() as *const u8,
75+
name_len as usize)
76+
.to_vec()
77+
).unwrap()
78+
);
7479
debug!("get_metadata_section: name {}", name);
7580
if read_metadata_section_name(target) == name {
7681
let cbuf = llvm::LLVMGetSectionContents(si.llsi);

0 commit comments

Comments
 (0)