about summary refs log tree commit diff
path: root/src/libstd/os.rs
diff options
context:
space:
mode:
authorHuon Wilson <dbau.pp+github@gmail.com>2014-02-18 22:25:32 +1100
committerHuon Wilson <dbau.pp+github@gmail.com>2014-02-19 14:09:16 +1100
commitc9b4538babbc85b971b19bbeff16bd12a4f4db54 (patch)
tree8efb0c748d1330d1e5bfcb5c9b0c1a14ff92655b /src/libstd/os.rs
parent4f841ee1509fafdf688a3898e01560ae29ee7836 (diff)
downloadrust-c9b4538babbc85b971b19bbeff16bd12a4f4db54.tar.gz
rust-c9b4538babbc85b971b19bbeff16bd12a4f4db54.zip
str: add a function for truncating a vector of u16 at NUL.
Many of the functions interacting with Windows APIs allocate a vector of
0's and do not retrieve a length directly from the API call, and so need
to be sure to remove the unmodified junk at the end of the vector.
Diffstat (limited to 'src/libstd/os.rs')
-rw-r--r--src/libstd/os.rs10
1 files changed, 7 insertions, 3 deletions
diff --git a/src/libstd/os.rs b/src/libstd/os.rs
index 31e88905b30..74e2fceb6ca 100644
--- a/src/libstd/os.rs
+++ b/src/libstd/os.rs
@@ -88,7 +88,8 @@ pub fn getcwd() -> Path {
             fail!();
         }
     }
-    Path::new(str::from_utf16(buf).expect("GetCurrentDirectoryW returned invalid UTF-16"))
+    Path::new(str::from_utf16(str::truncate_utf16_at_nul(buf))
+              .expect("GetCurrentDirectoryW returned invalid UTF-16"))
 }
 
 #[cfg(windows)]
@@ -744,7 +745,8 @@ pub fn last_os_error() -> ~str {
                 fail!("[{}] FormatMessage failure", errno());
             }
 
-            str::from_utf16(buf).expect("FormatMessageW returned invalid UTF-16")
+            str::from_utf16(str::truncate_utf16_at_nul(buf))
+                .expect("FormatMessageW returned invalid UTF-16")
         }
     }
 
@@ -833,7 +835,9 @@ fn real_args() -> ~[~str] {
             while *ptr.offset(len as int) != 0 { len += 1; }
 
             // Push it onto the list.
-            let opt_s = vec::raw::buf_as_slice(ptr, len, str::from_utf16);
+            let opt_s = vec::raw::buf_as_slice(ptr, len, |buf| {
+                    str::from_utf16(str::truncate_utf16_at_nul(buf))
+                });
             args.push(opt_s.expect("CommandLineToArgvW returned invalid UTF-16"));
         }
     }