Skip to content

Ignore malformed environment strings like glibc does #29297

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 1 commit into from
Nov 17, 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
25 changes: 17 additions & 8 deletions src/libstd/sys/unix/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
))
}
}

Expand Down
45 changes: 45 additions & 0 deletions src/test/run-pass/env-funky-keys.rs
Original file line number Diff line number Diff line change
@@ -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");
}
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);
}
}