Skip to content

Commit 1e668e0

Browse files
tardypcuviper
authored andcommitted
fix panic when rustc tries to reduce intermediate filenames length with multi byte chars
The issue cannot be reproduced with the former testcase of creating external crates because rust refuses to use "external crate 28_找出字符串中第一个匹配项的下标" because it is not a valid indentifier (starts with number, and contain non ascii chars) But still using 28_找出字符串中第一个匹配项的下标.rs as a filename is accepted by previous rustc releases So we consider it valid, and add an integration test for it to catch any regression on other code related to non ascii filenames. (cherry picked from commit c6acffe)
1 parent 912aba6 commit 1e668e0

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

compiler/rustc_session/src/config.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,13 +1207,22 @@ fn maybe_strip_file_name(mut path: PathBuf) -> PathBuf {
12071207
if path.file_name().map_or(0, |name| name.len()) > MAX_FILENAME_LENGTH {
12081208
let filename = path.file_name().unwrap().to_string_lossy();
12091209
let hash_len = 64 / 4; // Hash64 is 64 bits encoded in hex
1210-
let stripped_len = filename.len() - MAX_FILENAME_LENGTH + hash_len;
1210+
let hyphen_len = 1; // the '-' we insert between hash and suffix
1211+
1212+
// number of bytes of suffix we can keep so that "hash-<suffix>" fits
1213+
let allowed_suffix = MAX_FILENAME_LENGTH.saturating_sub(hash_len + hyphen_len);
1214+
1215+
// number of bytes to remove from the start
1216+
let stripped_bytes = filename.len().saturating_sub(allowed_suffix);
1217+
1218+
// ensure we don't cut in a middle of a char
1219+
let split_at = filename.ceil_char_boundary(stripped_bytes);
12111220

12121221
let mut hasher = StableHasher::new();
1213-
filename[..stripped_len].hash(&mut hasher);
1222+
filename[..split_at].hash(&mut hasher);
12141223
let hash = hasher.finish::<Hash64>();
12151224

1216-
path.set_file_name(format!("{:x}-{}", hash, &filename[stripped_len..]));
1225+
path.set_file_name(format!("{:x}-{}", hash, &filename[split_at..]));
12171226
}
12181227
path
12191228
}

compiler/rustc_session/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// tidy-alphabetical-start
22
#![allow(internal_features)]
3+
#![cfg_attr(bootstrap, feature(round_char_boundary))]
34
#![feature(default_field_values)]
45
#![feature(iter_intersperse)]
56
#![feature(rustc_attrs)]

tests/run-make/lto-long-filenames/rmake.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
//@ ignore-cross-compile
1111

12-
use std::fs;
13-
1412
use run_make_support::{rfs, rustc};
1513

1614
// This test make sure we don't get such following error:
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//@ ignore-cross-compile
2+
// gnu ld is confused with intermediate files having multibytes characters in their names:
3+
// = note: ld.exe: cannot find f0d5ff18d6510ebc-???_???_??????????_?_?????_?_???????.d50c2 \
4+
// 4c0c4ea93cc-cgu.0.rcgu.o: Invalid argument
5+
// as this is not something rustc can fix by itself,
6+
// we just skip the test on windows-gnu for now. Hence:
7+
//@ ignore-windows-gnu
8+
9+
use run_make_support::{rfs, rustc};
10+
11+
// This test make sure we don't crash when lto creates output files with long names.
12+
// cn characters can be multi-byte and thus trigger the long filename reduction code more easily.
13+
// we need to make sure that the code is properly generating names at char boundaries.
14+
// as reported in issue #147975
15+
fn main() {
16+
let lto_flags = ["-Clto", "-Clto=yes", "-Clto=off", "-Clto=thin", "-Clto=fat"];
17+
for prefix_len in 0..4 {
18+
let prefix: String = std::iter::repeat("_").take(prefix_len).collect();
19+
let main_file = format!("{}ⵅⴻⵎⵎⴻⵎ_ⴷⵉⵎⴰ_ⵖⴻⴼ_ⵢⵉⵙⴻⴽⴽⵉⵍⴻⵏ_ⵏ_ⵡⴰⵟⴰⵙ_ⵏ_ⵢⵉⴱⵢⵜⴻⵏ.rs", prefix);
20+
rfs::write(&main_file, "fn main() {}\n");
21+
for flag in lto_flags {
22+
rustc().input(&main_file).arg(flag).run();
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)