From 942a2d2d047c0c3be74931424fd29f28b25e77b2 Mon Sep 17 00:00:00 2001 From: LFC <990479+MichaelScofield@users.noreply.github.com> Date: Tue, 24 Nov 2026 18:28:17 +0800 Subject: [PATCH] fix(datatypes): bounds-check Vector::get and get_ref [Backport release/v1.3] (#10605) fix(datatypes): bounds-check Vector::get and get_ref (#10594) The indexed accessors checked is_valid() and then called value_unchecked(), but is_valid() is not a bounds check: it returns true for any index when the array has no null buffer. An out-of-range index read past the array buffers instead of panicking. Use the bounds-checked value() accessors. This also covers DictionaryVector, whose lookups go through the item vector's get(). (cherry picked from commit f51d01b1857a2fa6e29230a195ecef6c4f9e8517) Signed-off-by: Dennis Zhuang Co-authored-by: dennis zhuang --- src/datatypes/src/vectors.rs | 6 ++---- src/datatypes/src/vectors/decimal.rs | 3 +-- src/datatypes/src/vectors/primitive.rs | 13 +++++++++---- src/datatypes/src/vectors/string.rs | 7 +++++++ 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/datatypes/src/vectors.rs b/src/datatypes/src/vectors.rs index 38db5940df9..da025b85acf 100644 --- a/src/datatypes/src/vectors.rs +++ b/src/datatypes/src/vectors.rs @@ -260,8 +260,7 @@ macro_rules! impl_validity_for_vector { macro_rules! impl_get_for_vector { ($array: expr, $index: ident) => { if $array.is_valid($index) { - // Safety: The index have been checked by `is_valid()`. - unsafe { $array.value_unchecked($index).into() } + $array.value($index).into() } else { Value::Null } @@ -271,8 +270,7 @@ macro_rules! impl_get_for_vector { macro_rules! impl_get_ref_for_vector { ($array: expr, $index: ident) => { if $array.is_valid($index) { - // Safety: The index have been checked by `is_valid()`. - unsafe { $array.value_unchecked($index).into() } + $array.value($index).into() } else { ValueRef::Null } diff --git a/src/datatypes/src/vectors/decimal.rs b/src/datatypes/src/vectors/decimal.rs index 34d066f0725..f1e7d141c60 100644 --- a/src/datatypes/src/vectors/decimal.rs +++ b/src/datatypes/src/vectors/decimal.rs @@ -138,8 +138,7 @@ impl Decimal128Vector { /// Get decimal128 Value from array by index. fn get_decimal128_value_from_array(&self, index: usize) -> Option { if self.array.is_valid(index) { - // Safety: The index have been checked by `is_valid()`. - let value = unsafe { self.array.value_unchecked(index) }; + let value = self.array.value(index); // Safety: The precision and scale have been checked by Vector. Some(Decimal128::new(value, self.precision(), self.scale())) } else { diff --git a/src/datatypes/src/vectors/primitive.rs b/src/datatypes/src/vectors/primitive.rs index 92c8394e710..04276684ad7 100644 --- a/src/datatypes/src/vectors/primitive.rs +++ b/src/datatypes/src/vectors/primitive.rs @@ -164,8 +164,7 @@ impl Vector for PrimitiveVector { fn get(&self, index: usize) -> Value { if self.array.is_valid(index) { - // Safety: The index have been checked by `is_valid()`. - let wrapper = unsafe { T::Wrapper::from_native(self.array.value_unchecked(index)) }; + let wrapper = T::Wrapper::from_native(self.array.value(index)); wrapper.into() } else { Value::Null @@ -174,8 +173,7 @@ impl Vector for PrimitiveVector { fn get_ref(&self, index: usize) -> ValueRef<'_> { if self.array.is_valid(index) { - // Safety: The index have been checked by `is_valid()`. - let wrapper = unsafe { T::Wrapper::from_native(self.array.value_unchecked(index)) }; + let wrapper = T::Wrapper::from_native(self.array.value(index)); wrapper.into() } else { ValueRef::Null @@ -416,6 +414,13 @@ mod tests { assert_eq!(&ArrowDataType::Int32, arrow_arr.data_type()); } + #[test] + #[should_panic(expected = "Trying to access an element at index")] + fn test_get_out_of_bounds_without_nulls_panics() { + let vector = Int32Vector::from_slice([1]); + vector.get(1); + } + #[test] fn test_from_values() { let v = Int32Vector::from_values(vec![1, 2, 3, 4]); diff --git a/src/datatypes/src/vectors/string.rs b/src/datatypes/src/vectors/string.rs index 0476ecc6440..9a7999f2cc5 100644 --- a/src/datatypes/src/vectors/string.rs +++ b/src/datatypes/src/vectors/string.rs @@ -523,6 +523,13 @@ mod tests { use super::*; + #[test] + #[should_panic(expected = "Trying to access an element at index")] + fn test_get_ref_out_of_bounds_without_nulls_panics() { + let vector = StringVector::from(vec!["a"]); + vector.get_ref(1); + } + #[test] fn test_string_vector_build_get() { let mut builder = StringVectorBuilder::with_capacity(4);