Auto merge of #124112 - beetrees:incremental-os-str, r=Nadrieril

Fix ICE when there is a non-Unicode entry in the incremental crate directory

Fix the ICE that occurs when there is a non-Unicode entry in the incremental crate directory by replacing uses of `to_string_lossy` + `assert_no_characters_lost` with `to_str`. The added test would cause the compiler to ICE before this PR.
This commit is contained in:
bors 2024-04-22 04:00:58 +00:00
commit 32885838c0
3 changed files with 46 additions and 17 deletions

View File

@ -162,8 +162,11 @@ pub fn query_cache_path(sess: &Session) -> PathBuf {
fn lock_file_path(session_dir: &Path) -> PathBuf { fn lock_file_path(session_dir: &Path) -> PathBuf {
let crate_dir = session_dir.parent().unwrap(); let crate_dir = session_dir.parent().unwrap();
let directory_name = session_dir.file_name().unwrap().to_string_lossy(); let directory_name = session_dir
assert_no_characters_lost(&directory_name); .file_name()
.unwrap()
.to_str()
.expect("malformed session dir name: contains non-Unicode characters");
let dash_indices: Vec<_> = directory_name.match_indices('-').map(|(idx, _)| idx).collect(); let dash_indices: Vec<_> = directory_name.match_indices('-').map(|(idx, _)| idx).collect();
if dash_indices.len() != 3 { if dash_indices.len() != 3 {
@ -329,8 +332,11 @@ pub fn finalize_session_directory(sess: &Session, svh: Option<Svh>) {
debug!("finalize_session_directory() - session directory: {}", incr_comp_session_dir.display()); debug!("finalize_session_directory() - session directory: {}", incr_comp_session_dir.display());
let old_sub_dir_name = incr_comp_session_dir.file_name().unwrap().to_string_lossy(); let old_sub_dir_name = incr_comp_session_dir
assert_no_characters_lost(&old_sub_dir_name); .file_name()
.unwrap()
.to_str()
.expect("malformed session dir name: contains non-Unicode characters");
// Keep the 's-{timestamp}-{random-number}' prefix, but replace the // Keep the 's-{timestamp}-{random-number}' prefix, but replace the
// '-working' part with the SVH of the crate // '-working' part with the SVH of the crate
@ -527,8 +533,10 @@ where
for session_dir in iter { for session_dir in iter {
debug!("find_source_directory_in_iter - inspecting `{}`", session_dir.display()); debug!("find_source_directory_in_iter - inspecting `{}`", session_dir.display());
let directory_name = session_dir.file_name().unwrap().to_string_lossy(); let Some(directory_name) = session_dir.file_name().unwrap().to_str() else {
assert_no_characters_lost(&directory_name); debug!("find_source_directory_in_iter - ignoring");
continue;
};
if source_directories_already_tried.contains(&session_dir) if source_directories_already_tried.contains(&session_dir)
|| !is_session_directory(&directory_name) || !is_session_directory(&directory_name)
@ -619,12 +627,6 @@ fn crate_path(sess: &Session) -> PathBuf {
incr_dir.join(crate_name) incr_dir.join(crate_name)
} }
fn assert_no_characters_lost(s: &str) {
if s.contains('\u{FFFD}') {
bug!("Could not losslessly convert '{}'.", s)
}
}
fn is_old_enough_to_be_collected(timestamp: SystemTime) -> bool { fn is_old_enough_to_be_collected(timestamp: SystemTime) -> bool {
timestamp < SystemTime::now() - Duration::from_secs(10) timestamp < SystemTime::now() - Duration::from_secs(10)
} }
@ -657,14 +659,14 @@ pub(crate) fn garbage_collect_session_directories(sess: &Session) -> io::Result<
}; };
let entry_name = dir_entry.file_name(); let entry_name = dir_entry.file_name();
let entry_name = entry_name.to_string_lossy(); let Some(entry_name) = entry_name.to_str() else {
continue;
};
if is_session_directory_lock_file(&entry_name) { if is_session_directory_lock_file(&entry_name) {
assert_no_characters_lost(&entry_name); lock_files.insert(entry_name.to_string());
lock_files.insert(entry_name.into_owned());
} else if is_session_directory(&entry_name) { } else if is_session_directory(&entry_name) {
assert_no_characters_lost(&entry_name); session_directories.insert(entry_name.to_string());
session_directories.insert(entry_name.into_owned());
} else { } else {
// This is something we don't know, leave it alone // This is something we don't know, leave it alone
} }

View File

@ -0,0 +1 @@
fn main() {}

View File

@ -0,0 +1,26 @@
extern crate run_make_support;
use run_make_support::{rustc, tmp_dir};
fn main() {
#[cfg(unix)]
let non_unicode: &std::ffi::OsStr = std::os::unix::ffi::OsStrExt::from_bytes(&[0xFF]);
#[cfg(windows)]
let non_unicode: std::ffi::OsString = std::os::windows::ffi::OsStringExt::from_wide(&[0xD800]);
match std::fs::create_dir(tmp_dir().join(&non_unicode)) {
// If an error occurs, check if creating a directory with a valid Unicode name would
// succeed.
Err(e) if std::fs::create_dir(tmp_dir().join("valid_unicode")).is_ok() => {
// Filesystem doesn't appear support non-Unicode paths.
return;
}
Err(e) => panic!("error creating non-Unicode directory: {e}"),
_ => {}
}
let incr_dir = tmp_dir().join("incr-dir");
rustc().input("foo.rs").incremental(&incr_dir).run();
for crate_dir in std::fs::read_dir(&incr_dir).unwrap() {
std::fs::create_dir(crate_dir.unwrap().path().join(&non_unicode)).unwrap();
}
rustc().input("foo.rs").incremental(&incr_dir).run();
}