Skip to content

Commit 9d7c5e5

Browse files
committed
Auto merge of #29134 - wthrowe:linker-output-ICE, r=pnkfelix
The escaped form isn't pretty, but this should be a very rare error. Having a general binary-escaping string creation function might be a good idea, though. Closes #29122
2 parents 4af89fe + d12522c commit 9d7c5e5

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/librustc_trans/back/link.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ use util::sha2::{Digest, Sha256};
3232
use util::fs::fix_windows_verbatim_for_gcc;
3333
use rustc_back::tempdir::TempDir;
3434

35+
use std::ascii;
36+
use std::char;
3537
use std::env;
3638
use std::ffi::OsString;
3739
use std::fs::{self, PathExt};
@@ -883,18 +885,28 @@ fn link_natively(sess: &Session, dylib: bool,
883885
let prog = time(sess.time_passes(), "running linker", || cmd.output());
884886
match prog {
885887
Ok(prog) => {
888+
fn escape_string(s: &[u8]) -> String {
889+
str::from_utf8(s).map(|s| s.to_owned())
890+
.unwrap_or_else(|_| {
891+
let mut x = "Non-UTF-8 output: ".to_string();
892+
x.extend(s.iter()
893+
.flat_map(|&b| ascii::escape_default(b))
894+
.map(|b| char::from_u32(b as u32).unwrap()));
895+
x
896+
})
897+
}
886898
if !prog.status.success() {
887899
sess.err(&format!("linking with `{}` failed: {}",
888900
pname,
889901
prog.status));
890902
sess.note(&format!("{:?}", &cmd));
891903
let mut output = prog.stderr.clone();
892904
output.push_all(&prog.stdout);
893-
sess.note(str::from_utf8(&output[..]).unwrap());
905+
sess.note(&*escape_string(&output[..]));
894906
sess.abort_if_errors();
895907
}
896-
info!("linker stderr:\n{}", String::from_utf8(prog.stderr).unwrap());
897-
info!("linker stdout:\n{}", String::from_utf8(prog.stdout).unwrap());
908+
info!("linker stderr:\n{}", escape_string(&prog.stderr[..]));
909+
info!("linker stdout:\n{}", escape_string(&prog.stdout[..]));
898910
},
899911
Err(e) => {
900912
sess.fatal(&format!("could not exec the linker `{}`: {}", pname, e));

0 commit comments

Comments
 (0)