From 6f3dd7f0c2d58b9e466800bacb4d1c0d8cb12ed0 Mon Sep 17 00:00:00 2001 From: link2xt Date: Fri, 10 Sep 2021 22:49:48 +0000 Subject: [PATCH] Use saturating addition for ephemeral timers Integer overflows crash the application by default. On a first sight this is only a potential crash that can't be triggered, because timestamps are stored as i64 and ephemeral timer duration is u32. --- src/chat.rs | 2 +- src/dc_receive_imf.rs | 4 +++- src/ephemeral.rs | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/chat.rs b/src/chat.rs index c39b63dd0..997b3df6d 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -1222,7 +1222,7 @@ impl Chat { }; let ephemeral_timestamp = match ephemeral_timer { EphemeralTimer::Disabled => 0, - EphemeralTimer::Enabled { duration } => time() + i64::from(duration), + EphemeralTimer::Enabled { duration } => time().saturating_add(duration.into()), }; let new_mime_headers = if msg.has_html() { diff --git a/src/dc_receive_imf.rs b/src/dc_receive_imf.rs index 67cb86a46..c39ac6482 100644 --- a/src/dc_receive_imf.rs +++ b/src/dc_receive_imf.rs @@ -1046,7 +1046,9 @@ INSERT INTO msgs } else { match ephemeral_timer { EphemeralTimer::Disabled => 0, - EphemeralTimer::Enabled { duration } => rcvd_timestamp + i64::from(duration), + EphemeralTimer::Enabled { duration } => { + rcvd_timestamp.saturating_add(duration.into()) + } } }; diff --git a/src/ephemeral.rs b/src/ephemeral.rs index b63b3511e..d48a7e024 100644 --- a/src/ephemeral.rs +++ b/src/ephemeral.rs @@ -279,7 +279,7 @@ impl MsgId { /// Starts ephemeral message timer for the message if it is not started yet. pub(crate) async fn start_ephemeral_timer(self, context: &Context) -> anyhow::Result<()> { if let Timer::Enabled { duration } = self.ephemeral_timer(context).await? { - let ephemeral_timestamp = time() + i64::from(duration); + let ephemeral_timestamp = time().saturating_add(duration.into()); context .sql