Skip to content

Commit 1498347

Browse files
committed
Ignore malformed environment variables on Windows too
Leading equals symbols are treated as part of the variable name, if there is no other equality symbol or none at all, the environment string is ignored.
1 parent 3e48b0e commit 1498347

File tree

1 file changed

+24
-15
lines changed
  • src/libstd/sys/windows

1 file changed

+24
-15
lines changed

src/libstd/sys/windows/os.rs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,22 +78,31 @@ impl Iterator for Env {
7878
type Item = (OsString, OsString);
7979

8080
fn next(&mut self) -> Option<(OsString, OsString)> {
81-
unsafe {
82-
if *self.cur == 0 { return None }
83-
let p = &*self.cur;
84-
let mut len = 0;
85-
while *(p as *const u16).offset(len) != 0 {
86-
len += 1;
81+
loop {
82+
unsafe {
83+
if *self.cur == 0 { return None }
84+
let p = &*self.cur as *const u16;
85+
let mut len = 0;
86+
while *p.offset(len) != 0 {
87+
len += 1;
88+
}
89+
let s = slice::from_raw_parts(p, len as usize);
90+
self.cur = self.cur.offset(len + 1);
91+
92+
// Windows allows environment variables to start with an equals
93+
// symbol (in any other position, this is the separator between
94+
// variable name and value). Since`s` has at least length 1 at
95+
// this point (because the empty string terminates the array of
96+
// environment variables), we can safely slice.
97+
let pos = match s[1..].iter().position(|&u| u == b'=' as u16).map(|p| p + 1) {
98+
Some(p) => p,
99+
None => continue,
100+
}
101+
return Some((
102+
OsStringExt::from_wide(&s[..pos]),
103+
OsStringExt::from_wide(&s[pos+1..]),
104+
))
87105
}
88-
let p = p as *const u16;
89-
let s = slice::from_raw_parts(p, len as usize);
90-
self.cur = self.cur.offset(len + 1);
91-
92-
let (k, v) = match s.iter().position(|&b| b == '=' as u16) {
93-
Some(n) => (&s[..n], &s[n+1..]),
94-
None => (s, &[][..]),
95-
};
96-
Some((OsStringExt::from_wide(k), OsStringExt::from_wide(v)))
97106
}
98107
}
99108
}

0 commit comments

Comments
 (0)