Skip to content

Commit d22f53b

Browse files
committed
dladdr cannot leave dli_fname to be null
1 parent e42bbfe commit d22f53b

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed

compiler/rustc_session/src/filesearch.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,7 @@ fn current_dll_path() -> Result<PathBuf, String> {
8282
let fname_ptr = info.dli_fname.as_ptr();
8383
#[cfg(not(target_os = "cygwin"))]
8484
let fname_ptr = {
85-
if info.dli_fname.is_null() {
86-
return Err("dladdr returned null pointer".into());
87-
}
85+
assert!(!info.dli_fname.is_null(), "the docs do not allow dladdr to be null");
8886
info.dli_fname
8987
};
9088
let bytes = CStr::from_ptr(fname_ptr).to_bytes();

src/tools/miri/src/shims/native_lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
9292
fn get_func_ptr_explicitly_from_lib(&mut self, link_name: Symbol) -> Option<CodePtr> {
9393
let this = self.eval_context_mut();
9494
// Try getting the function from the shared library.
95-
// On windows `_lib_path` will be unused, hence the name starting with `_`.
96-
let (lib, _lib_path) = this.machine.native_lib.as_ref().unwrap();
95+
let (lib, lib_path) = this.machine.native_lib.as_ref().unwrap();
9796
let func: libloading::Symbol<'_, unsafe extern "C" fn()> = unsafe {
9897
match lib.get(link_name.as_str().as_bytes()) {
9998
Ok(x) => x,
@@ -114,16 +113,17 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
114113
// This code is a reimplementation of the mechanism for getting `dli_fname` in `libloading`,
115114
// from: https://docs.rs/libloading/0.7.3/src/libloading/os/unix/mod.rs.html#411
116115
// using the `libc` crate where this interface is public.
117-
let mut info = std::mem::MaybeUninit::<libc::Dl_info>::uninit();
116+
let mut info = std::mem::MaybeUninit::<libc::Dl_info>::zeroed();
118117
unsafe {
119118
if libc::dladdr(*func.deref() as *const _, info.as_mut_ptr()) != 0 {
120119
let info = info.assume_init();
121120
#[cfg(target_os = "cygwin")]
122121
let fname_ptr = info.dli_fname.as_ptr();
123122
#[cfg(not(target_os = "cygwin"))]
124123
let fname_ptr = info.dli_fname;
124+
assert!(!fname_ptr.is_null());
125125
if std::ffi::CStr::from_ptr(fname_ptr).to_str().unwrap()
126-
!= _lib_path.to_str().unwrap()
126+
!= lib_path.to_str().unwrap()
127127
{
128128
return None;
129129
}

0 commit comments

Comments
 (0)