From 26caf7e1b2eb8a3fcba6811bf69338c0070b43f3 Mon Sep 17 00:00:00 2001 From: nibon7 Date: Sun, 31 Jul 2022 20:32:16 +0800 Subject: [PATCH] Return error early if seconds part of timestamp is invalid (#6193) Signed-off-by: nibon7 --- crates/nu-command/src/filesystem/touch.rs | 10 ++- crates/nu-command/tests/commands/touch.rs | 84 +++++++++++++++++++++++ 2 files changed, 93 insertions(+), 1 deletion(-) diff --git a/crates/nu-command/src/filesystem/touch.rs b/crates/nu-command/src/filesystem/touch.rs index 1806c6506..1d5f5b189 100644 --- a/crates/nu-command/src/filesystem/touch.rs +++ b/crates/nu-command/src/filesystem/touch.rs @@ -121,7 +121,15 @@ impl Command for Touch { // Checks for the seconds stamp and removes the '.' delimiter if any let (val, has_sec): (String, bool) = match stamp.split_once('.') { - Some((dtime, sec)) => (format!("{}{}", dtime, sec), true), + Some((dtime, sec)) => match sec.parse::() { + Ok(sec) if sec < 60 => (format!("{}{}", dtime, sec), true), + _ => { + return Err(ShellError::UnsupportedInput( + "input has an invalid timestamp".to_string(), + span, + )) + } + }, None => (stamp.to_string(), false), }; diff --git a/crates/nu-command/tests/commands/touch.rs b/crates/nu-command/tests/commands/touch.rs index 04c2518eb..220ebd688 100644 --- a/crates/nu-command/tests/commands/touch.rs +++ b/crates/nu-command/tests/commands/touch.rs @@ -180,6 +180,34 @@ fn errors_if_change_modified_time_of_file_with_invalid_timestamp() { ); assert!(outcome.err.contains("input has an invalid timestamp")); + + outcome = nu!( + cwd: dirs.test(), + "touch -m -t 082412.3012 file.txt" + ); + + assert!(outcome.err.contains("input has an invalid timestamp")); + + outcome = nu!( + cwd: dirs.test(), + "touch -m -t 0824.123012 file.txt" + ); + + assert!(outcome.err.contains("input has an invalid timestamp")); + + outcome = nu!( + cwd: dirs.test(), + "touch -m -t 08.24123012 file.txt" + ); + + assert!(outcome.err.contains("input has an invalid timestamp")); + + outcome = nu!( + cwd: dirs.test(), + "touch -m -t 0.824123012 file.txt" + ); + + assert!(outcome.err.contains("input has an invalid timestamp")); }) } @@ -401,6 +429,34 @@ fn errors_if_change_access_time_of_file_with_invalid_timestamp() { ); assert!(outcome.err.contains("input has an invalid timestamp")); + + outcome = nu!( + cwd: dirs.test(), + "touch -a -t 082412.3012 file.txt" + ); + + assert!(outcome.err.contains("input has an invalid timestamp")); + + outcome = nu!( + cwd: dirs.test(), + "touch -a -t 0824.123012 file.txt" + ); + + assert!(outcome.err.contains("input has an invalid timestamp")); + + outcome = nu!( + cwd: dirs.test(), + "touch -a -t 08.24123012 file.txt" + ); + + assert!(outcome.err.contains("input has an invalid timestamp")); + + outcome = nu!( + cwd: dirs.test(), + "touch -a -t 0.824123012 file.txt" + ); + + assert!(outcome.err.contains("input has an invalid timestamp")); }) } @@ -634,6 +690,34 @@ fn errors_if_change_modified_and_access_time_of_file_with_invalid_timestamp() { ); assert!(outcome.err.contains("input has an invalid timestamp")); + + outcome = nu!( + cwd: dirs.test(), + "touch -m -a -t 082412.3012 file.txt" + ); + + assert!(outcome.err.contains("input has an invalid timestamp")); + + outcome = nu!( + cwd: dirs.test(), + "touch -m -a -t 0824.123012 file.txt" + ); + + assert!(outcome.err.contains("input has an invalid timestamp")); + + outcome = nu!( + cwd: dirs.test(), + "touch -m -a -t 08.24123012 file.txt" + ); + + assert!(outcome.err.contains("input has an invalid timestamp")); + + outcome = nu!( + cwd: dirs.test(), + "touch -m -a -t 0.824123012 file.txt" + ); + + assert!(outcome.err.contains("input has an invalid timestamp")); }) }