avoid env::var which requires valid UTF-8

This commit is contained in:
Ralf Jung 2020-05-09 11:58:18 +02:00
parent 20097be2fe
commit 024cc435f4
2 changed files with 14 additions and 10 deletions

View File

@ -269,7 +269,7 @@ fn ask_to_run(mut cmd: Command, ask: bool, text: &str) {
/// `MIRI_SYSROOT`. Skipped if `MIRI_SYSROOT` is already set, in which case we expect the user has
/// done all this already.
fn setup(subcommand: MiriCommand) {
if std::env::var("MIRI_SYSROOT").is_ok() {
if std::env::var_os("MIRI_SYSROOT").is_some() {
if subcommand == MiriCommand::Setup {
println!("WARNING: MIRI_SYSROOT already set, not doing anything.")
}
@ -282,7 +282,7 @@ fn setup(subcommand: MiriCommand) {
// First, we need xargo.
if xargo_version().map_or(true, |v| v < XARGO_MIN_VERSION) {
if std::env::var("XARGO_CHECK").is_ok() {
if std::env::var_os("XARGO_CHECK").is_some() {
// The user manually gave us a xargo binary; don't do anything automatically.
show_error(format!("Your xargo is too old; please upgrade to the latest version"))
}
@ -292,9 +292,9 @@ fn setup(subcommand: MiriCommand) {
}
// Determine where the rust sources are located. `XARGO_RUST_SRC` env var trumps everything.
let rust_src = match std::env::var("XARGO_RUST_SRC") {
Ok(val) => PathBuf::from(val),
Err(_) => {
let rust_src = match std::env::var_os("XARGO_RUST_SRC") {
Some(val) => PathBuf::from(val),
None => {
// Check for `rust-src` rustup component.
let sysroot = rustc()
.args(&["--print", "sysroot"])
@ -522,7 +522,7 @@ fn is_runnable_crate() -> bool {
is_bin || is_test
}
let verbose = std::env::var("MIRI_VERBOSE").is_ok();
let verbose = std::env::var_os("MIRI_VERBOSE").is_some();
let target_crate = is_target_crate();
// Figure out which arguments we need to pass.
@ -531,6 +531,7 @@ fn is_runnable_crate() -> bool {
// other args for target crates - that is, crates which are ultimately
// going to get interpreted by Miri.
if target_crate {
// FIXME: breaks for non-UTF-8 sysroots (use `var_os` instead).
let sysroot =
std::env::var("MIRI_SYSROOT").expect("The wrapper should have set MIRI_SYSROOT");
args.push("--sysroot".to_owned());
@ -545,6 +546,8 @@ fn is_runnable_crate() -> bool {
// we want to interpret under Miri. We deserialize the user-provided arguments
// from the special environment variable "MIRI_ARGS", and feed them
// to the 'miri' binary.
//
// `env::var` is okay here, well-formed JSON is always UTF-8.
let magic = std::env::var("MIRI_ARGS").expect("missing MIRI_ARGS");
let mut user_args: Vec<String> =
serde_json::from_str(&magic).expect("failed to deserialize MIRI_ARGS");

View File

@ -61,7 +61,7 @@ fn init_early_loggers() {
// If it is not set, we avoid initializing now so that we can initialize
// later with our custom settings, and *not* log anything for what happens before
// `miri` gets started.
if env::var("RUSTC_LOG").is_ok() {
if env::var_os("RUSTC_LOG").is_some() {
rustc_driver::init_rustc_env_logger();
}
}
@ -69,8 +69,9 @@ fn init_early_loggers() {
fn init_late_loggers(tcx: TyCtxt<'_>) {
// We initialize loggers right before we start evaluation. We overwrite the `RUSTC_LOG`
// env var if it is not set, control it based on `MIRI_LOG`.
// (FIXE: use `var_os`, but then we need to manually concatenate instead of `format!`.)
if let Ok(var) = env::var("MIRI_LOG") {
if env::var("RUSTC_LOG").is_err() {
if env::var_os("RUSTC_LOG").is_none() {
// We try to be a bit clever here: if `MIRI_LOG` is just a single level
// used for everything, we only apply it to the parts of rustc that are
// CTFE-related. Otherwise, we use it verbatim for `RUSTC_LOG`.
@ -90,8 +91,8 @@ fn init_late_loggers(tcx: TyCtxt<'_>) {
// If `MIRI_BACKTRACE` is set and `RUSTC_CTFE_BACKTRACE` is not, set `RUSTC_CTFE_BACKTRACE`.
// Do this late, so we ideally only apply this to Miri's errors.
if let Ok(val) = env::var("MIRI_BACKTRACE") {
let ctfe_backtrace = match &*val {
if let Some(val) = env::var_os("MIRI_BACKTRACE") {
let ctfe_backtrace = match &*val.to_string_lossy() {
"immediate" => CtfeBacktrace::Immediate,
"0" => CtfeBacktrace::Disabled,
_ => CtfeBacktrace::Capture,