Skip to content

Commit cffe9e0

Browse files
committed
std::os: Handle FormatMessage failure
`FormatMessageW()` is called by `std::os::last_os_error()` to convert errno into string, but the function may fail on non-english locale. I don't know why it fails, but anyway it's better to return errno than to `fail!()` in the case. Fixes #13075 Fixes #13073
1 parent 30165e0 commit cffe9e0

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/libstd/os.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -740,11 +740,16 @@ pub fn last_os_error() -> ~str {
740740
buf.len() as DWORD,
741741
ptr::null());
742742
if res == 0 {
743-
fail!("[{}] FormatMessage failure", errno());
743+
// Sometimes FormatMessageW can fail e.g. system doesn't like langId,
744+
let fm_err = errno();
745+
return format!("OS Error {} (FormatMessageW() returned error {})", err, fm_err);
744746
}
745747

746-
str::from_utf16(str::truncate_utf16_at_nul(buf))
747-
.expect("FormatMessageW returned invalid UTF-16")
748+
let msg = str::from_utf16(str::truncate_utf16_at_nul(buf));
749+
match msg {
750+
Some(msg) => format!("OS Error {}: {}", err, msg),
751+
None => format!("OS Error {} (FormatMessageW() returned invalid UTF-16)", err),
752+
}
748753
}
749754
}
750755

0 commit comments

Comments
 (0)