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
This commit is contained in:
@@ -740,11 +740,16 @@ pub fn last_os_error() -> ~str {
|
|||||||
buf.len() as DWORD,
|
buf.len() as DWORD,
|
||||||
ptr::null());
|
ptr::null());
|
||||||
if res == 0 {
|
if res == 0 {
|
||||||
fail!("[{}] FormatMessage failure", errno());
|
// Sometimes FormatMessageW can fail e.g. system doesn't like langId,
|
||||||
|
let fm_err = errno();
|
||||||
|
return format!("OS Error {} (FormatMessageW() returned error {})", err, fm_err);
|
||||||
}
|
}
|
||||||
|
|
||||||
str::from_utf16(str::truncate_utf16_at_nul(buf))
|
let msg = str::from_utf16(str::truncate_utf16_at_nul(buf));
|
||||||
.expect("FormatMessageW returned invalid UTF-16")
|
match msg {
|
||||||
|
Some(msg) => format!("OS Error {}: {}", err, msg),
|
||||||
|
None => format!("OS Error {} (FormatMessageW() returned invalid UTF-16)", err),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user