From fcb5059570f91730530b42bd7f4d44832fb25774 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 15 Jun 2020 22:04:14 +0800 Subject: [PATCH] Do not check for data oversize when unchecked. --- src/packages/array_basic.rs | 25 +++++++++++---------- src/packages/string_more.rs | 43 ++++++++++++++++++++----------------- 2 files changed, 37 insertions(+), 31 deletions(-) diff --git a/src/packages/array_basic.rs b/src/packages/array_basic.rs index c8615865..c6904e90 100644 --- a/src/packages/array_basic.rs +++ b/src/packages/array_basic.rs @@ -29,24 +29,27 @@ fn pad(engine: &Engine, args: &mut [&mut Dynamic]) -> FuncRe let len = *args[1].downcast_ref::().unwrap(); // Check if array will be over max size limit - if engine.max_array_size > 0 && len > 0 && (len as usize) > engine.max_array_size { - Err(Box::new(EvalAltResult::ErrorDataTooLarge( - "Size of array".to_string(), - engine.max_array_size, - len as usize, - Position::none(), - ))) - } else if len >= 0 { + #[cfg(not(feature = "unchecked"))] + { + if engine.max_array_size > 0 && len > 0 && (len as usize) > engine.max_array_size { + return Err(Box::new(EvalAltResult::ErrorDataTooLarge( + "Size of array".to_string(), + engine.max_array_size, + len as usize, + Position::none(), + ))); + } + } + + if len >= 0 { let item = args[2].downcast_ref::().unwrap().clone(); let list = args[0].downcast_mut::().unwrap(); while list.len() < len as usize { push(list, item.clone())?; } - Ok(()) - } else { - Ok(()) } + Ok(()) } macro_rules! reg_op { diff --git a/src/packages/string_more.rs b/src/packages/string_more.rs index 6b5928c1..804af8ef 100644 --- a/src/packages/string_more.rs +++ b/src/packages/string_more.rs @@ -222,32 +222,35 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str let len = *args[1].downcast_ref::< INT>().unwrap(); // Check if string will be over max size limit - if engine.max_string_size > 0 && len > 0 && (len as usize) > engine.max_string_size { + #[cfg(not(feature = "unchecked"))] + { + if engine.max_string_size > 0 && len > 0 && (len as usize) > engine.max_string_size { + return Err(Box::new(EvalAltResult::ErrorDataTooLarge( + "Length of string".to_string(), + engine.max_string_size, + len as usize, + Position::none(), + ))); + } + } + + let ch = *args[2].downcast_ref::< char>().unwrap(); + let s = args[0].downcast_mut::().unwrap(); + + let copy = s.make_mut(); + for _ in 0..copy.chars().count() - len as usize { + copy.push(ch); + } + + if engine.max_string_size > 0 && copy.len() > engine.max_string_size { Err(Box::new(EvalAltResult::ErrorDataTooLarge( "Length of string".to_string(), engine.max_string_size, - len as usize, + copy.len(), Position::none(), ))) } else { - let ch = *args[2].downcast_ref::< char>().unwrap(); - let s = args[0].downcast_mut::().unwrap(); - - let copy = s.make_mut(); - for _ in 0..copy.chars().count() - len as usize { - copy.push(ch); - } - - if engine.max_string_size > 0 && copy.len() > engine.max_string_size { - Err(Box::new(EvalAltResult::ErrorDataTooLarge( - "Length of string".to_string(), - engine.max_string_size, - copy.len(), - Position::none(), - ))) - } else { - Ok(()) - } + Ok(()) } }, );