From 9347096d54a983d47996c5f8020dbec04fea81fa Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 19 Feb 2014 08:11:00 -0800 Subject: [PATCH] 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 --- src/libstd/os.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/libstd/os.rs b/src/libstd/os.rs index 74e2fceb6cae6..458e31fd86f5f 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; } @@ -1494,6 +1496,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();