]> git.lizzy.rs Git - rust.git/commitdiff
Do not recalculate string length in error_string
authorSimonas Kazlauskas <git@kazlauskas.me>
Tue, 25 Aug 2015 15:26:29 +0000 (18:26 +0300)
committerSimonas Kazlauskas <git@kazlauskas.me>
Tue, 25 Aug 2015 15:33:39 +0000 (18:33 +0300)
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.

src/libstd/sys/windows/os.rs

index 694d873d0d265f053e38631337f2f292ce628d34..bf56aaa277ec9317a43ccbd55f3024c9b35202fc 100644 (file)
@@ -75,7 +75,7 @@ fn FormatMessageW(flags: DWORD,
                                  langId,
                                  buf.as_mut_ptr(),
                                  buf.len() as DWORD,
-                                 ptr::null());
+                                 ptr::null()) as usize;
         if res == 0 {
             // Sometimes FormatMessageW can fail e.g. system doesn't like langId,
             let fm_err = errno();
@@ -83,8 +83,7 @@ fn FormatMessageW(flags: DWORD,
                            errnum, fm_err);
         }
 
-        let b = buf.iter().position(|&b| b == 0).unwrap_or(buf.len());
-        match String::from_utf16(&buf[..b]) {
+        match String::from_utf16(&buf[..res]) {
             Ok(mut msg) => {
                 // Trim trailing CRLF inserted by FormatMessageW
                 let len = msg.trim_right().len();