From abd809cc16d2fb14a28e9b8a170eb2dbdf801517 Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Fri, 22 Sep 2023 19:35:37 +0000 Subject: [PATCH] fix: unchecked conversion of array index --- --- a/crates/jrsonnet-evaluator/src/error.rs +++ b/crates/jrsonnet-evaluator/src/error.rs @@ -114,7 +114,7 @@ InComprehensionCanOnlyIterateOverArray, #[error("array out of bounds: {0} is not within [0,{1})")] - ArrayBoundsError(usize, usize), + ArrayBoundsError(isize, usize), #[error("string out of bounds: {0} is not within [0,{1})")] StringBoundsError(usize, usize), --- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs +++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs @@ -18,8 +18,8 @@ function::{CallLocation, FuncDesc, FuncVal}, typed::Typed, val::{CachedUnbound, IndexableVal, StrValue, Thunk, ThunkValue}, - Context, GcHashMap, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result, ResultExt, - State, Unbound, Val, Error, + Context, Error, GcHashMap, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result, + ResultExt, State, Unbound, Val, }; pub mod destructure; pub mod operator; @@ -518,8 +518,11 @@ if n.fract() > f64::EPSILON { bail!(FractionalIndex) } + if n < 0.0 { + bail!(ArrayBoundsError(n as isize, v.len())); + } v.get(n as usize)? - .ok_or_else(|| ArrayBoundsError(n as usize, v.len()))? + .ok_or_else(|| ArrayBoundsError(n as isize, v.len()))? } (Val::Arr(_), Val::Str(n)) => { bail!(AttemptedIndexAnArrayWithString(n.into_flat())) -- gitstuff