Skip to content

Fix overflow in the max_array_size check of array pad - #1218

Merged
schungx merged 1 commit into
rhaiscript:mainfrom
Rockyrok77:array-pad-size-overflow
Nov 19, 2026
Merged

schungx merged 1 commit into
rhaiscript:mainfrom
Rockyrok77:array-pad-size-overflow

Conversation

@Rockyrok77

Copy link
Copy Markdown
Contributor

With max_array_size set, padding an array to a huge length gets past the size check and takes down the host:

// Engine with max_array_size(10_000), release build
let item = [];
item.pad(63, 0);
let a = [];
a.pad(288230376151711744, item);   // memory allocation of 4611686018427387904 bytes failed (SIGABRT)

array_functions::pad projects the padded size as a + pad + ax * pad (and m + mx * pad, s + sx * pad for maps and strings) in plain usize arithmetic, and pad comes straight from the script. In a release build the totals wrap: each copy of the 63-element item counts 64 towards the array total, 64 × 2^58 = 2^64 wraps to 0, throw_on_size sees an empty array, and Vec::resize then asks for 4 EiB. The allocation failure aborts the process, so neither catch_unwind nor any engine limit can stop it. A smaller item gives a panic instead, e.g. a.pad(4611686018427387904, [1, 2, 3]) hits "capacity overflow", and debug builds panic on the overflowing arithmetic itself. The totals are now saturated, so an oversized pad returns ErrorDataTooLarge like any other, and pads that fit compute the same totals as before. pad is the only place where a script-supplied count is multiplied into a size check; the string and BLOB pad and blob(len, value) compare the length directly.

Added an assertion to test_max_array_size in tests/data_size.rs, gated to 64-bit builds without only_i32 where the length fits. Without the fix it panics; with it, it passes. cargo test --tests passes with default features and with the no_object, no_index, only_i32, f32_float,only_i32, unchecked, sync, decimal, compact_str and no_float,only_i32,no_object,no_index,no_module,no_function,sync rows of the CI matrix. cargo fmt --check, the CI clippy invocation and cargo +1.66.0 check against Cargo.msrv.lock are clean. CHANGELOG line under 1.27.1's bug fixes.

@schungx schungx added the bug label Nov 18, 2026
@schungx

schungx commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

Great find. I wonder would we need to do this for maps as well? Perhaps the check_data_sizes function also...

@schungx

schungx commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

Ah seems that maps don't have a padding function so it doesn't apply.

@schungx
schungx merged commit 08377af into rhaiscript:main Nov 19, 2026
54 of 56 checks passed
@Rockyrok77

Copy link
Copy Markdown
Contributor Author

Agreed on maps. On check_data_size, I don't think it needs the same treatment: it and the calc_*_sizes helpers only add up data that already exists, element by element, so there's no script-supplied count multiplied in and the totals hit a limit long before they could get near usize::MAX. When I ran the other built-ins with huge length arguments, pad was the only one that got past the check. Thanks for merging this.

@schungx

schungx commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

I'm wondering whether we should also change check_data_sizes and family to use saturaring_add just in case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants