Skip to content

Commit

Permalink
Merge #120
Browse files Browse the repository at this point in the history
120: Backport #119 r=marc-casperlabs a=marc-casperlabs

Backports the security fix from #119 to 1.4.13.

Co-authored-by: Félix <[email protected]>
Co-authored-by: Marc Brinkmann <[email protected]>
  • Loading branch information
3 people authored Feb 9, 2023
2 parents d7bc3c8 + 2db3a68 commit c8db6a7
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion node/src/types/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ static TIMESTAMP_EXAMPLE: Lazy<Timestamp> = Lazy::new(|| {
pub struct Timestamp(u64);

impl Timestamp {
/// The maximum value a timestamp can have.
pub const MAX: Timestamp = Timestamp(u64::MAX);

/// Returns the timestamp of the current moment.
pub fn now() -> Self {
let millis = SystemTime::UNIX_EPOCH.elapsed().unwrap().as_millis() as u64;
Expand Down Expand Up @@ -84,7 +87,8 @@ impl Timestamp {
impl Display for Timestamp {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match SystemTime::UNIX_EPOCH.checked_add(Duration::from_millis(self.0)) {
Some(system_time) => write!(f, "{}", humantime::format_rfc3339_millis(system_time)),
Some(system_time) => write!(f, "{}", humantime::format_rfc3339_millis(system_time))
.or_else(|e| write!(f, "Invalid timestamp: {}: {}", e, self.0)),
None => write!(f, "invalid Timestamp: {} ms after the Unix epoch", self.0),
}
}
Expand Down Expand Up @@ -355,4 +359,9 @@ mod tests {

bytesrepr::test_serialization_roundtrip(&timediff);
}

#[test]
fn does_not_crash_for_big_timestamp_value() {
assert!(Timestamp::MAX.to_string().starts_with("Invalid timestamp:"));
}
}

0 comments on commit c8db6a7

Please sign in to comment.