Skip to content

Commit

Permalink
rustc_codegen_ssa: fix get_rpath_relative_to_output panic when lib on…
Browse files Browse the repository at this point in the history
…ly contains file name
  • Loading branch information
name1e5s committed Jun 1, 2024
1 parent 05965ae commit 5e802f0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions compiler/rustc_codegen_ssa/src/back/rpath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ fn get_rpath_relative_to_output(config: &RPathConfig<'_>, lib: &Path) -> OsStrin
// Strip filenames
let lib = lib.parent().unwrap();
let output = config.out_filename.parent().unwrap();

// If output or lib is empty, just assume it locates in current path
let lib = if lib == Path::new("") { Path::new(".") } else { lib };
let output = if output == Path::new("") { Path::new(".") } else { output };

let lib = try_canonicalize(lib).unwrap();
let output = try_canonicalize(output).unwrap();
let relative = path_relative_from(&lib, &output)
Expand Down
16 changes: 16 additions & 0 deletions compiler/rustc_codegen_ssa/src/back/rpath/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ fn test_rpath_relative() {
}
}

#[test]
fn test_rpath_relative_issue_119571() {
let config = &mut RPathConfig {
libs: &[],
out_filename: PathBuf::from("rustc"),
has_rpath: true,
is_like_osx: false,
linker_is_gnu: true,
};
// Should not panic when out_filename only contains filename.
// Issue 119571
let _ = get_rpath_relative_to_output(config, Path::new("lib/libstd.so"));
// Should not panic when lib only contains filename.
let _ = get_rpath_relative_to_output(config, Path::new("libstd.so"));
}

#[test]
fn test_xlinker() {
let args = rpaths_to_flags(vec!["a/normal/path".into(), "a,comma,path".into()]);
Expand Down

0 comments on commit 5e802f0

Please sign in to comment.