From 698b3d59545acfd7caa4039ad79ee69851dcc9d1 Mon Sep 17 00:00:00 2001 From: Shimi Bornstein Date: Wed, 6 Aug 2025 17:38:30 +0300 Subject: [PATCH] DEV-6150 Added length protection to left and right functions --- src/calculate.rs | 8 ++++++-- tests/test.rs | 3 +++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/calculate.rs b/src/calculate.rs index 3ebc697..e5c659e 100644 --- a/src/calculate.rs +++ b/src/calculate.rs @@ -980,7 +980,9 @@ fn calculate_right(number_string: (types::Value, types::Value)) -> types::Value let trimmed_string = match string { types::Value::Text(s) => { let temp: &'static str = Box::leak(s.into_boxed_str()); - &temp[(temp.len() - trim_length)..] + let len = temp.len(); + let start = if trim_length > len { len } else { trim_length }; + &temp[(len - start)..] } _ => "", }; @@ -997,7 +999,9 @@ fn calculate_left(number_string: (types::Value, types::Value)) -> types::Value { let trimmed_string = match string { types::Value::Text(s) => { let temp: &'static str = Box::leak(s.into_boxed_str()); - &temp[..trim_length] + let len = temp.len(); + let end = if trim_length > len { len } else { trim_length }; + &temp[..end] } _ => "", }; diff --git a/tests/test.rs b/tests/test.rs index ac2c946..3120e1d 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -779,6 +779,9 @@ fn it_evaluate_left_and_right_functions() { assert_eq!(evaluate_formula_string("=LEFT(\"apple\")"), "a",); assert_eq!(evaluate_formula_string("=\"P\"&LEFT(\"000\"&1,3)"), "P000",); + + assert_eq!(evaluate_formula_string("=LEFT(\"apple\", 10)"), "apple",); + assert_eq!(evaluate_formula_string("=RIGHT(\"apple\", 10)"), "apple",); } #[test]