Skip to content

Commit f412c48

Browse files
committed
rustc_dejagnu: Fix handling of invalid UTF8 in test content
1 parent 3962f0b commit f412c48

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

src/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub fn copy_rs_files(
6969
.collect()
7070
}
7171

72-
fn pass_dispatch(pass: &PassKind) -> Vec<Box<dyn Pass>> {
72+
fn pass_dispatch(pass: PassKind) -> Vec<Box<dyn Pass>> {
7373
match pass {
7474
PassKind::GccrsParsing => vec![Box::new(passes::GccrsParsing)],
7575
PassKind::RustcDejagnu => vec![Box::new(passes::RustcDejagnu)],
@@ -142,7 +142,7 @@ fn main() -> anyhow::Result<()> {
142142

143143
let ftf_header = String::from("tests:\n");
144144

145-
let pass_kind = &args.pass;
145+
let pass_kind = args.pass;
146146
let passes = pass_dispatch(pass_kind);
147147
log!("running pass `{}`...", pass_kind);
148148

src/passes/rustc_dejagnu.rs

+22-12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::error::Error;
55
use crate::passes::{Pass, TestCase};
66

77
use std::fs;
8+
use std::io::Read;
89
use std::path::{Path, PathBuf};
910

1011
pub struct RustcDejagnu;
@@ -18,18 +19,27 @@ impl Pass for RustcDejagnu {
1819
}
1920

2021
fn adapt(&self, args: &Args, file: &Path) -> Result<TestCase, Error> {
21-
let test_content = fs::read_to_string(file)?;
22-
23-
let exit_code = u8::from(test_content.contains("dg-error"));
24-
25-
// FIXME: This should be removed once we have a proper main shim in gccrs
26-
// This is to make sure that we can't ever get a "success" because a test
27-
// contains a dg-error directive and a `fn main() -> i32` so rustc produces
28-
// the correct exit code
29-
let exit_code = if test_content.contains("fn main() -> i32") {
30-
255
31-
} else {
32-
exit_code
22+
// we have invalid UTF-8 testcases, so we cannot just use `fs::read_to_string`
23+
let mut test_file = fs::File::open(file)?;
24+
let mut bytes = Vec::new();
25+
test_file.read_to_end(&mut bytes)?;
26+
27+
let exit_code = match String::from_utf8(bytes) {
28+
Ok(content) => {
29+
let mut exit_code = u8::from(content.contains("dg-error"));
30+
31+
// FIXME: This should be removed once we have a proper main shim in gccrs
32+
// This is to make sure that we can't ever get a "success" because a test
33+
// contains a dg-error directive and a `fn main() -> i32` so rustc produces
34+
// the correct exit code
35+
if content.contains("fn main()") {
36+
exit_code = 255;
37+
}
38+
39+
exit_code
40+
}
41+
// invalid UTF-8 in the file
42+
Err(_) => 1, // Is that stable?
3343
};
3444

3545
let test_case = TestCase::from_compiler(Compiler::new(Kind::RustcBootstrap, args))

0 commit comments

Comments
 (0)