Skip to content

Ignore malformed environment variables on Windows too #29901

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 24 additions & 15 deletions src/libstd/sys/windows/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,31 @@ impl Iterator for Env {
type Item = (OsString, OsString);

fn next(&mut self) -> Option<(OsString, OsString)> {
unsafe {
if *self.cur == 0 { return None }
let p = &*self.cur;
let mut len = 0;
while *(p as *const u16).offset(len) != 0 {
len += 1;
loop {
unsafe {
if *self.cur == 0 { return None }
let p = &*self.cur as *const u16;
let mut len = 0;
while *p.offset(len) != 0 {
len += 1;
}
let s = slice::from_raw_parts(p, len as usize);
self.cur = self.cur.offset(len + 1);

// Windows allows environment variables to start with an equals
// symbol (in any other position, this is the separator between
// variable name and value). Since`s` has at least length 1 at
// this point (because the empty string terminates the array of
// environment variables), we can safely slice.
let pos = match s[1..].iter().position(|&u| u == b'=' as u16).map(|p| p + 1) {
Some(p) => p,
None => continue,
};
return Some((
OsStringExt::from_wide(&s[..pos]),
OsStringExt::from_wide(&s[pos+1..]),
))
}
let p = p as *const u16;
let s = slice::from_raw_parts(p, len as usize);
self.cur = self.cur.offset(len + 1);

let (k, v) = match s.iter().position(|&b| b == '=' as u16) {
Some(n) => (&s[..n], &s[n+1..]),
None => (s, &[][..]),
};
Some((OsStringExt::from_wide(k), OsStringExt::from_wide(v)))
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/test/run-pass/env-vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ use std::env::*;
fn main() {
for (k, v) in vars_os() {
let v2 = var_os(&k);
// MingW seems to set some funky environment variables like
// "=C:=C:\MinGW\msys\1.0\bin" and "!::=::\" that are returned
// from vars() but not visible from var().
assert!(v2.is_none() || v2.as_ref().map(|s| &**s) == Some(&*v),
assert!(v2.as_ref().map(|s| &**s) == Some(&*v),
"bad vars->var transition: {:?} {:?} {:?}", k, v, v2);
}
}