Skip to content

Commit

Permalink
fix: potential segfault in the time crate RUSTSEC-2020-0071
Browse files Browse the repository at this point in the history
migrating `time` to latest `chrono` 0.4

fixes #142
  • Loading branch information
joseluisq committed Sep 25, 2022
1 parent 3ca743a commit 048bd77
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 6 deletions.
64 changes: 63 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -50,7 +50,7 @@ serde = { version = "1.0", default-features = false, features = ["derive"] }
serde_ignored = "0.1"
serde_repr = "0.1"
structopt = { version = "0.3", default-features = false }
time = { version = "0.1", default-features = false }
chrono = "0.4"
tokio = { version = "1", default-features = false, features = ["rt-multi-thread", "macros", "fs", "io-util", "signal"] }
tokio-rustls = { version = "0.23" }
tokio-util = { version = "0.7", default-features = false, features = ["io"] }
Expand Down
15 changes: 11 additions & 4 deletions src/directory_listing.rs
@@ -1,3 +1,4 @@
use chrono::{DateTime, Local, NaiveDateTime, Utc};
use futures_util::future::Either;
use futures_util::{future, FutureExt};
use headers::{ContentLength, ContentType, HeaderMapExt};
Expand Down Expand Up @@ -145,7 +146,7 @@ async fn read_dir_entries(
}

let modified = match parse_last_modified(meta.modified()?) {
Ok(tm) => tm.to_local().strftime("%F %T")?.to_string(),
Ok(local_dt) => local_dt.format("%F %T").to_string(),
Err(err) => {
tracing::error!("error determining file last modified: {:?}", err);
String::from("-")
Expand Down Expand Up @@ -310,7 +311,9 @@ fn sort_files(
(name, last_modified, size)
}

fn parse_last_modified(modified: SystemTime) -> Result<time::Tm, Box<dyn std::error::Error>> {
fn parse_last_modified(
modified: SystemTime,
) -> Result<DateTime<Local>, Box<dyn std::error::Error>> {
let since_epoch = modified.duration_since(UNIX_EPOCH)?;
// HTTP times don't have nanosecond precision, so we truncate
// the modification time.
Expand All @@ -321,6 +324,10 @@ fn parse_last_modified(modified: SystemTime) -> Result<time::Tm, Box<dyn std::er
// the modification time of a file with greater than second
// precision appears to be something that only is possible to
// do on Linux.
let ts = time::Timespec::new(since_epoch.as_secs() as i64, 0);
Ok(time::at_utc(ts))
let utc_dt = NaiveDateTime::from_timestamp(
since_epoch.as_secs() as i64,
since_epoch.subsec_nanos() as u32,
);
let local_dt = DateTime::<Utc>::from_utc(utc_dt, Utc).with_timezone(&Local);
Ok(local_dt)
}

0 comments on commit 048bd77

Please sign in to comment.