Skip to content

Commit 86eb725

Browse files
committed
Rollup merge of #47895 - varkor:non-utf-stdin, r=estebank
Fix ICE when reading non-UTF-8 input from stdin Fixes #22387.
2 parents 61972e7 + a43d7eb commit 86eb725

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

src/librustc_driver/lib.rs

+22-7
Original file line numberDiff line numberDiff line change
@@ -456,10 +456,13 @@ pub fn run_compiler<'a>(args: &[String],
456456
None);
457457

458458
let (odir, ofile) = make_output(&matches);
459-
let (input, input_file_path) = match make_input(&matches.free) {
460-
Some((input, input_file_path)) => callbacks.some_input(input, input_file_path),
459+
let (input, input_file_path, input_err) = match make_input(&matches.free) {
460+
Some((input, input_file_path, input_err)) => {
461+
let (input, input_file_path) = callbacks.some_input(input, input_file_path);
462+
(input, input_file_path, input_err)
463+
},
461464
None => match callbacks.no_input(&matches, &sopts, &cfg, &odir, &ofile, &descriptions) {
462-
Some((input, input_file_path)) => (input, input_file_path),
465+
Some((input, input_file_path)) => (input, input_file_path, None),
463466
None => return (Ok(()), None),
464467
},
465468
};
@@ -470,6 +473,13 @@ pub fn run_compiler<'a>(args: &[String],
470473
sopts, input_file_path.clone(), descriptions, codemap, emitter_dest,
471474
);
472475

476+
if let Some(err) = input_err {
477+
// Immediately stop compilation if there was an issue reading
478+
// the input (for example if the input stream is not UTF-8).
479+
sess.err(&format!("{}", err));
480+
return (Err(CompileIncomplete::Stopped), Some(sess));
481+
}
482+
473483
let trans = get_trans(&sess);
474484

475485
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
@@ -512,17 +522,22 @@ fn make_output(matches: &getopts::Matches) -> (Option<PathBuf>, Option<PathBuf>)
512522
}
513523

514524
// Extract input (string or file and optional path) from matches.
515-
fn make_input(free_matches: &[String]) -> Option<(Input, Option<PathBuf>)> {
525+
fn make_input(free_matches: &[String]) -> Option<(Input, Option<PathBuf>, Option<io::Error>)> {
516526
if free_matches.len() == 1 {
517527
let ifile = &free_matches[0];
518528
if ifile == "-" {
519529
let mut src = String::new();
520-
io::stdin().read_to_string(&mut src).unwrap();
530+
let err = if io::stdin().read_to_string(&mut src).is_err() {
531+
Some(io::Error::new(io::ErrorKind::InvalidData,
532+
"couldn't read from stdin, as it did not contain valid UTF-8"))
533+
} else {
534+
None
535+
};
521536
Some((Input::Str { name: FileName::Anon, input: src },
522-
None))
537+
None, err))
523538
} else {
524539
Some((Input::File(PathBuf::from(ifile)),
525-
Some(PathBuf::from(ifile))))
540+
Some(PathBuf::from(ifile)), None))
526541
}
527542
} else {
528543
None
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-include ../tools.mk
2+
3+
all:
4+
cp non-utf8 $(TMPDIR)/non-utf.rs
5+
cat $(TMPDIR)/non-utf.rs | $(RUSTC) - 2>&1 \
6+
| $(CGREP) "error: couldn't read from stdin, as it did not contain valid UTF-8"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)