Skip to content

Commit e2ea7d8

Browse files
authored
Rollup merge of rust-lang#125861 - name1e5s:fix/rpath_null_panic, r=michaelwoerister
rustc_codegen_ssa: fix `get_rpath_relative_to_output` panic when lib only contains file name <!-- If this PR is related to an unstable feature or an otherwise tracked effort, please link to the relevant tracking issue here. If you don't know of a related tracking issue or there are none, feel free to ignore this. This PR will get automatically assigned to a reviewer. In case you would like a specific user to review your work, you can assign it to them by using r​? <reviewer name> --> When compiles program with `-C rpath=yes` but with no output filename specified, or with filename ONLY, we will get an ICE for now. Fix it by treat empty `output` path in `get_rpath_relative_to_output` as current dir. Before this patch: ```bash rustc -C prefer_dynamic=yes -C rpath=yes -O h.rs # ICE, no output filename specified rustc -o hello -C prefer_dynamic=yes -C rpath=yes -O h.rs # ICE, output filename has no path rustc -o ./hello -C prefer_dynamic=yes -C rpath=yes -O h.rs # Works ``` All those examples work after the patch. Close rust-lang#119571. Close rust-lang#125785.
2 parents 94c19c6 + 5e802f0 commit e2ea7d8

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

compiler/rustc_codegen_ssa/src/back/rpath.rs

+5
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ fn get_rpath_relative_to_output(config: &RPathConfig<'_>, lib: &Path) -> OsStrin
8585
// Strip filenames
8686
let lib = lib.parent().unwrap();
8787
let output = config.out_filename.parent().unwrap();
88+
89+
// If output or lib is empty, just assume it locates in current path
90+
let lib = if lib == Path::new("") { Path::new(".") } else { lib };
91+
let output = if output == Path::new("") { Path::new(".") } else { output };
92+
8893
let lib = try_canonicalize(lib).unwrap();
8994
let output = try_canonicalize(output).unwrap();
9095
let relative = path_relative_from(&lib, &output)

compiler/rustc_codegen_ssa/src/back/rpath/tests.rs

+16
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,22 @@ fn test_rpath_relative() {
5757
}
5858
}
5959

60+
#[test]
61+
fn test_rpath_relative_issue_119571() {
62+
let config = &mut RPathConfig {
63+
libs: &[],
64+
out_filename: PathBuf::from("rustc"),
65+
has_rpath: true,
66+
is_like_osx: false,
67+
linker_is_gnu: true,
68+
};
69+
// Should not panic when out_filename only contains filename.
70+
// Issue 119571
71+
let _ = get_rpath_relative_to_output(config, Path::new("lib/libstd.so"));
72+
// Should not panic when lib only contains filename.
73+
let _ = get_rpath_relative_to_output(config, Path::new("libstd.so"));
74+
}
75+
6076
#[test]
6177
fn test_xlinker() {
6278
let args = rpaths_to_flags(vec!["a/normal/path".into(), "a,comma,path".into()]);

0 commit comments

Comments
 (0)