diff options
| author | kennytm <kennytm@gmail.com> | 2018-04-17 01:50:58 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-04-17 01:50:58 +0800 |
| commit | ccd2c403ac6d1b92c49985d09c4b1d0af8f651e1 (patch) | |
| tree | 71a311ffb2516522446b559562f932678591696e | |
| parent | bf602952116262f261eec20a2dc085d915de7bc7 (diff) | |
| parent | 7ab31f6556c2cce433695b113f53d6275edd724d (diff) | |
| download | rust-ccd2c403ac6d1b92c49985d09c4b1d0af8f651e1.tar.gz rust-ccd2c403ac6d1b92c49985d09c4b1d0af8f651e1.zip | |
Rollup merge of #49606 - varkor:pipe-repair, r=alexcrichton
Prevent broken pipes causing ICEs As the private `std::io::print_to` panics if there is an I/O error, which is used by `println!`, the compiler would ICE if one attempted to use a broken pipe (e.g. `rustc --help | false`). This introduces a new (private) macro `try_println!` which allows us to avoid this. As a side note, it seems this macro might be useful publicly (and actually there seems to be [a crate specifically for this purpose](https://crates.io/crates/try_print/)), though that can probably be left for a future discussion. One slight alternative approach would be to simply early exit without an error (i.e. exit code `0`), which [this comment](https://github.com/rust-lang/rust/issues/34376#issuecomment-377822526) suggests is the usual approach. I've opted not to take that approach initially, because I think it's more helpful to know when there is a broken pipe. Fixes #34376.
| -rw-r--r-- | src/librustc_driver/lib.rs | 12 | ||||
| -rw-r--r-- | src/librustdoc/lib.rs | 1 | ||||
| -rw-r--r-- | src/libstd/sys/unix/mod.rs | 4 | ||||
| -rw-r--r-- | src/rustc/rustc.rs | 5 |
4 files changed, 19 insertions, 3 deletions
diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs index 3dec84d174d..cb91ff6a43e 100644 --- a/src/librustc_driver/lib.rs +++ b/src/librustc_driver/lib.rs @@ -547,6 +547,18 @@ fn run_compiler_impl<'a>(args: &[String], (result, Some(sess)) } +#[cfg(unix)] +pub fn set_sigpipe_handler() { + unsafe { + // Set the SIGPIPE signal handler, so that an EPIPE + // will cause rustc to terminate, as expected. + assert!(libc::signal(libc::SIGPIPE, libc::SIG_DFL) != libc::SIG_ERR); + } +} + +#[cfg(windows)] +pub fn set_sigpipe_handler() {} + // Extract output directory and file from matches. fn make_output(matches: &getopts::Matches) -> (Option<PathBuf>, Option<PathBuf>) { let odir = matches.opt_str("out-dir").map(|o| PathBuf::from(&o)); diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index 1339a66f8b2..148a57c420f 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -100,6 +100,7 @@ struct Output { pub fn main() { const STACK_SIZE: usize = 32_000_000; // 32MB + rustc_driver::set_sigpipe_handler(); env_logger::init(); let res = std::thread::Builder::new().stack_size(STACK_SIZE).spawn(move || { syntax::with_globals(move || { diff --git a/src/libstd/sys/unix/mod.rs b/src/libstd/sys/unix/mod.rs index 9bdea945ea4..c1298e5040d 100644 --- a/src/libstd/sys/unix/mod.rs +++ b/src/libstd/sys/unix/mod.rs @@ -80,11 +80,11 @@ pub fn init() { reset_sigpipe(); } - #[cfg(not(any(target_os = "emscripten", target_os="fuchsia")))] + #[cfg(not(any(target_os = "emscripten", target_os = "fuchsia")))] unsafe fn reset_sigpipe() { assert!(signal(libc::SIGPIPE, libc::SIG_IGN) != libc::SIG_ERR); } - #[cfg(any(target_os = "emscripten", target_os="fuchsia"))] + #[cfg(any(target_os = "emscripten", target_os = "fuchsia"))] unsafe fn reset_sigpipe() {} } diff --git a/src/rustc/rustc.rs b/src/rustc/rustc.rs index a888838ce43..ab5a7c3f747 100644 --- a/src/rustc/rustc.rs +++ b/src/rustc/rustc.rs @@ -23,4 +23,7 @@ extern {} extern crate rustc_driver; -fn main() { rustc_driver::main() } +fn main() { + rustc_driver::set_sigpipe_handler(); + rustc_driver::main() +} |
