Skip to content

Commit

Permalink
Auto merge of #11420 - epage:atty, r=weihanglo
Browse files Browse the repository at this point in the history
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 #11416
  • Loading branch information
bors committed Nov 25, 2022
2 parents 79fe757 + 48895b1 commit e027c4b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ name = "cargo"
path = "src/cargo/lib.rs"

[dependencies]
atty = "0.2"
bytesize = "1.0"
cargo-platform = { path = "crates/cargo-platform", version = "0.1.2" }
cargo-util = { path = "crates/cargo-util", version = "0.2.3" }
Expand All @@ -37,6 +36,7 @@ http-auth = { version = "0.1.6", default-features = false }
humantime = "2.0.0"
indexmap = "1"
ignore = "0.4.7"
is-terminal = "0.4.0"
lazy_static = "1.2.0"
jobserver = "0.1.24"
lazycell = "1.2.0"
Expand Down
2 changes: 1 addition & 1 deletion crates/resolver-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 1 addition & 1 deletion crates/resolver-tests/tests/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
33 changes: 22 additions & 11 deletions src/cargo/core/shell.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(())
}
Expand Down Expand Up @@ -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
Expand All @@ -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};
Expand Down

0 comments on commit e027c4b

Please sign in to comment.