diff --git a/src/libstd/sys/unix/os.rs b/src/libstd/sys/unix/os.rs index 3c53db53f85..5bc5567df2f 100644 --- a/src/libstd/sys/unix/os.rs +++ b/src/libstd/sys/unix/os.rs @@ -386,24 +386,33 @@ pub fn env() -> Env { let _g = ENV_LOCK.lock(); return unsafe { let mut environ = *environ(); - if environ as usize == 0 { + if environ == ptr::null() { panic!("os::env() failure getting env string from OS: {}", io::Error::last_os_error()); } let mut result = Vec::new(); while *environ != ptr::null() { - result.push(parse(CStr::from_ptr(*environ).to_bytes())); + if let Some(key_value) = parse(CStr::from_ptr(*environ).to_bytes()) { + result.push(key_value); + } environ = environ.offset(1); } Env { iter: result.into_iter(), _dont_send_or_sync_me: ptr::null_mut() } }; - fn parse(input: &[u8]) -> (OsString, OsString) { - let mut it = input.splitn(2, |b| *b == b'='); - let key = it.next().unwrap().to_vec(); - let default: &[u8] = &[]; - let val = it.next().unwrap_or(default).to_vec(); - (OsStringExt::from_vec(key), OsStringExt::from_vec(val)) + fn parse(input: &[u8]) -> Option<(OsString, OsString)> { + // Strategy (copied from glibc): Variable name and value are separated + // by an ASCII equals sign '='. Since a variable name must not be + // empty, allow variable names starting with an equals sign. Skip all + // malformed lines. + if input.is_empty() { + return None; + } + let pos = input[1..].iter().position(|&b| b == b'=').map(|p| p + 1); + pos.map(|p| ( + OsStringExt::from_vec(input[..p].to_vec()), + OsStringExt::from_vec(input[p+1..].to_vec()), + )) } } diff --git a/src/test/run-pass/env-funky-keys.rs b/src/test/run-pass/env-funky-keys.rs new file mode 100644 index 00000000000..3ee20980747 --- /dev/null +++ b/src/test/run-pass/env-funky-keys.rs @@ -0,0 +1,45 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Ignore this test on Android, because it segfaults there. + +// ignore-android +// ignore-windows +// no-prefer-dynamic + +#![feature(convert)] +#![feature(libc)] + +extern crate libc; + +use libc::c_char; +use libc::execve; +use std::env; +use std::ffi::OsStr; +use std::ptr; + +fn main() { + if env::args_os().next().is_none() { + for (key, value) in env::vars_os() { + panic!("found env value {:?} {:?}", key, value); + } + return; + } + + let current_exe = env::current_exe().unwrap().into_os_string().to_cstring().unwrap(); + let new_env_var = OsStr::new("FOOBAR").to_cstring().unwrap(); + let filename: *const c_char = current_exe.as_ptr(); + let argv: &[*const c_char] = &[ptr::null()]; + let envp: &[*const c_char] = &[new_env_var.as_ptr(), ptr::null()]; + unsafe { + execve(filename, &argv[0], &envp[0]); + } + panic!("execve failed"); +} diff --git a/src/test/run-pass/env-vars.rs b/src/test/run-pass/env-vars.rs index d86f63c9cb9..933d9a728db 100644 --- a/src/test/run-pass/env-vars.rs +++ b/src/test/run-pass/env-vars.rs @@ -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); } }