mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
Fix unsafe attribute used without unsafe
This commit is contained in:
parent
e0fbced116
commit
2739c19330
6 changed files with 55 additions and 20 deletions
9
src/uu/env/src/env.rs
vendored
9
src/uu/env/src/env.rs
vendored
|
@ -163,10 +163,12 @@ fn load_config_file(opts: &mut Options) -> UResult<()> {
|
||||||
for (_, prop) in &conf {
|
for (_, prop) in &conf {
|
||||||
// ignore all INI section lines (treat them as comments)
|
// ignore all INI section lines (treat them as comments)
|
||||||
for (key, value) in prop {
|
for (key, value) in prop {
|
||||||
|
unsafe {
|
||||||
env::set_var(key, value);
|
env::set_var(key, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -559,9 +561,11 @@ fn apply_removal_of_all_env_vars(opts: &Options<'_>) {
|
||||||
// remove all env vars if told to ignore presets
|
// remove all env vars if told to ignore presets
|
||||||
if opts.ignore_env {
|
if opts.ignore_env {
|
||||||
for (ref name, _) in env::vars_os() {
|
for (ref name, _) in env::vars_os() {
|
||||||
|
unsafe {
|
||||||
env::remove_var(name);
|
env::remove_var(name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn make_options(matches: &clap::ArgMatches) -> UResult<Options<'_>> {
|
fn make_options(matches: &clap::ArgMatches) -> UResult<Options<'_>> {
|
||||||
|
@ -634,9 +638,10 @@ fn apply_unset_env_vars(opts: &Options<'_>) -> Result<(), Box<dyn UError>> {
|
||||||
format!("cannot unset {}: Invalid argument", name.quote()),
|
format!("cannot unset {}: Invalid argument", name.quote()),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
unsafe {
|
||||||
env::remove_var(name);
|
env::remove_var(name);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -692,8 +697,10 @@ fn apply_specified_env_vars(opts: &Options<'_>) {
|
||||||
show_warning!("no name specified for value {}", val.quote());
|
show_warning!("no name specified for value {}", val.quote());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
unsafe {
|
||||||
env::set_var(name, val);
|
env::set_var(name, val);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
|
|
|
@ -1111,8 +1111,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
.get_one::<String>(options::PARALLEL)
|
.get_one::<String>(options::PARALLEL)
|
||||||
.map(String::from)
|
.map(String::from)
|
||||||
.unwrap_or_else(|| "0".to_string());
|
.unwrap_or_else(|| "0".to_string());
|
||||||
|
unsafe {
|
||||||
env::set_var("RAYON_NUM_THREADS", &settings.threads);
|
env::set_var("RAYON_NUM_THREADS", &settings.threads);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
settings.buffer_size =
|
settings.buffer_size =
|
||||||
matches
|
matches
|
||||||
|
|
|
@ -49,7 +49,9 @@ impl WithEnvVarSet {
|
||||||
/// Save previous value assigned to key, set key=value
|
/// Save previous value assigned to key, set key=value
|
||||||
fn new(key: &str, value: &str) -> Self {
|
fn new(key: &str, value: &str) -> Self {
|
||||||
let previous_env_value = env::var(key);
|
let previous_env_value = env::var(key);
|
||||||
|
unsafe {
|
||||||
env::set_var(key, value);
|
env::set_var(key, value);
|
||||||
|
}
|
||||||
Self {
|
Self {
|
||||||
_previous_var_key: String::from(key),
|
_previous_var_key: String::from(key),
|
||||||
_previous_var_value: previous_env_value,
|
_previous_var_value: previous_env_value,
|
||||||
|
@ -61,11 +63,15 @@ impl Drop for WithEnvVarSet {
|
||||||
/// Restore previous value now that this is being dropped by context
|
/// Restore previous value now that this is being dropped by context
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
if let Ok(ref prev_value) = self._previous_var_value {
|
if let Ok(ref prev_value) = self._previous_var_value {
|
||||||
|
unsafe {
|
||||||
env::set_var(&self._previous_var_key, prev_value);
|
env::set_var(&self._previous_var_key, prev_value);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
|
unsafe {
|
||||||
env::remove_var(&self._previous_var_key);
|
env::remove_var(&self._previous_var_key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
impl FilterWriter {
|
impl FilterWriter {
|
||||||
/// Create a new filter running a command with $FILE pointing at the output name
|
/// Create a new filter running a command with $FILE pointing at the output name
|
||||||
|
|
|
@ -64,7 +64,7 @@ fn set_buffer(stream: *mut FILE, value: &str) {
|
||||||
|
|
||||||
/// # Safety
|
/// # Safety
|
||||||
/// ToDO ... (safety note)
|
/// ToDO ... (safety note)
|
||||||
#[no_mangle]
|
#[unsafe(no_mangle)]
|
||||||
pub unsafe extern "C" fn __stdbuf() {
|
pub unsafe extern "C" fn __stdbuf() {
|
||||||
if let Ok(val) = env::var("_STDBUF_E") {
|
if let Ok(val) = env::var("_STDBUF_E") {
|
||||||
set_buffer(__stdbuf_get_stderr(), &val);
|
set_buffer(__stdbuf_get_stderr(), &val);
|
||||||
|
|
|
@ -16,26 +16,44 @@ fn test_invalid_arg() {
|
||||||
fn test_shell_syntax() {
|
fn test_shell_syntax() {
|
||||||
use std::env;
|
use std::env;
|
||||||
let last = env::var("SHELL");
|
let last = env::var("SHELL");
|
||||||
|
unsafe {
|
||||||
env::set_var("SHELL", "/path/csh");
|
env::set_var("SHELL", "/path/csh");
|
||||||
|
}
|
||||||
assert_eq!(OutputFmt::CShell, guess_syntax());
|
assert_eq!(OutputFmt::CShell, guess_syntax());
|
||||||
|
unsafe {
|
||||||
env::set_var("SHELL", "csh");
|
env::set_var("SHELL", "csh");
|
||||||
|
}
|
||||||
assert_eq!(OutputFmt::CShell, guess_syntax());
|
assert_eq!(OutputFmt::CShell, guess_syntax());
|
||||||
|
unsafe {
|
||||||
env::set_var("SHELL", "/path/bash");
|
env::set_var("SHELL", "/path/bash");
|
||||||
|
}
|
||||||
assert_eq!(OutputFmt::Shell, guess_syntax());
|
assert_eq!(OutputFmt::Shell, guess_syntax());
|
||||||
|
unsafe {
|
||||||
env::set_var("SHELL", "bash");
|
env::set_var("SHELL", "bash");
|
||||||
|
}
|
||||||
assert_eq!(OutputFmt::Shell, guess_syntax());
|
assert_eq!(OutputFmt::Shell, guess_syntax());
|
||||||
|
unsafe {
|
||||||
env::set_var("SHELL", "/asd/bar");
|
env::set_var("SHELL", "/asd/bar");
|
||||||
|
}
|
||||||
assert_eq!(OutputFmt::Shell, guess_syntax());
|
assert_eq!(OutputFmt::Shell, guess_syntax());
|
||||||
|
unsafe {
|
||||||
env::set_var("SHELL", "foo");
|
env::set_var("SHELL", "foo");
|
||||||
|
}
|
||||||
assert_eq!(OutputFmt::Shell, guess_syntax());
|
assert_eq!(OutputFmt::Shell, guess_syntax());
|
||||||
|
unsafe {
|
||||||
env::set_var("SHELL", "");
|
env::set_var("SHELL", "");
|
||||||
|
}
|
||||||
assert_eq!(OutputFmt::Unknown, guess_syntax());
|
assert_eq!(OutputFmt::Unknown, guess_syntax());
|
||||||
|
unsafe {
|
||||||
env::remove_var("SHELL");
|
env::remove_var("SHELL");
|
||||||
|
}
|
||||||
assert_eq!(OutputFmt::Unknown, guess_syntax());
|
assert_eq!(OutputFmt::Unknown, guess_syntax());
|
||||||
|
|
||||||
if let Ok(s) = last {
|
if let Ok(s) = last {
|
||||||
|
unsafe {
|
||||||
env::set_var("SHELL", s);
|
env::set_var("SHELL", s);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -324,7 +324,9 @@ fn test_filter_with_env_var_set() {
|
||||||
RandomFile::new(&at, name).add_lines(n_lines);
|
RandomFile::new(&at, name).add_lines(n_lines);
|
||||||
|
|
||||||
let env_var_value = "some-value";
|
let env_var_value = "some-value";
|
||||||
|
unsafe {
|
||||||
env::set_var("FILE", env_var_value);
|
env::set_var("FILE", env_var_value);
|
||||||
|
}
|
||||||
ucmd.args(&[format!("--filter={}", "cat > $FILE").as_str(), name])
|
ucmd.args(&[format!("--filter={}", "cat > $FILE").as_str(), name])
|
||||||
.succeeds();
|
.succeeds();
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue