Skip to content

Commit

Permalink
Auto merge of #124112 - beetrees:incremental-os-str, r=Nadrieril
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Apr 22, 2024
2 parents eb70af2 + 71f751d commit 3288583
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 17 deletions.
36 changes: 19 additions & 17 deletions compiler/rustc_incremental/src/persist/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,11 @@ pub fn query_cache_path(sess: &Session) -> PathBuf {
fn lock_file_path(session_dir: &Path) -> PathBuf {
let crate_dir = session_dir.parent().unwrap();

let directory_name = session_dir.file_name().unwrap().to_string_lossy();
assert_no_characters_lost(&directory_name);
let directory_name = session_dir
.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();
if dash_indices.len() != 3 {
Expand Down Expand Up @@ -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());

let old_sub_dir_name = incr_comp_session_dir.file_name().unwrap().to_string_lossy();
assert_no_characters_lost(&old_sub_dir_name);
let old_sub_dir_name = incr_comp_session_dir
.file_name()
.unwrap()
.to_str()
.expect("malformed session dir name: contains non-Unicode characters");

// Keep the 's-{timestamp}-{random-number}' prefix, but replace the
// '-working' part with the SVH of the crate
Expand Down Expand Up @@ -527,8 +533,10 @@ where
for session_dir in iter {
debug!("find_source_directory_in_iter - inspecting `{}`", session_dir.display());

let directory_name = session_dir.file_name().unwrap().to_string_lossy();
assert_no_characters_lost(&directory_name);
let Some(directory_name) = session_dir.file_name().unwrap().to_str() else {
debug!("find_source_directory_in_iter - ignoring");
continue;
};

if source_directories_already_tried.contains(&session_dir)
|| !is_session_directory(&directory_name)
Expand Down Expand Up @@ -619,12 +627,6 @@ fn crate_path(sess: &Session) -> PathBuf {
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 {
timestamp < SystemTime::now() - Duration::from_secs(10)
}
Expand Down Expand Up @@ -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 = entry_name.to_string_lossy();
let Some(entry_name) = entry_name.to_str() else {
continue;
};

if is_session_directory_lock_file(&entry_name) {
assert_no_characters_lost(&entry_name);
lock_files.insert(entry_name.into_owned());
lock_files.insert(entry_name.to_string());
} else if is_session_directory(&entry_name) {
assert_no_characters_lost(&entry_name);
session_directories.insert(entry_name.into_owned());
session_directories.insert(entry_name.to_string());
} else {
// This is something we don't know, leave it alone
}
Expand Down
1 change: 1 addition & 0 deletions tests/run-make/non-unicode-in-incremental-dir/foo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn main() {}
26 changes: 26 additions & 0 deletions tests/run-make/non-unicode-in-incremental-dir/rmake.rs
Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit 3288583

Please sign in to comment.