diff options
| author | Hai-Hsin <me@hai-hs.in> | 2024-06-01 23:13:33 +0800 |
|---|---|---|
| committer | Hai-Hsin <me@hai-hs.in> | 2024-06-02 00:14:05 +0800 |
| commit | 5e802f07bae5a078c241ed1c2a122e756cbcad30 (patch) | |
| tree | 35a40ab606cd6b11784de233ab624918bf7c5f4f | |
| parent | 05965ae238403d8c141170b411245a62aa046240 (diff) | |
| download | rust-5e802f07bae5a078c241ed1c2a122e756cbcad30.tar.gz rust-5e802f07bae5a078c241ed1c2a122e756cbcad30.zip | |
rustc_codegen_ssa: fix get_rpath_relative_to_output panic when lib only contains file name
| -rw-r--r-- | compiler/rustc_codegen_ssa/src/back/rpath.rs | 5 | ||||
| -rw-r--r-- | compiler/rustc_codegen_ssa/src/back/rpath/tests.rs | 16 |
2 files changed, 21 insertions, 0 deletions
diff --git a/compiler/rustc_codegen_ssa/src/back/rpath.rs b/compiler/rustc_codegen_ssa/src/back/rpath.rs index 3114f1c38ae..f499bbcf853 100644 --- a/compiler/rustc_codegen_ssa/src/back/rpath.rs +++ b/compiler/rustc_codegen_ssa/src/back/rpath.rs @@ -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) diff --git a/compiler/rustc_codegen_ssa/src/back/rpath/tests.rs b/compiler/rustc_codegen_ssa/src/back/rpath/tests.rs index ac2e54072c4..0de90a1036e 100644 --- a/compiler/rustc_codegen_ssa/src/back/rpath/tests.rs +++ b/compiler/rustc_codegen_ssa/src/back/rpath/tests.rs @@ -58,6 +58,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()]); |
