diff --git a/src/uu/date/src/date.rs b/src/uu/date/src/date.rs index a94ffd65f..381619f06 100644 --- a/src/uu/date/src/date.rs +++ b/src/uu/date/src/date.rs @@ -262,6 +262,13 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { Ok(date) => { // GNU `date` uses `%N` for nano seconds, however crate::chrono uses `%f` let format_string = &format_string.replace("%N", "%f"); + // Refuse to pass this string to chrono as it is crashing in this crate + if format_string.contains("%#z") { + return Err(USimpleError::new( + 1, + format!("invalid format {}", format_string.replace("%f", "%N")), + )); + } // Hack to work around panic in chrono, // TODO - remove when a fix for https://github.com/chronotope/chrono/issues/623 is released let format_items = StrftimeItems::new(format_string); diff --git a/tests/by-util/test_date.rs b/tests/by-util/test_date.rs index 4dc02ef6f..c8c33aa89 100644 --- a/tests/by-util/test_date.rs +++ b/tests/by-util/test_date.rs @@ -356,6 +356,13 @@ fn test_invalid_format_string() { assert!(result.stderr_str().starts_with("date: invalid format ")); } +#[test] +fn test_unsupported_format() { + let result = new_ucmd!().arg("+%#z").fails(); + result.no_stdout(); + assert!(result.stderr_str().starts_with("date: invalid format %#z")); +} + #[test] fn test_date_string_human() { let date_formats = vec![