Skip to content

Commit c9aca02

Browse files
committed
Don't panic on std::env::vars() when env in null.
Fixes #53200
1 parent b73535f commit c9aca02

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

src/libstd/sys/unix/os.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -414,12 +414,8 @@ pub fn env() -> Env {
414414
unsafe {
415415
let _guard = ENV_LOCK.lock();
416416
let mut environ = *environ();
417-
if environ == ptr::null() {
418-
panic!("os::env() failure getting env string from OS: {}",
419-
io::Error::last_os_error());
420-
}
421417
let mut result = Vec::new();
422-
while *environ != ptr::null() {
418+
while environ != ptr::null() && *environ != ptr::null() {
423419
if let Some(key_value) = parse(CStr::from_ptr(*environ).to_bytes()) {
424420
result.push(key_value);
425421
}

src/test/run-pass/env-null-vars.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// ignore-windows
12+
// ignore-wasm32-bare no libc to test ffi with
13+
14+
// issue-53200
15+
16+
#![feature(libc)]
17+
extern crate libc;
18+
19+
use std::env;
20+
21+
// FIXME: more platforms?
22+
#[cfg(target_os = "linux")]
23+
fn main() {
24+
unsafe { libc::clearenv(); }
25+
assert_eq!(env::vars().count(), 0);
26+
}
27+
28+
#[cfg(not(target_os = "linux"))]
29+
fn main() {}

0 commit comments

Comments
 (0)