Skip to content

Commit 9347096

Browse files
committed
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
1 parent ace204a commit 9347096

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/libstd/os.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,15 @@ pub mod win32 {
113113
let mut done = false;
114114
while !done {
115115
let mut buf = vec::from_elem(n as uint, 0u16);
116-
let k = f(buf.as_mut_ptr(), TMPBUF_SZ as DWORD);
116+
let k = f(buf.as_mut_ptr(), n);
117117
if k == (0 as DWORD) {
118118
done = true;
119119
} else if k == n &&
120120
libc::GetLastError() ==
121121
libc::ERROR_INSUFFICIENT_BUFFER as DWORD {
122122
n *= (2 as DWORD);
123+
} else if k >= n {
124+
n = k;
123125
} else {
124126
done = true;
125127
}
@@ -1494,6 +1496,16 @@ mod tests {
14941496
}
14951497
}
14961498
1499+
#[test]
1500+
fn test_env_set_get_huge() {
1501+
let n = make_rand_name();
1502+
let s = "x".repeat(10000);
1503+
setenv(n, s);
1504+
assert_eq!(getenv(n), Some(s));
1505+
unsetenv(n);
1506+
assert_eq!(getenv(n), None);
1507+
}
1508+
14971509
#[test]
14981510
fn test_env_setenv() {
14991511
let n = make_rand_name();

0 commit comments

Comments
 (0)