From bb1fc25812fb467263ca8009f5aca97ffafd2e00 Mon Sep 17 00:00:00 2001 From: Rocky Jim Date: Wed, 18 Nov 2026 00:28:29 +0530 Subject: [PATCH] Fix overflow in the max_array_size check of array pad --- CHANGELOG.md | 1 + src/packages/array_basic.rs | 8 ++++++-- tests/data_size.rs | 16 ++++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a49d97d43..df235c112 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ Bug fixes * (Fuzzing) Fixed panic when assigning to a property of a constant object. * Fixed undefined behavior (an aliasing violation detected by Miri) when a variable is passed to a function that does not take its first argument by `&mut`, e.g. `x.contains("a")` or `f(x)` for a script function. This was a regression in 1.27.0. * `sort` and `sort_desc` no longer panic on an array of floating-point numbers that contains NaN. NaN now sorts after every other number (before them for `sort_desc`) (thanks [`@Rockyrok77`](https://github.com/Rockyrok77)). +* `pad` on an array now enforces `max_array_size` when the padding length is large enough to overflow the size calculation. Previously it could pass the check and then panic or abort the process while allocating (thanks [`@Rockyrok77`](https://github.com/Rockyrok77)). Enhancements ------------ diff --git a/src/packages/array_basic.rs b/src/packages/array_basic.rs index 25527d63e..c4f61ee82 100644 --- a/src/packages/array_basic.rs +++ b/src/packages/array_basic.rs @@ -271,8 +271,12 @@ pub mod array_functions { let (a, m, s) = crate::eval::calc_array_sizes(array); let (ax, mx, sx) = crate::eval::calc_data_sizes(&item, true); - _ctx.engine() - .throw_on_size((a + pad + ax * pad, m + mx * pad, s + sx * pad))?; + // `pad` comes from the script, so saturate to keep the totals from wrapping + _ctx.engine().throw_on_size(( + a.saturating_add(pad).saturating_add(ax.saturating_mul(pad)), + m.saturating_add(mx.saturating_mul(pad)), + s.saturating_add(sx.saturating_mul(pad)), + ))?; } array.resize(len, item); diff --git a/tests/data_size.rs b/tests/data_size.rs index 170685706..f5eae8c80 100644 --- a/tests/data_size.rs +++ b/tests/data_size.rs @@ -199,6 +199,22 @@ fn test_max_array_size() { EvalAltResult::ErrorDataTooLarge(..) )); + // 4 * 2^62 wraps to zero in a 64-bit usize + #[cfg(not(feature = "no_object"))] + #[cfg(not(feature = "only_i32"))] + #[cfg(target_pointer_width = "64")] + assert!(matches!( + *engine + .run( + " + let x = []; + x.pad(4611686018427387904, [1, 2, 3]); + " + ) + .unwrap_err(), + EvalAltResult::ErrorDataTooLarge(..) + )); + assert_eq!( engine .eval::(