From a4b674d0157c4de6feab906c7122d8bc1c5f5fae Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Fri, 9 Oct 2020 12:51:26 +0800 Subject: [PATCH] Trap elapsed error when timestamp is later than now. --- src/packages/arithmetic.rs | 4 ++-- src/packages/time_basic.rs | 8 +++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/packages/arithmetic.rs b/src/packages/arithmetic.rs index 7b011ceb..c848fbe3 100644 --- a/src/packages/arithmetic.rs +++ b/src/packages/arithmetic.rs @@ -16,8 +16,8 @@ use num_traits::float::Float; use crate::stdlib::{format, string::String}; #[inline(always)] -pub fn make_err(msg: String) -> Box { - EvalAltResult::ErrorArithmetic(msg, Position::none()).into() +pub fn make_err(msg: impl Into) -> Box { + EvalAltResult::ErrorArithmetic(msg.into(), Position::none()).into() } macro_rules! gen_arithmetic_functions { diff --git a/src/packages/time_basic.rs b/src/packages/time_basic.rs index f53e703c..ab0cbd5e 100644 --- a/src/packages/time_basic.rs +++ b/src/packages/time_basic.rs @@ -34,7 +34,11 @@ mod time_functions { pub fn elapsed(timestamp: &mut Instant) -> Result> { #[cfg(not(feature = "no_float"))] { - Ok((timestamp.elapsed().as_secs_f64() as FLOAT).into()) + if *timestamp <= Instant::now() { + Ok((timestamp.elapsed().as_secs_f64() as FLOAT).into()) + } else { + Err(make_arithmetic_err("Time-stamp is later than now")) + } } #[cfg(feature = "no_float")] @@ -46,6 +50,8 @@ mod time_functions { "Integer overflow for timestamp.elapsed: {}", seconds ))) + } else if *timestamp <= Instant::now() { + Err(make_arithmetic_err("Time-stamp is later than now")) } else { Ok((seconds as INT).into()) }