rhai/doc/src/safety/max-array-size.md

41 lines
1.6 KiB
Markdown
Raw Normal View History

2020-06-20 06:06:17 +02:00
Maximum Size of Arrays
=====================
{{#include ../links.md}}
2020-07-16 06:09:40 +02:00
Limit How Large Arrays Can Grow
------------------------------
2020-06-20 06:06:17 +02:00
Rhai by default does not limit how large an [array] can be.
This can be changed via the `Engine::set_max_array_size` method, with zero being unlimited (the default).
A script attempting to create an array literal larger than the maximum will terminate with a parse error.
Any script operation that produces an array larger than the maximum also terminates the script with an error result.
This check can be disabled via the [`unchecked`] feature for higher performance (but higher risks as well).
```rust
let mut engine = Engine::new();
2020-09-24 05:17:39 +02:00
engine.set_max_array_size(500); // allow arrays only up to 500 items
2020-06-20 06:06:17 +02:00
engine.set_max_array_size(0); // allow unlimited arrays
```
Setting Maximum Size
-------------------
Be conservative when setting a maximum limit and always consider the fact that a registered function may grow
an array's size without Rhai noticing until the very end.
For instance, the built-in '`+`' operator for arrays concatenates two arrays together to form one larger array;
if both arrays are _slightly_ below the maximum size limit, the resultant array may be almost _twice_ the maximum size.
As a malicious script may create a deeply-nested array which consumes huge amounts of memory while each individual
array still stays under the maximum size limit, Rhai also recursively adds up the sizes of all [strings], [arrays]
and [object maps] contained within each array to make sure that the _aggregate_ sizes of none of these data structures
exceed their respective maximum size limits (if any).