summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2014-02-19 08:11:00 -0800
committerAlex Crichton <alex@alexcrichton.com>2014-02-19 08:11:00 -0800
commit9347096d54a983d47996c5f8020dbec04fea81fa (patch)
tree894f92aeeb7a183dfbb67d76ce7bc50a4b4eb85b /src/libstd
parentace204a74548947914d08a7feca694941106175d (diff)
downloadrust-9347096d54a983d47996c5f8020dbec04fea81fa.tar.gz
rust-9347096d54a983d47996c5f8020dbec04fea81fa.zip
Fix getting/setting huge env vars on windows
On windows, the GetEnvironmentVariable function will return the necessary buffer
size if the buffer provided was too small. This case previously fell through the
checks inside of fill_utf16_buf_and_decode, tripping an assertion in the `slice`
method.

This adds an extra case for when the return value is >= the buffer size, in
which case we assume the return value as the new buffer size and try again.

Closes #12376
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/os.rs14
1 files changed, 13 insertions, 1 deletions
diff --git a/src/libstd/os.rs b/src/libstd/os.rs
index 74e2fceb6ca..458e31fd86f 100644
--- a/src/libstd/os.rs
+++ b/src/libstd/os.rs
@@ -113,13 +113,15 @@ pub mod win32 {
             let mut done = false;
             while !done {
                 let mut buf = vec::from_elem(n as uint, 0u16);
-                let k = f(buf.as_mut_ptr(), TMPBUF_SZ as DWORD);
+                let k = f(buf.as_mut_ptr(), n);
                 if k == (0 as DWORD) {
                     done = true;
                 } else if k == n &&
                           libc::GetLastError() ==
                           libc::ERROR_INSUFFICIENT_BUFFER as DWORD {
                     n *= (2 as DWORD);
+                } else if k >= n {
+                    n = k;
                 } else {
                     done = true;
                 }
@@ -1495,6 +1497,16 @@ mod tests {
     }
 
     #[test]
+    fn test_env_set_get_huge() {
+        let n = make_rand_name();
+        let s = "x".repeat(10000);
+        setenv(n, s);
+        assert_eq!(getenv(n), Some(s));
+        unsetenv(n);
+        assert_eq!(getenv(n), None);
+    }
+
+    #[test]
     fn test_env_setenv() {
         let n = make_rand_name();