Auto merge of #148018 - tardyp:lto_big_filesize_utf8, r=JonathanBrouwer

fix panic when rustc tries to reduce intermediate filenames len with utf8

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.

Fix rust-lang/rust#147975
This commit is contained in:
bors
2025-10-24 18:23:10 +00:00
3 changed files with 37 additions and 5 deletions

View File

@@ -1211,13 +1211,22 @@ fn maybe_strip_file_name(mut path: PathBuf) -> PathBuf {
if path.file_name().map_or(0, |name| name.len()) > MAX_FILENAME_LENGTH {
let filename = path.file_name().unwrap().to_string_lossy();
let hash_len = 64 / 4; // Hash64 is 64 bits encoded in hex
let stripped_len = filename.len() - MAX_FILENAME_LENGTH + hash_len;
let hyphen_len = 1; // the '-' we insert between hash and suffix
// number of bytes of suffix we can keep so that "hash-<suffix>" fits
let allowed_suffix = MAX_FILENAME_LENGTH.saturating_sub(hash_len + hyphen_len);
// number of bytes to remove from the start
let stripped_bytes = filename.len().saturating_sub(allowed_suffix);
// ensure we don't cut in a middle of a char
let split_at = filename.ceil_char_boundary(stripped_bytes);
let mut hasher = StableHasher::new();
filename[..stripped_len].hash(&mut hasher);
filename[..split_at].hash(&mut hasher);
let hash = hasher.finish::<Hash64>();
path.set_file_name(format!("{:x}-{}", hash, &filename[stripped_len..]));
path.set_file_name(format!("{:x}-{}", hash, &filename[split_at..]));
}
path
}