Fix Array::pad infinite loop.

This commit is contained in:
Stephen Chung 2022-01-13 22:05:07 +08:00
parent 5e32af0ceb
commit 5ab7c59ba0
3 changed files with 35 additions and 6 deletions

View File

@ -7,6 +7,7 @@ Version 1.5.0
Bug fixes Bug fixes
--------- ---------
* Padding arrays with another array via `pad` no longer loops indefinitely.
* `set_bit` for bit-flags with negative index now works correctly. * `set_bit` for bit-flags with negative index now works correctly.
* Misnamed `params` field `name` in the JSON output of `Engine::gen_fn_metadata_to_json` is fixed (was incorrectly named `type`). * Misnamed `params` field `name` in the JSON output of `Engine::gen_fn_metadata_to_json` is fixed (was incorrectly named `type`).

View File

@ -102,14 +102,30 @@ pub mod array_functions {
let check_sizes = false; let check_sizes = false;
if check_sizes { if check_sizes {
let arr = mem::take(array); let mut arr_len = array.len();
let mut arr = Dynamic::from_array(arr); let mut arr = Dynamic::from_array(mem::take(array));
while array.len() < len {
arr.write_lock::<Array>().unwrap().push(item.clone());
#[cfg(not(feature = "unchecked"))] #[cfg(not(feature = "unchecked"))]
_ctx.engine().ensure_data_size_within_limits(&arr)?; let (mut a1, mut m1, mut s1) = Engine::calc_data_sizes(&arr, true);
#[cfg(not(feature = "unchecked"))]
let (a2, m2, s2) = Engine::calc_data_sizes(&item, true);
{
let mut guard = arr.write_lock::<Array>().unwrap();
while arr_len < len {
#[cfg(not(feature = "unchecked"))]
{
a1 += a2;
m1 += m2;
s1 += s2;
_ctx.engine()
.raise_err_if_over_data_size_limit((a1, m1, s1), Position::NONE)?;
}
guard.push(item.clone());
arr_len += 1;
}
} }
*array = arr.into_array().unwrap(); *array = arr.into_array().unwrap();

View File

@ -22,6 +22,18 @@ fn test_arrays() -> Result<(), Box<EvalAltResult>> {
assert_eq!(engine.eval::<INT>("let y = [1, 2, 3]; y[-3]")?, 1); assert_eq!(engine.eval::<INT>("let y = [1, 2, 3]; y[-3]")?, 1);
assert!(engine.eval::<bool>("let y = [1, 2, 3]; 2 in y")?); assert!(engine.eval::<bool>("let y = [1, 2, 3]; 2 in y")?);
assert_eq!(engine.eval::<INT>("let y = [1, 2, 3]; y += 4; y[3]")?, 4); assert_eq!(engine.eval::<INT>("let y = [1, 2, 3]; y += 4; y[3]")?, 4);
assert_eq!(
engine.eval::<INT>("let y = [1, 2, 3]; pad(y, 5, 42); len(y)")?,
5
);
assert_eq!(
engine.eval::<INT>("let y = [1, 2, 3]; pad(y, 5, [42]); len(y)")?,
5
);
assert_eq!(
engine.eval::<INT>("let y = [1, 2, 3]; pad(y, 5, [42, 999, 123]); y[4][0]")?,
42
);
assert_eq!( assert_eq!(
engine engine
.eval::<Dynamic>("let y = [1, 2, 3]; y[1] += 4; y")? .eval::<Dynamic>("let y = [1, 2, 3]; y[1] += 4; y")?