Skip to content

Unsoundness in Command::get_resolved_envs example #160074

Description

@tbu-

CC #148432
CC #149070

In the std::process::Command::get_resolved_envs example, we have the following code block:

#![feature(command_resolved_envs)]
use std::process::Command;
use std::ffi::{OsString, OsStr};
use std::env;
use std::collections::HashMap;

let mut cmd = Command::new("ls");
cmd.env("TZ", "UTC");
unsafe { env::set_var("EDITOR", "vim"); }

let resolved: HashMap<OsString, OsString> = cmd.get_resolved_envs().collect();
assert_eq!(resolved.get(OsStr::new("TZ")), Some(&OsString::from("UTC")));
assert_eq!(resolved.get(OsStr::new("EDITOR")), Some(&OsString::from("vim")));

The env::set_var runs concurrently to other doctests and is therefore unsound. Since it's an example for users to learn from, it should probably also mention to the user that setting env vars is unsafe in multi-threaded programs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions