From 9d67d9d42b6f9f4447740e3b511280d4f4c54bb1 Mon Sep 17 00:00:00 2001 From: Jose Quintana Date: Sat, 12 Nov 2022 23:18:29 +0100 Subject: [PATCH] fix: unhandled panic when get "last modified" on dir listing module --- src/directory_listing.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/directory_listing.rs b/src/directory_listing.rs index aed0d89..788e9a0 100644 --- a/src/directory_listing.rs +++ b/src/directory_listing.rs @@ -438,6 +438,7 @@ fn sort_file_entries(files: &mut [FileEntry], order_code: u8) -> SortingAttr<'_> } } +/// Return the last modified `DateTime` in local timescale. fn parse_last_modified(modified: SystemTime) -> Result> { let since_epoch = modified.duration_since(UNIX_EPOCH)?; // HTTP times don't have nanosecond precision, so we truncate @@ -449,10 +450,16 @@ fn parse_last_modified(modified: SystemTime) -> Result> { // the modification time of a file with greater than second // precision appears to be something that only is possible to // do on Linux. - let utc_dt = NaiveDateTime::from_timestamp( + + let utc_dt = NaiveDateTime::from_timestamp_opt( since_epoch.as_secs() as i64, since_epoch.subsec_nanos() as u32, ); - let local_dt = DateTime::::from_utc(utc_dt, Utc).with_timezone(&Local); - Ok(local_dt) + + match utc_dt { + Some(utc_dt) => Ok(DateTime::::from_utc(utc_dt, Utc).with_timezone(&Local)), + None => Err(anyhow!( + "out-of-range number of seconds and/or invalid nanosecond" + )), + } } -- libgit2 1.7.2