Skip to content

Commit 024cc43

Browse files
committed
avoid env::var which requires valid UTF-8
1 parent 20097be commit 024cc43

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

src/bin/cargo-miri.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ fn ask_to_run(mut cmd: Command, ask: bool, text: &str) {
269269
/// `MIRI_SYSROOT`. Skipped if `MIRI_SYSROOT` is already set, in which case we expect the user has
270270
/// done all this already.
271271
fn setup(subcommand: MiriCommand) {
272-
if std::env::var("MIRI_SYSROOT").is_ok() {
272+
if std::env::var_os("MIRI_SYSROOT").is_some() {
273273
if subcommand == MiriCommand::Setup {
274274
println!("WARNING: MIRI_SYSROOT already set, not doing anything.")
275275
}
@@ -282,7 +282,7 @@ fn setup(subcommand: MiriCommand) {
282282

283283
// First, we need xargo.
284284
if xargo_version().map_or(true, |v| v < XARGO_MIN_VERSION) {
285-
if std::env::var("XARGO_CHECK").is_ok() {
285+
if std::env::var_os("XARGO_CHECK").is_some() {
286286
// The user manually gave us a xargo binary; don't do anything automatically.
287287
show_error(format!("Your xargo is too old; please upgrade to the latest version"))
288288
}
@@ -292,9 +292,9 @@ fn setup(subcommand: MiriCommand) {
292292
}
293293

294294
// Determine where the rust sources are located. `XARGO_RUST_SRC` env var trumps everything.
295-
let rust_src = match std::env::var("XARGO_RUST_SRC") {
296-
Ok(val) => PathBuf::from(val),
297-
Err(_) => {
295+
let rust_src = match std::env::var_os("XARGO_RUST_SRC") {
296+
Some(val) => PathBuf::from(val),
297+
None => {
298298
// Check for `rust-src` rustup component.
299299
let sysroot = rustc()
300300
.args(&["--print", "sysroot"])
@@ -522,7 +522,7 @@ fn inside_cargo_rustc() {
522522
is_bin || is_test
523523
}
524524

525-
let verbose = std::env::var("MIRI_VERBOSE").is_ok();
525+
let verbose = std::env::var_os("MIRI_VERBOSE").is_some();
526526
let target_crate = is_target_crate();
527527

528528
// Figure out which arguments we need to pass.
@@ -531,6 +531,7 @@ fn inside_cargo_rustc() {
531531
// other args for target crates - that is, crates which are ultimately
532532
// going to get interpreted by Miri.
533533
if target_crate {
534+
// FIXME: breaks for non-UTF-8 sysroots (use `var_os` instead).
534535
let sysroot =
535536
std::env::var("MIRI_SYSROOT").expect("The wrapper should have set MIRI_SYSROOT");
536537
args.push("--sysroot".to_owned());
@@ -545,6 +546,8 @@ fn inside_cargo_rustc() {
545546
// we want to interpret under Miri. We deserialize the user-provided arguments
546547
// from the special environment variable "MIRI_ARGS", and feed them
547548
// to the 'miri' binary.
549+
//
550+
// `env::var` is okay here, well-formed JSON is always UTF-8.
548551
let magic = std::env::var("MIRI_ARGS").expect("missing MIRI_ARGS");
549552
let mut user_args: Vec<String> =
550553
serde_json::from_str(&magic).expect("failed to deserialize MIRI_ARGS");

src/bin/miri.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,17 @@ fn init_early_loggers() {
6161
// If it is not set, we avoid initializing now so that we can initialize
6262
// later with our custom settings, and *not* log anything for what happens before
6363
// `miri` gets started.
64-
if env::var("RUSTC_LOG").is_ok() {
64+
if env::var_os("RUSTC_LOG").is_some() {
6565
rustc_driver::init_rustc_env_logger();
6666
}
6767
}
6868

6969
fn init_late_loggers(tcx: TyCtxt<'_>) {
7070
// We initialize loggers right before we start evaluation. We overwrite the `RUSTC_LOG`
7171
// env var if it is not set, control it based on `MIRI_LOG`.
72+
// (FIXE: use `var_os`, but then we need to manually concatenate instead of `format!`.)
7273
if let Ok(var) = env::var("MIRI_LOG") {
73-
if env::var("RUSTC_LOG").is_err() {
74+
if env::var_os("RUSTC_LOG").is_none() {
7475
// We try to be a bit clever here: if `MIRI_LOG` is just a single level
7576
// used for everything, we only apply it to the parts of rustc that are
7677
// CTFE-related. Otherwise, we use it verbatim for `RUSTC_LOG`.
@@ -90,8 +91,8 @@ fn init_late_loggers(tcx: TyCtxt<'_>) {
9091

9192
// If `MIRI_BACKTRACE` is set and `RUSTC_CTFE_BACKTRACE` is not, set `RUSTC_CTFE_BACKTRACE`.
9293
// Do this late, so we ideally only apply this to Miri's errors.
93-
if let Ok(val) = env::var("MIRI_BACKTRACE") {
94-
let ctfe_backtrace = match &*val {
94+
if let Some(val) = env::var_os("MIRI_BACKTRACE") {
95+
let ctfe_backtrace = match &*val.to_string_lossy() {
9596
"immediate" => CtfeBacktrace::Immediate,
9697
"0" => CtfeBacktrace::Disabled,
9798
_ => CtfeBacktrace::Capture,

0 commit comments

Comments
 (0)