]> git.lizzy.rs Git - rust.git/commitdiff
Make os::setenv() and os::unsetenv() panic if an error occurs
authorBarosl Lee <vcs@barosl.com>
Tue, 11 Nov 2014 17:50:44 +0000 (02:50 +0900)
committerBarosl Lee <vcs@barosl.com>
Tue, 18 Nov 2014 20:31:45 +0000 (05:31 +0900)
These functions can fail if:

- EINVAL: The name is empty, or contains an '=' character
- ENOMEM: Insufficient memory

src/libstd/os.rs

index cf22b8014ca7ae8cbb62e43340b384248920a8f7..b898b9a2df43ecf6fa536494960d8895b86c3e7e 100644 (file)
@@ -422,7 +422,9 @@ fn _setenv(n: &str, v: &[u8]) {
             with_env_lock(|| {
                 n.with_c_str(|nbuf| {
                     v.with_c_str(|vbuf| {
-                        libc::funcs::posix01::unistd::setenv(nbuf, vbuf, 1);
+                        if libc::funcs::posix01::unistd::setenv(nbuf, vbuf, 1) != 0 {
+                            panic!(IoError::last_error());
+                        }
                     })
                 })
             })
@@ -438,7 +440,9 @@ fn _setenv(n: &str, v: &[u8]) {
 
         unsafe {
             with_env_lock(|| {
-                libc::SetEnvironmentVariableW(n.as_ptr(), v.as_ptr());
+                if libc::SetEnvironmentVariableW(n.as_ptr(), v.as_ptr()) == 0 {
+                    panic!(IoError::last_error());
+                }
             })
         }
     }
@@ -453,7 +457,9 @@ fn _unsetenv(n: &str) {
         unsafe {
             with_env_lock(|| {
                 n.with_c_str(|nbuf| {
-                    libc::funcs::posix01::unistd::unsetenv(nbuf);
+                    if libc::funcs::posix01::unistd::unsetenv(nbuf) != 0 {
+                        panic!(IoError::last_error());
+                    }
                 })
             })
         }
@@ -465,11 +471,14 @@ fn _unsetenv(n: &str) {
         n.push(0);
         unsafe {
             with_env_lock(|| {
-                libc::SetEnvironmentVariableW(n.as_ptr(), ptr::null());
+                if libc::SetEnvironmentVariableW(n.as_ptr(), ptr::null()) == 0 {
+                    panic!(IoError::last_error());
+                }
             })
         }
     }
-    _unsetenv(n);
+
+    _unsetenv(n)
 }
 
 /// Parses input according to platform conventions for the `PATH`