diff --git a/src/packages/array_basic.rs b/src/packages/array_basic.rs index 2b9da1f5..bf06ed47 100644 --- a/src/packages/array_basic.rs +++ b/src/packages/array_basic.rs @@ -41,11 +41,13 @@ fn pad(engine: &Engine, args: &mut [&mut Dynamic]) -> FuncRe } } - if len >= 0 { + if len > 0 { let item = args[2].clone(); let list = args[0].downcast_mut::().unwrap(); - list.resize(len as usize, item); + if len as usize > list.len() { + list.resize(len as usize, item); + } } Ok(()) } diff --git a/src/packages/string_more.rs b/src/packages/string_more.rs index 2037c215..7bc10012 100644 --- a/src/packages/string_more.rs +++ b/src/packages/string_more.rs @@ -242,29 +242,31 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str } } - let ch = *args[2].downcast_ref::< char>().unwrap(); - let s = args[0].downcast_mut::().unwrap(); + if len > 0 { + let ch = *args[2].downcast_ref::< char>().unwrap(); + let s = args[0].downcast_mut::().unwrap(); - let orig_len = s.chars().count(); + let orig_len = s.chars().count(); - if orig_len < len as usize { - let p = s.make_mut(); + if len as usize > orig_len { + let p = s.make_mut(); - for _ in 0..(len as usize - orig_len) { - p.push(ch); + for _ in 0..(len as usize - orig_len) { + p.push(ch); + } + + if engine.max_string_size > 0 && s.len() > engine.max_string_size { + return Err(Box::new(EvalAltResult::ErrorDataTooLarge( + "Length of string".to_string(), + engine.max_string_size, + s.len(), + Position::none(), + ))); + } } } - if engine.max_string_size > 0 && s.len() > engine.max_string_size { - Err(Box::new(EvalAltResult::ErrorDataTooLarge( - "Length of string".to_string(), - engine.max_string_size, - s.len(), - Position::none(), - ))) - } else { - Ok(()) - } + Ok(()) }, ); lib.set_fn_3_mut(