mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-11-29 14:14:13 +00:00
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 f51d01b185)
Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
Co-authored-by: dennis zhuang <killme2008@gmail.com>
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -138,8 +138,7 @@ impl Decimal128Vector {
|
||||
/// Get decimal128 Value from array by index.
|
||||
fn get_decimal128_value_from_array(&self, index: usize) -> Option<Decimal128> {
|
||||
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 {
|
||||
|
||||
@@ -164,8 +164,7 @@ impl<T: LogicalPrimitiveType> Vector for PrimitiveVector<T> {
|
||||
|
||||
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<T: LogicalPrimitiveType> Vector for PrimitiveVector<T> {
|
||||
|
||||
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]);
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user