Skip to content

Commit a081007

Browse files
committed
rustc_driver: avoid fallible conversions
Use `std::path::PathBuf` rather than `String`; use `std::env::var_os` rather than `std::env::var`. These changes avoid a number of error paths which can arise in the presence of non-UTF-8 paths.
1 parent 7654d4b commit a081007

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

compiler/rustc_driver_impl/src/lib.rs

+15-12
Original file line numberDiff line numberDiff line change
@@ -1290,14 +1290,18 @@ pub fn ice_path() -> &'static Option<PathBuf> {
12901290
if let Some(s) = std::env::var_os("RUST_BACKTRACE") && s == "0" {
12911291
return None;
12921292
}
1293-
let mut path = match std::env::var("RUSTC_ICE").as_deref() {
1294-
// Explicitly opting out of writing ICEs to disk.
1295-
Ok("0") => return None,
1296-
Ok(s) => PathBuf::from(s),
1297-
Err(_) => std::env::current_dir().unwrap_or_default(),
1293+
let mut path = match std::env::var_os("RUSTC_ICE") {
1294+
Some(s) => {
1295+
if s == "0" {
1296+
// Explicitly opting out of writing ICEs to disk.
1297+
return None;
1298+
}
1299+
PathBuf::from(s)
1300+
}
1301+
None => std::env::current_dir().unwrap_or_default(),
12981302
};
12991303
let now: OffsetDateTime = SystemTime::now().into();
1300-
let file_now = now.format(&Rfc3339).unwrap_or(String::new());
1304+
let file_now = now.format(&Rfc3339).unwrap_or_default();
13011305
let pid = std::process::id();
13021306
path.push(format!("rustc-ice-{file_now}-{pid}.txt"));
13031307
Some(path)
@@ -1411,12 +1415,11 @@ pub fn report_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str, extra_info:
14111415

14121416
static FIRST_PANIC: AtomicBool = AtomicBool::new(true);
14131417

1414-
let file = if let Some(path) = ice_path().as_ref() {
1418+
let file = if let Some(path) = ice_path() {
14151419
// Create the ICE dump target file.
14161420
match crate::fs::File::options().create(true).append(true).open(&path) {
14171421
Ok(mut file) => {
1418-
handler
1419-
.emit_note(session_diagnostics::IcePath { path: path.display().to_string() });
1422+
handler.emit_note(session_diagnostics::IcePath { path: path.clone() });
14201423
if FIRST_PANIC.swap(false, Ordering::SeqCst) {
14211424
let _ = write!(file, "\n\nrustc version: {version}\nplatform: {triple}");
14221425
}
@@ -1425,10 +1428,10 @@ pub fn report_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str, extra_info:
14251428
Err(err) => {
14261429
// The path ICE couldn't be written to disk, provide feedback to the user as to why.
14271430
handler.emit_warning(session_diagnostics::IcePathError {
1428-
path: path.display().to_string(),
1431+
path: path.clone(),
14291432
error: err.to_string(),
1430-
env_var: std::env::var("RUSTC_ICE")
1431-
.ok()
1433+
env_var: std::env::var_os("RUSTC_ICE")
1434+
.map(PathBuf::from)
14321435
.map(|env_var| session_diagnostics::IcePathErrorEnv { env_var }),
14331436
});
14341437
handler.emit_note(session_diagnostics::IceVersion { version, triple });

compiler/rustc_driver_impl/src/session_diagnostics.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ pub(crate) struct IceVersion<'a> {
5252
#[derive(Diagnostic)]
5353
#[diag(driver_impl_ice_path)]
5454
pub(crate) struct IcePath {
55-
pub path: String,
55+
pub path: std::path::PathBuf,
5656
}
5757

5858
#[derive(Diagnostic)]
5959
#[diag(driver_impl_ice_path_error)]
6060
pub(crate) struct IcePathError {
61-
pub path: String,
61+
pub path: std::path::PathBuf,
6262
pub error: String,
6363
#[subdiagnostic]
6464
pub env_var: Option<IcePathErrorEnv>,
@@ -67,7 +67,7 @@ pub(crate) struct IcePathError {
6767
#[derive(Subdiagnostic)]
6868
#[note(driver_impl_ice_path_error_env)]
6969
pub(crate) struct IcePathErrorEnv {
70-
pub env_var: String,
70+
pub env_var: std::path::PathBuf,
7171
}
7272

7373
#[derive(Diagnostic)]

0 commit comments

Comments
 (0)