From c73925452c870f028ab393acf8edc5ceea382fcb Mon Sep 17 00:00:00 2001 From: varkor Date: Wed, 31 Jan 2018 00:59:20 +0000 Subject: [PATCH 1/3] Fix ICE when reading non-UTF-8 input from stdin Fixes #22387. --- src/librustc_driver/lib.rs | 29 +++++++++++++++++------ src/test/run-make/stdin-non-utf8/Makefile | 5 ++++ 2 files changed, 27 insertions(+), 7 deletions(-) create mode 100644 src/test/run-make/stdin-non-utf8/Makefile diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs index 6118ee94c84cf..2f083c2193a21 100644 --- a/src/librustc_driver/lib.rs +++ b/src/librustc_driver/lib.rs @@ -456,10 +456,13 @@ pub fn run_compiler<'a>(args: &[String], None); let (odir, ofile) = make_output(&matches); - let (input, input_file_path) = match make_input(&matches.free) { - Some((input, input_file_path)) => callbacks.some_input(input, input_file_path), + let (input, input_file_path, input_err) = match make_input(&matches.free) { + Some((input, input_file_path, input_err)) => { + let (input, input_file_path) = callbacks.some_input(input, input_file_path); + (input, input_file_path, input_err) + }, None => match callbacks.no_input(&matches, &sopts, &cfg, &odir, &ofile, &descriptions) { - Some((input, input_file_path)) => (input, input_file_path), + Some((input, input_file_path)) => (input, input_file_path, None), None => return (Ok(()), None), }, }; @@ -470,6 +473,13 @@ pub fn run_compiler<'a>(args: &[String], sopts, input_file_path.clone(), descriptions, codemap, emitter_dest, ); + if let Some(err) = input_err { + // Immediately stop compilation if there was an issue reading + // the input (for example if the input stream is not UTF-8). + sess.err(&format!("{}", err)); + return (Err(CompileIncomplete::Stopped), Some(sess)); + } + let trans = get_trans(&sess); rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess)); @@ -512,17 +522,22 @@ fn make_output(matches: &getopts::Matches) -> (Option, Option) } // Extract input (string or file and optional path) from matches. -fn make_input(free_matches: &[String]) -> Option<(Input, Option)> { +fn make_input(free_matches: &[String]) -> Option<(Input, Option, Option)> { if free_matches.len() == 1 { let ifile = &free_matches[0]; if ifile == "-" { let mut src = String::new(); - io::stdin().read_to_string(&mut src).unwrap(); + let err = if io::stdin().read_to_string(&mut src).is_err() { + Some(io::Error::new(io::ErrorKind::InvalidData, + "couldn't read from stdin, as it did not contain valid UTF-8")) + } else { + None + }; Some((Input::Str { name: FileName::Anon, input: src }, - None)) + None, err)) } else { Some((Input::File(PathBuf::from(ifile)), - Some(PathBuf::from(ifile)))) + Some(PathBuf::from(ifile)), None)) } } else { None diff --git a/src/test/run-make/stdin-non-utf8/Makefile b/src/test/run-make/stdin-non-utf8/Makefile new file mode 100644 index 0000000000000..307f3d85c0067 --- /dev/null +++ b/src/test/run-make/stdin-non-utf8/Makefile @@ -0,0 +1,5 @@ +-include ../tools.mk + +all: + echo '\xD2' | $(RUSTC) - 2>&1 \ + | $(CGREP) "error: couldn't read from stdin, as it did not contain valid UTF-8" From 00a33d685be2d325381a5b2842774f4ad1039dff Mon Sep 17 00:00:00 2001 From: varkor Date: Wed, 31 Jan 2018 10:23:15 +0000 Subject: [PATCH 2/3] Add echo escape flag --- src/test/run-make/stdin-non-utf8/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/run-make/stdin-non-utf8/Makefile b/src/test/run-make/stdin-non-utf8/Makefile index 307f3d85c0067..f883e546e9c9b 100644 --- a/src/test/run-make/stdin-non-utf8/Makefile +++ b/src/test/run-make/stdin-non-utf8/Makefile @@ -1,5 +1,5 @@ -include ../tools.mk all: - echo '\xD2' | $(RUSTC) - 2>&1 \ + echo -e '\xD2' | $(RUSTC) - 2>&1 \ | $(CGREP) "error: couldn't read from stdin, as it did not contain valid UTF-8" From a43d7eb3dd9cb97a81374001a23456bbee008d65 Mon Sep 17 00:00:00 2001 From: varkor Date: Wed, 31 Jan 2018 11:56:15 +0000 Subject: [PATCH 3/3] Use file containing non-UTF-8 character instead of echo -e --- src/test/run-make/stdin-non-utf8/Makefile | 3 ++- src/test/run-make/stdin-non-utf8/non-utf8 | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 src/test/run-make/stdin-non-utf8/non-utf8 diff --git a/src/test/run-make/stdin-non-utf8/Makefile b/src/test/run-make/stdin-non-utf8/Makefile index f883e546e9c9b..7948c442616e3 100644 --- a/src/test/run-make/stdin-non-utf8/Makefile +++ b/src/test/run-make/stdin-non-utf8/Makefile @@ -1,5 +1,6 @@ -include ../tools.mk all: - echo -e '\xD2' | $(RUSTC) - 2>&1 \ + cp non-utf8 $(TMPDIR)/non-utf.rs + cat $(TMPDIR)/non-utf.rs | $(RUSTC) - 2>&1 \ | $(CGREP) "error: couldn't read from stdin, as it did not contain valid UTF-8" diff --git a/src/test/run-make/stdin-non-utf8/non-utf8 b/src/test/run-make/stdin-non-utf8/non-utf8 new file mode 100644 index 0000000000000..bc87051a85299 --- /dev/null +++ b/src/test/run-make/stdin-non-utf8/non-utf8 @@ -0,0 +1 @@ +�