From 55881c92df70c8c3863d2d5641aa0cebe42b65a6 Mon Sep 17 00:00:00 2001 From: printfn Date: Fri, 15 Mar 2024 10:58:27 +0000 Subject: [PATCH] Set reasonable upper bound for roman numerals --- core/src/ast.rs | 12 +++++++++++- core/src/num.rs | 4 ++-- core/tests/integration_tests.rs | 1 + 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/core/src/ast.rs b/core/src/ast.rs index 2cc2e2b9..9d590af5 100644 --- a/core/src/ast.rs +++ b/core/src/ast.rs @@ -2,7 +2,7 @@ use crate::error::{FendError, Interrupt}; use crate::eval::evaluate_to_value; use crate::ident::Ident; use crate::interrupt::test_int; -use crate::num::{Base, FormattingStyle, Number}; +use crate::num::{Base, FormattingStyle, Number, Range, RangeBound}; use crate::result::FResult; use crate::scope::Scope; use crate::serialize::{Deserialize, Serialize}; @@ -628,6 +628,16 @@ fn evaluate_as( if a == 0 { return Err(FendError::RomanNumeralZero); } + let upper_limit = 100_000; + if a > upper_limit { + return Err(FendError::OutOfRange { + value: Box::new(a), + range: Range { + start: RangeBound::Closed(Box::new(1)), + end: RangeBound::Closed(Box::new(upper_limit)), + }, + }); + } return Ok(Value::String(borrow::Cow::Owned(to_roman(a)))); } _ => (), diff --git a/core/src/num.rs b/core/src/num.rs index 0c392002..afcc4e3f 100644 --- a/core/src/num.rs +++ b/core/src/num.rs @@ -39,8 +39,8 @@ impl RangeBound { #[derive(Debug)] pub(crate) struct Range { - start: RangeBound, - end: RangeBound, + pub(crate) start: RangeBound, + pub(crate) end: RangeBound, } impl Range { diff --git a/core/tests/integration_tests.rs b/core/tests/integration_tests.rs index b8e0d0db..e7880de9 100644 --- a/core/tests/integration_tests.rs +++ b/core/tests/integration_tests.rs @@ -5935,4 +5935,5 @@ fn test_roman() { test_eval_simple("3456 to roman", "MMMCDLVI"); test_eval_simple("1452 to roman", "MCDLII"); test_eval_simple("20002 to roman", "MMMMMMMMMMMMMMMMMMMMII"); + expect_error("100001 to roman", Some("100001 must lie in the interval [1, 100000]")); }