From dd31a4736b2ef77c65062c6b647e9a89552530f6 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 24 Nov 2022 09:52:09 -0600 Subject: [PATCH] fix: Move off atty to resolve soundness issue There is a soundness issue with atty when building on Windows with a custom allocator. This PR switches direct dependencies on atty to is-terminal. New semver compatible versions of clap and snapbox remove atty. #11417 upgrades env_logger to remove it from there. Fixes #11415 --- Cargo.toml | 2 +- crates/resolver-tests/Cargo.toml | 2 +- crates/resolver-tests/tests/resolve.rs | 2 +- src/cargo/core/shell.rs | 33 +++++++++++++++++--------- 4 files changed, 25 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f94d1e858201..fd9b127faa4c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,7 @@ name = "cargo" path = "src/cargo/lib.rs" [dependencies] -atty = "0.2" +is-terminal = "0.4.0" bytesize = "1.0" cargo-platform = { path = "crates/cargo-platform", version = "0.1.2" } cargo-util = { path = "crates/cargo-util", version = "0.2.3" } diff --git a/crates/resolver-tests/Cargo.toml b/crates/resolver-tests/Cargo.toml index 23bb6000ad1a..fd2a723b01e7 100644 --- a/crates/resolver-tests/Cargo.toml +++ b/crates/resolver-tests/Cargo.toml @@ -9,4 +9,4 @@ cargo-util = { path = "../cargo-util" } proptest = "0.9.1" lazy_static = "1.3.0" varisat = "0.2.1" -atty = "0.2.11" +is-terminal = "0.4.0" diff --git a/crates/resolver-tests/tests/resolve.rs b/crates/resolver-tests/tests/resolve.rs index 6a065b938009..d4bfe90303c4 100644 --- a/crates/resolver-tests/tests/resolve.rs +++ b/crates/resolver-tests/tests/resolve.rs @@ -21,7 +21,7 @@ use proptest::prelude::*; proptest! { #![proptest_config(ProptestConfig { max_shrink_iters: - if is_ci() || !atty::is(atty::Stream::Stderr) { + if is_ci() || !is_terminal::IsTerminal::is_terminal(std::io::stderr()){ // This attempts to make sure that CI will fail fast, 0 } else { diff --git a/src/cargo/core/shell.rs b/src/cargo/core/shell.rs index d03ef8e8bd01..473c574fb53a 100644 --- a/src/cargo/core/shell.rs +++ b/src/cargo/core/shell.rs @@ -1,6 +1,7 @@ use std::fmt; use std::io::prelude::*; +use is_terminal::IsTerminal; use termcolor::Color::{Cyan, Green, Red, Yellow}; use termcolor::{self, Color, ColorSpec, StandardStream, WriteColor}; @@ -99,14 +100,10 @@ impl Shell { let auto_clr = ColorChoice::CargoAuto; Shell { output: ShellOut::Stream { - stdout: StandardStream::stdout( - auto_clr.to_termcolor_color_choice(atty::Stream::Stdout), - ), - stderr: StandardStream::stderr( - auto_clr.to_termcolor_color_choice(atty::Stream::Stderr), - ), + stdout: StandardStream::stdout(auto_clr.to_termcolor_color_choice(Stream::Stdout)), + stderr: StandardStream::stderr(auto_clr.to_termcolor_color_choice(Stream::Stderr)), color_choice: ColorChoice::CargoAuto, - stderr_tty: atty::is(atty::Stream::Stderr), + stderr_tty: std::io::stderr().is_terminal(), }, verbosity: Verbosity::Verbose, needs_clear: false, @@ -301,8 +298,8 @@ impl Shell { ), }; *color_choice = cfg; - *stdout = StandardStream::stdout(cfg.to_termcolor_color_choice(atty::Stream::Stdout)); - *stderr = StandardStream::stderr(cfg.to_termcolor_color_choice(atty::Stream::Stderr)); + *stdout = StandardStream::stdout(cfg.to_termcolor_color_choice(Stream::Stdout)); + *stderr = StandardStream::stderr(cfg.to_termcolor_color_choice(Stream::Stderr)); } Ok(()) } @@ -496,12 +493,12 @@ impl ShellOut { impl ColorChoice { /// Converts our color choice to termcolor's version. - fn to_termcolor_color_choice(self, stream: atty::Stream) -> termcolor::ColorChoice { + fn to_termcolor_color_choice(self, stream: Stream) -> termcolor::ColorChoice { match self { ColorChoice::Always => termcolor::ColorChoice::Always, ColorChoice::Never => termcolor::ColorChoice::Never, ColorChoice::CargoAuto => { - if atty::is(stream) { + if stream.is_terminal() { termcolor::ColorChoice::Auto } else { termcolor::ColorChoice::Never @@ -511,6 +508,20 @@ impl ColorChoice { } } +enum Stream { + Stdout, + Stderr, +} + +impl Stream { + fn is_terminal(self) -> bool { + match self { + Self::Stdout => std::io::stdout().is_terminal(), + Self::Stderr => std::io::stderr().is_terminal(), + } + } +} + #[cfg(unix)] mod imp { use super::{Shell, TtyWidth};