1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

date: Catch format string that is not supported by chrono (#4244)

* Catch format string that is not supported by chrono

Co-authored-by: Sylvestre Ledru <sylvestre@debian.org>
This commit is contained in:
jaggededgedjustice 2023-05-27 14:41:12 +01:00 committed by GitHub
parent 76656aa2c6
commit 81a854a13e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 0 deletions

View file

@ -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);

View file

@ -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![