Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ICE when reading non-UTF-8 input from stdin #47895

Merged
merged 3 commits into from
Jan 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions src/librustc_driver/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
};
Expand All @@ -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));
Expand Down Expand Up @@ -512,17 +522,22 @@ fn make_output(matches: &getopts::Matches) -> (Option<PathBuf>, Option<PathBuf>)
}

// Extract input (string or file and optional path) from matches.
fn make_input(free_matches: &[String]) -> Option<(Input, Option<PathBuf>)> {
fn make_input(free_matches: &[String]) -> Option<(Input, Option<PathBuf>, Option<io::Error>)> {
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
Expand Down
6 changes: 6 additions & 0 deletions src/test/run-make/stdin-non-utf8/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-include ../tools.mk

all:
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"
1 change: 1 addition & 0 deletions src/test/run-make/stdin-non-utf8/non-utf8
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@