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