about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2015-11-17 00:31:20 +0000
committerbors <bors@rust-lang.org>2015-11-17 00:31:20 +0000
commita7644b33d9fb411151c522adf258a7cfbf566ef3 (patch)
tree324054e2482288e494c8863195861a8026677d92 /src/libstd
parent9f49ea0f4bfceee4d77663c09978b720358e763d (diff)
parent87243bcce8b56aa118d677c3af22d645a2ac1ab8 (diff)
downloadrust-a7644b33d9fb411151c522adf258a7cfbf566ef3.tar.gz
rust-a7644b33d9fb411151c522adf258a7cfbf566ef3.zip
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
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/sys/unix/os.rs25
1 files changed, 17 insertions, 8 deletions
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()),
+        ))
     }
 }