Skip to content

Commit e027c4b

Browse files
committed
Auto merge of #11420 - epage:atty, r=weihanglo
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
2 parents 79fe757 + 48895b1 commit e027c4b

File tree

4 files changed

+25
-14
lines changed

4 files changed

+25
-14
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ name = "cargo"
1616
path = "src/cargo/lib.rs"
1717

1818
[dependencies]
19-
atty = "0.2"
2019
bytesize = "1.0"
2120
cargo-platform = { path = "crates/cargo-platform", version = "0.1.2" }
2221
cargo-util = { path = "crates/cargo-util", version = "0.2.3" }
@@ -37,6 +36,7 @@ http-auth = { version = "0.1.6", default-features = false }
3736
humantime = "2.0.0"
3837
indexmap = "1"
3938
ignore = "0.4.7"
39+
is-terminal = "0.4.0"
4040
lazy_static = "1.2.0"
4141
jobserver = "0.1.24"
4242
lazycell = "1.2.0"

crates/resolver-tests/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ cargo-util = { path = "../cargo-util" }
99
proptest = "0.9.1"
1010
lazy_static = "1.3.0"
1111
varisat = "0.2.1"
12-
atty = "0.2.11"
12+
is-terminal = "0.4.0"

crates/resolver-tests/tests/resolve.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use proptest::prelude::*;
2121
proptest! {
2222
#![proptest_config(ProptestConfig {
2323
max_shrink_iters:
24-
if is_ci() || !atty::is(atty::Stream::Stderr) {
24+
if is_ci() || !is_terminal::IsTerminal::is_terminal(&std::io::stderr()){
2525
// This attempts to make sure that CI will fail fast,
2626
0
2727
} else {

src/cargo/core/shell.rs

+22-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::fmt;
22
use std::io::prelude::*;
33

4+
use is_terminal::IsTerminal;
45
use termcolor::Color::{Cyan, Green, Red, Yellow};
56
use termcolor::{self, Color, ColorSpec, StandardStream, WriteColor};
67

@@ -99,14 +100,10 @@ impl Shell {
99100
let auto_clr = ColorChoice::CargoAuto;
100101
Shell {
101102
output: ShellOut::Stream {
102-
stdout: StandardStream::stdout(
103-
auto_clr.to_termcolor_color_choice(atty::Stream::Stdout),
104-
),
105-
stderr: StandardStream::stderr(
106-
auto_clr.to_termcolor_color_choice(atty::Stream::Stderr),
107-
),
103+
stdout: StandardStream::stdout(auto_clr.to_termcolor_color_choice(Stream::Stdout)),
104+
stderr: StandardStream::stderr(auto_clr.to_termcolor_color_choice(Stream::Stderr)),
108105
color_choice: ColorChoice::CargoAuto,
109-
stderr_tty: atty::is(atty::Stream::Stderr),
106+
stderr_tty: std::io::stderr().is_terminal(),
110107
},
111108
verbosity: Verbosity::Verbose,
112109
needs_clear: false,
@@ -301,8 +298,8 @@ impl Shell {
301298
),
302299
};
303300
*color_choice = cfg;
304-
*stdout = StandardStream::stdout(cfg.to_termcolor_color_choice(atty::Stream::Stdout));
305-
*stderr = StandardStream::stderr(cfg.to_termcolor_color_choice(atty::Stream::Stderr));
301+
*stdout = StandardStream::stdout(cfg.to_termcolor_color_choice(Stream::Stdout));
302+
*stderr = StandardStream::stderr(cfg.to_termcolor_color_choice(Stream::Stderr));
306303
}
307304
Ok(())
308305
}
@@ -496,12 +493,12 @@ impl ShellOut {
496493

497494
impl ColorChoice {
498495
/// Converts our color choice to termcolor's version.
499-
fn to_termcolor_color_choice(self, stream: atty::Stream) -> termcolor::ColorChoice {
496+
fn to_termcolor_color_choice(self, stream: Stream) -> termcolor::ColorChoice {
500497
match self {
501498
ColorChoice::Always => termcolor::ColorChoice::Always,
502499
ColorChoice::Never => termcolor::ColorChoice::Never,
503500
ColorChoice::CargoAuto => {
504-
if atty::is(stream) {
501+
if stream.is_terminal() {
505502
termcolor::ColorChoice::Auto
506503
} else {
507504
termcolor::ColorChoice::Never
@@ -511,6 +508,20 @@ impl ColorChoice {
511508
}
512509
}
513510

511+
enum Stream {
512+
Stdout,
513+
Stderr,
514+
}
515+
516+
impl Stream {
517+
fn is_terminal(self) -> bool {
518+
match self {
519+
Self::Stdout => std::io::stdout().is_terminal(),
520+
Self::Stderr => std::io::stderr().is_terminal(),
521+
}
522+
}
523+
}
524+
514525
#[cfg(unix)]
515526
mod imp {
516527
use super::{Shell, TtyWidth};

0 commit comments

Comments
 (0)