Skip to content

Commit c4c533a

Browse files
committed
Do not recalculate string length in error_string
According to https://msdn.microsoft.com/en-us/library/windows/desktop/ms679351(v=vs.85).aspx: > If the function succeeds, the return value is the number of TCHARs stored in the output buffer, > excluding the terminating null character.
1 parent 2b45a0d commit c4c533a

File tree

1 file changed

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

1 file changed

+2
-3
lines changed

src/libstd/sys/windows/os.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,15 @@ pub fn error_string(errnum: i32) -> String {
7575
langId,
7676
buf.as_mut_ptr(),
7777
buf.len() as DWORD,
78-
ptr::null());
78+
ptr::null()) as usize;
7979
if res == 0 {
8080
// Sometimes FormatMessageW can fail e.g. system doesn't like langId,
8181
let fm_err = errno();
8282
return format!("OS Error {} (FormatMessageW() returned error {})",
8383
errnum, fm_err);
8484
}
8585

86-
let b = buf.iter().position(|&b| b == 0).unwrap_or(buf.len());
87-
match String::from_utf16(&buf[..b]) {
86+
match String::from_utf16(&buf[..res]) {
8887
Ok(mut msg) => {
8988
// Trim trailing CRLF inserted by FormatMessageW
9089
let len = msg.trim_right().len();

0 commit comments

Comments
 (0)