Auto merge of #29297 - tbu-:pr_env_ignore_malformed, r=alexcrichton

Otherwise, the iterator and the functions for getting specific
environment variables might disagree, for environments like

    FOOBAR
This commit is contained in:
bors 2015-11-17 00:31:20 +00:00
commit a7644b33d9
3 changed files with 63 additions and 12 deletions

View File

@ -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()),
))
}
}

View File

@ -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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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");
}

View File

@ -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);
}
}