Skip to content

Commit

Permalink
compiler: avoid calls to env::current_dir
Browse files Browse the repository at this point in the history
When these paths are already absolute the return value of
`env::current_dir` goes unused. Avoid possible panicks when the path is
already absolute.

Updates rust-lang#116426.
  • Loading branch information
tamird committed Oct 6, 2023
1 parent 28da466 commit 590468d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
10 changes: 9 additions & 1 deletion compiler/rustc_metadata/src/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,15 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
stable_crate_id: StableCrateId,
) -> Result<&'static [ProcMacro], CrateError> {
// Make sure the path contains a / or the linker will search for it.
let path = env::current_dir().unwrap().join(path);
//
// FIXME(https://github.com/rust-lang/rust/issues/92750): use std::path::absolute.
let buf;
let path = if path.is_absolute() {
path
} else {
buf = env::current_dir().unwrap().join(path);
&buf
};
let lib = load_dylib(&path, 5).map_err(|err| CrateError::DlOpen(err))?;

let sym_name = self.sess.generate_proc_macro_decls_symbol(stable_crate_id);
Expand Down
18 changes: 13 additions & 5 deletions compiler/rustc_plugin_impl/src/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rustc_span::symbol::{sym, Ident};

use std::env;
use std::mem;
use std::path::PathBuf;
use std::path::Path;

/// Pointer to a registrar function.
type PluginRegistrarFn = fn(&mut Registry<'_>);
Expand Down Expand Up @@ -51,7 +51,7 @@ fn load_plugin(
ident: Ident,
) {
let lib = locator::find_plugin_registrar(sess, metadata_loader, ident.span, ident.name);
let fun = dylink_registrar(lib).unwrap_or_else(|err| {
let fun = dylink_registrar(&lib).unwrap_or_else(|err| {
// This is fatal: there are almost certainly macros we need inside this crate, so
// continuing would spew "macro undefined" errors.
sess.emit_fatal(LoadPluginError { span: ident.span, msg: err.to_string() });
Expand All @@ -60,11 +60,19 @@ fn load_plugin(
}

/// Dynamically link a registrar function into the compiler process.
fn dylink_registrar(lib_path: PathBuf) -> Result<PluginRegistrarFn, libloading::Error> {
fn dylink_registrar(lib_path: &Path) -> Result<PluginRegistrarFn, libloading::Error> {
// Make sure the path contains a / or the linker will search for it.
let lib_path = env::current_dir().unwrap().join(&lib_path);
//
// FIXME(https://github.com/rust-lang/rust/issues/92750): use std::path::absolute.
let buf;
let lib_path = if lib_path.is_absolute() {
lib_path
} else {
buf = env::current_dir().unwrap().join(lib_path);
&buf
};

let lib = unsafe { Library::new(&lib_path) }?;
let lib = unsafe { Library::new(lib_path) }?;

let registrar_sym = unsafe { lib.get::<PluginRegistrarFn>(b"__rustc_plugin_registrar") }?;

Expand Down

0 comments on commit 590468d

Please sign in to comment.