Add missing data size check.

This commit is contained in:
Stephen Chung 2022-01-08 23:23:43 +08:00
parent f399e8a905
commit d1749131c5
2 changed files with 59 additions and 3 deletions

View File

@ -141,6 +141,7 @@ impl Engine {
let args = &mut [lhs_ptr_inner, &mut new_val];
match self.call_native_fn(global, state, lib, op, hash, args, true, true, op_pos) {
Ok(_) => self.check_data_size(&mut args[0], root.1)?,
Err(err) if matches!(*err, ERR::ErrorFunctionNotFound(ref f, _) if f.starts_with(op)) =>
{
// Expand to `var = var op rhs`
@ -153,7 +154,7 @@ impl Engine {
*args[0] = value.flatten();
}
err => return err.map(|_| ()),
Err(err) => return Err(err),
}
} else {
// Normal assignment

View File

@ -1,5 +1,5 @@
#![cfg(not(feature = "unchecked"))]
use rhai::{Engine, EvalAltResult, ParseErrorType};
use rhai::{Engine, EvalAltResult, ParseErrorType, INT};
#[cfg(not(feature = "no_index"))]
use rhai::Array;
@ -101,6 +101,39 @@ fn test_max_array_size() -> Result<(), Box<EvalAltResult>> {
EvalAltResult::ErrorDataTooLarge(_, _)
));
#[cfg(not(feature = "no_closure"))]
assert_eq!(
engine.eval::<INT>(
"
let x = 42;
let y = [];
let f = || x;
for n in 0..10 {
y += x;
}
len(y)
"
)?,
10
);
#[cfg(not(feature = "no_closure"))]
assert!(matches!(
*engine
.run(
"
let x = 42;
let y = [];
let f = || x;
for n in 0..11 {
y += x;
}
"
)
.expect_err("should error"),
EvalAltResult::ErrorDataTooLarge(_, _)
));
assert!(matches!(
*engine
.run(
@ -113,13 +146,25 @@ fn test_max_array_size() -> Result<(), Box<EvalAltResult>> {
EvalAltResult::ErrorDataTooLarge(_, _)
));
#[cfg(not(feature = "no_object"))]
assert_eq!(
engine.eval::<INT>(
"
let x = [1,2,3,4,5,6];
x.pad(10, 42);
len(x)
"
)?,
10
);
#[cfg(not(feature = "no_object"))]
assert!(matches!(
*engine
.run(
"
let x = [1,2,3,4,5,6];
x.pad(100, 42);
x.pad(11, 42);
x
"
)
@ -127,6 +172,16 @@ fn test_max_array_size() -> Result<(), Box<EvalAltResult>> {
EvalAltResult::ErrorDataTooLarge(_, _)
));
assert_eq!(
engine.eval::<INT>(
"
let x = [1,2,3];
len([x, x, x])
"
)?,
3
);
assert!(matches!(
*engine
.run(