Skip to content

Commit be2d4fb

Browse files
committed
Rollup merge of #27577 - diaphore:trailing-newline-formatmessagew, r=alexcrichton
`FormatMessageW` always inserts trailing `\r\n` to system messages which is a minor annoyance when they're fed to `Debug` but can break formatting with `Display`. ```rust fn main() { use std::env; if let Err(err) = env::set_current_dir("???") { println!("{:#?}\n{}", err, err); } } ``` ```_ Error { repr: Os { code: 2, message: "The system cannot find the file specified.\r\n" } } The system cannot find the file specified. (os error 2) ```
2 parents 2cad6fe + 2daa1b7 commit be2d4fb

File tree

1 file changed

+7
-3
lines changed
  • src/libstd/sys/windows

1 file changed

+7
-3
lines changed

src/libstd/sys/windows/os.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,13 @@ pub fn error_string(errnum: i32) -> String {
8484
}
8585

8686
let b = buf.iter().position(|&b| b == 0).unwrap_or(buf.len());
87-
let msg = String::from_utf16(&buf[..b]);
88-
match msg {
89-
Ok(msg) => msg,
87+
match String::from_utf16(&buf[..b]) {
88+
Ok(mut msg) => {
89+
// Trim trailing CRLF inserted by FormatMessageW
90+
let len = msg.trim_right().len();
91+
msg.truncate(len);
92+
msg
93+
},
9094
Err(..) => format!("OS Error {} (FormatMessageW() returned \
9195
invalid UTF-16)", errnum),
9296
}

0 commit comments

Comments
 (0)