Skip to content

Commit 1fd5012

Browse files
fix: only long options are allowed to contain '='
1 parent 564dd47 commit 1fd5012

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

src/uu/env/src/env.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,18 @@ impl EnvAppData {
346346
original_args: &Vec<OsString>,
347347
) -> UResult<Vec<std::ffi::OsString>> {
348348
let mut all_args: Vec<std::ffi::OsString> = Vec::new();
349+
349350
for arg in original_args {
351+
let arg_str = arg.to_string_lossy();
352+
353+
// Only long option is allowed to contain '='
354+
if arg_str.contains('=') && arg_str.starts_with('-') && !arg_str.starts_with("--") {
355+
return Err(USimpleError::new(
356+
1,
357+
format!("cannot unset '{}': Invalid argument", &arg_str[2..]),
358+
));
359+
}
360+
350361
match arg {
351362
b if check_and_handle_string_args(b, "--split-string", &mut all_args, None)? => {
352363
self.had_string_argument = true;
@@ -751,4 +762,73 @@ mod tests {
751762
parse_args_from_str(&NCvt::convert(r#"-i A='B \' C'"#)).unwrap()
752763
);
753764
}
765+
766+
#[test]
767+
fn test_apply_unset_env_vars_invalid_equal_sign() {
768+
let opts = Options {
769+
ignore_env: false,
770+
line_ending: uucore::line_ending::LineEnding::Newline,
771+
running_directory: None,
772+
files: vec![],
773+
unsets: vec![OsStr::new("INVALID=VAR")],
774+
sets: vec![],
775+
program: vec![],
776+
argv0: None,
777+
#[cfg(unix)]
778+
ignore_signal: vec![],
779+
};
780+
781+
let result = apply_unset_env_vars(&opts);
782+
assert!(result.is_err());
783+
784+
let err = result.unwrap_err();
785+
assert_eq!(err.code(), 125);
786+
assert_eq!(
787+
err.to_string(),
788+
"cannot unset 'INVALID=VAR': Invalid argument"
789+
);
790+
}
791+
792+
#[test]
793+
fn test_process_all_string_arguments() {
794+
// Define test cases with expected outcomes
795+
let test_cases = vec![
796+
// Argument with short option and '=' (should fail)
797+
(
798+
vec![OsString::from("-u=o")],
799+
Err("cannot unset '=o': Invalid argument".to_string()),
800+
),
801+
// Argument with long option and '=' (should succeed)
802+
(vec![OsString::from("--unset=o")], Ok(())),
803+
// Argument with short option and no '=' (should succeed)
804+
(vec![OsString::from("-u o")], Ok(())),
805+
// Set env variable (should succeed)
806+
(vec![OsString::from("u=o")], Ok(())),
807+
// Empty assignment (should succeed)
808+
(vec![OsString::from("=")], Ok(())),
809+
// Argument ends with '='
810+
(vec![OsString::from("--split-string=X=Y=")], Ok(())),
811+
// No argument (should succeed)
812+
(vec![OsString::from("")], Ok(())),
813+
// TODO: implement handling of empty parameter value
814+
// (vec![OsString::from("--unset=")], Err("env: cannot unset ‘’: Invalid argument".to_string())),
815+
];
816+
817+
for (input_args, expected) in test_cases {
818+
let mut env_app_data = EnvAppData::default();
819+
820+
let result = env_app_data.process_all_string_arguments(&input_args);
821+
822+
match expected {
823+
Ok(()) => {
824+
assert!(result.is_ok());
825+
}
826+
Err(expected_err) => {
827+
assert!(result.is_err());
828+
let err = result.unwrap_err();
829+
assert_eq!(err.to_string(), expected_err);
830+
}
831+
}
832+
}
833+
}
754834
}

0 commit comments

Comments
 (0)