Skip to content

Commit b5286af

Browse files
committed
Make os::setenv() and os::unsetenv() panic if an error occurs
These functions can fail if: - EINVAL: The name is empty, or contains an '=' character - ENOMEM: Insufficient memory
1 parent 5de56b3 commit b5286af

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/libstd/os.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,9 @@ pub fn setenv<T: BytesContainer>(n: &str, v: T) {
422422
with_env_lock(|| {
423423
n.with_c_str(|nbuf| {
424424
v.with_c_str(|vbuf| {
425-
libc::funcs::posix01::unistd::setenv(nbuf, vbuf, 1);
425+
if libc::funcs::posix01::unistd::setenv(nbuf, vbuf, 1) != 0 {
426+
panic!(IoError::last_error());
427+
}
426428
})
427429
})
428430
})
@@ -438,7 +440,9 @@ pub fn setenv<T: BytesContainer>(n: &str, v: T) {
438440

439441
unsafe {
440442
with_env_lock(|| {
441-
libc::SetEnvironmentVariableW(n.as_ptr(), v.as_ptr());
443+
if libc::SetEnvironmentVariableW(n.as_ptr(), v.as_ptr()) == 0 {
444+
panic!(IoError::last_error());
445+
}
442446
})
443447
}
444448
}
@@ -453,7 +457,9 @@ pub fn unsetenv(n: &str) {
453457
unsafe {
454458
with_env_lock(|| {
455459
n.with_c_str(|nbuf| {
456-
libc::funcs::posix01::unistd::unsetenv(nbuf);
460+
if libc::funcs::posix01::unistd::unsetenv(nbuf) != 0 {
461+
panic!(IoError::last_error());
462+
}
457463
})
458464
})
459465
}
@@ -465,11 +471,14 @@ pub fn unsetenv(n: &str) {
465471
n.push(0);
466472
unsafe {
467473
with_env_lock(|| {
468-
libc::SetEnvironmentVariableW(n.as_ptr(), ptr::null());
474+
if libc::SetEnvironmentVariableW(n.as_ptr(), ptr::null()) == 0 {
475+
panic!(IoError::last_error());
476+
}
469477
})
470478
}
471479
}
472-
_unsetenv(n);
480+
481+
_unsetenv(n)
473482
}
474483

475484
/// Parses input according to platform conventions for the `PATH`

0 commit comments

Comments
 (0)