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 {
|
||||
// ignore all INI section lines (treat them as comments)
|
||||
for (key, value) in prop {
|
||||
unsafe {
|
||||
env::set_var(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -559,10 +561,12 @@ fn apply_removal_of_all_env_vars(opts: &Options<'_>) {
|
|||
// remove all env vars if told to ignore presets
|
||||
if opts.ignore_env {
|
||||
for (ref name, _) in env::vars_os() {
|
||||
unsafe {
|
||||
env::remove_var(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn make_options(matches: &clap::ArgMatches) -> UResult<Options<'_>> {
|
||||
let ignore_env = matches.get_flag("ignore-environment");
|
||||
|
@ -634,9 +638,10 @@ fn apply_unset_env_vars(opts: &Options<'_>) -> Result<(), Box<dyn UError>> {
|
|||
format!("cannot unset {}: Invalid argument", name.quote()),
|
||||
));
|
||||
}
|
||||
|
||||
unsafe {
|
||||
env::remove_var(name);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -692,9 +697,11 @@ fn apply_specified_env_vars(opts: &Options<'_>) {
|
|||
show_warning!("no name specified for value {}", val.quote());
|
||||
continue;
|
||||
}
|
||||
unsafe {
|
||||
env::set_var(name, val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn apply_ignore_signal(opts: &Options<'_>) -> UResult<()> {
|
||||
|
|
|
@ -1111,8 +1111,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
|||
.get_one::<String>(options::PARALLEL)
|
||||
.map(String::from)
|
||||
.unwrap_or_else(|| "0".to_string());
|
||||
unsafe {
|
||||
env::set_var("RAYON_NUM_THREADS", &settings.threads);
|
||||
}
|
||||
}
|
||||
|
||||
settings.buffer_size =
|
||||
matches
|
||||
|
|
|
@ -49,7 +49,9 @@ impl WithEnvVarSet {
|
|||
/// Save previous value assigned to key, set key=value
|
||||
fn new(key: &str, value: &str) -> Self {
|
||||
let previous_env_value = env::var(key);
|
||||
unsafe {
|
||||
env::set_var(key, value);
|
||||
}
|
||||
Self {
|
||||
_previous_var_key: String::from(key),
|
||||
_previous_var_value: previous_env_value,
|
||||
|
@ -61,12 +63,16 @@ impl Drop for WithEnvVarSet {
|
|||
/// Restore previous value now that this is being dropped by context
|
||||
fn drop(&mut self) {
|
||||
if let Ok(ref prev_value) = self._previous_var_value {
|
||||
unsafe {
|
||||
env::set_var(&self._previous_var_key, prev_value);
|
||||
}
|
||||
} else {
|
||||
unsafe {
|
||||
env::remove_var(&self._previous_var_key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
impl FilterWriter {
|
||||
/// 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
|
||||
/// ToDO ... (safety note)
|
||||
#[no_mangle]
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn __stdbuf() {
|
||||
if let Ok(val) = env::var("_STDBUF_E") {
|
||||
set_buffer(__stdbuf_get_stderr(), &val);
|
||||
|
|
|
@ -16,27 +16,45 @@ fn test_invalid_arg() {
|
|||
fn test_shell_syntax() {
|
||||
use std::env;
|
||||
let last = env::var("SHELL");
|
||||
unsafe {
|
||||
env::set_var("SHELL", "/path/csh");
|
||||
}
|
||||
assert_eq!(OutputFmt::CShell, guess_syntax());
|
||||
unsafe {
|
||||
env::set_var("SHELL", "csh");
|
||||
}
|
||||
assert_eq!(OutputFmt::CShell, guess_syntax());
|
||||
unsafe {
|
||||
env::set_var("SHELL", "/path/bash");
|
||||
}
|
||||
assert_eq!(OutputFmt::Shell, guess_syntax());
|
||||
unsafe {
|
||||
env::set_var("SHELL", "bash");
|
||||
}
|
||||
assert_eq!(OutputFmt::Shell, guess_syntax());
|
||||
unsafe {
|
||||
env::set_var("SHELL", "/asd/bar");
|
||||
}
|
||||
assert_eq!(OutputFmt::Shell, guess_syntax());
|
||||
unsafe {
|
||||
env::set_var("SHELL", "foo");
|
||||
}
|
||||
assert_eq!(OutputFmt::Shell, guess_syntax());
|
||||
unsafe {
|
||||
env::set_var("SHELL", "");
|
||||
}
|
||||
assert_eq!(OutputFmt::Unknown, guess_syntax());
|
||||
unsafe {
|
||||
env::remove_var("SHELL");
|
||||
}
|
||||
assert_eq!(OutputFmt::Unknown, guess_syntax());
|
||||
|
||||
if let Ok(s) = last {
|
||||
unsafe {
|
||||
env::set_var("SHELL", s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_str_utils() {
|
||||
|
|
|
@ -324,7 +324,9 @@ fn test_filter_with_env_var_set() {
|
|||
RandomFile::new(&at, name).add_lines(n_lines);
|
||||
|
||||
let env_var_value = "some-value";
|
||||
unsafe {
|
||||
env::set_var("FILE", env_var_value);
|
||||
}
|
||||
ucmd.args(&[format!("--filter={}", "cat > $FILE").as_str(), name])
|
||||
.succeeds();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue