Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/nanoarrow/common/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -1619,6 +1619,17 @@ static int ArrowArrayViewValidateMinimal(struct ArrowArrayView* array_view,
// Only loop over the first two buffers because the size of the third buffer
// is always data dependent for all current Arrow types.
for (int i = 0; i < 2; i++) {
// Ensure that the buffer sizes calculated below fit within an int64 (the largest
// is the element size in bits times offset + length + 1, rounded up to bytes)
int64_t element_size_bits = array_view->layout.element_size_bits[i];
if (element_size_bits > 0 &&
offset_plus_length > (INT64_MAX - 8) / element_size_bits - 1) {
ArrowErrorSet(error,
"Offset + length of %" PRId64 " is too large for %s array buffer %d",
offset_plus_length, ArrowTypeString(array_view->storage_type), i);
return EINVAL;
}

int64_t element_size_bytes = array_view->layout.element_size_bits[i] / 8;
// Initialize with a value that will cause an error if accidentally used uninitialized
// Need to suppress the clang-tidy warning because gcc warns for possible use
Expand Down Expand Up @@ -1724,6 +1735,14 @@ static int ArrowArrayViewValidateMinimal(struct ArrowArrayView* array_view,
break;

case NANOARROW_TYPE_FIXED_SIZE_LIST:
if (array_view->layout.child_size_elements > 0 &&
offset_plus_length > INT64_MAX / array_view->layout.child_size_elements) {
ArrowErrorSet(error,
"Offset + length of %" PRId64
" is too large for fixed_size_list array with list size %" PRId64,
offset_plus_length, array_view->layout.child_size_elements);
return EINVAL;
}
child_min_length = (array_view->offset + array_view->length) *
array_view->layout.child_size_elements;
if (array_view->children[0]->length < child_min_length) {
Expand Down
38 changes: 38 additions & 0 deletions src/nanoarrow/common/array_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2842,6 +2842,44 @@ TEST(ArrayTest, ArrayViewTestMove) {
ArrowArrayViewReset(&array_view2);
}

TEST(ArrayTest, ArrayViewTestBufferSizeOverflow) {
struct ArrowError error;

// 4 * (2^62 + 1) wraps around to 4, so without a check a 16-byte data buffer
// would pass as large enough for 2^62 + 1 int32 values
struct ArrowArrayView array_view;
ArrowArrayViewInitFromType(&array_view, NANOARROW_TYPE_INT32);
uint8_t data[16] = {0};
array_view.length = (int64_t{1} << 62) + 1;
array_view.buffer_views[1].data.as_uint8 = data;
array_view.buffer_views[1].size_bytes = sizeof(data);
EXPECT_EQ(
ArrowArrayViewValidate(&array_view, NANOARROW_VALIDATION_LEVEL_MINIMAL, &error),
EINVAL);
EXPECT_STREQ(error.message,
"Offset + length of 4611686018427387905 is too large for int32 array "
"buffer 1");
ArrowArrayViewReset(&array_view);

// The same for a fixed-size list's minimum child length
nanoarrow::UniqueSchema schema;
ArrowSchemaInit(schema.get());
ASSERT_EQ(ArrowSchemaSetTypeFixedSize(schema.get(), NANOARROW_TYPE_FIXED_SIZE_LIST, 4),
NANOARROW_OK);
ASSERT_EQ(ArrowSchemaSetType(schema->children[0], NANOARROW_TYPE_NA), NANOARROW_OK);
nanoarrow::UniqueArrayView list_view;
ASSERT_EQ(ArrowArrayViewInitFromSchema(list_view.get(), schema.get(), &error),
NANOARROW_OK);
list_view->length = (int64_t{1} << 62) + 1;
list_view->children[0]->length = 4;
EXPECT_EQ(
ArrowArrayViewValidate(list_view.get(), NANOARROW_VALIDATION_LEVEL_MINIMAL, &error),
EINVAL);
EXPECT_STREQ(error.message,
"Offset + length of 4611686018427387905 is too large for fixed_size_list "
"array with list size 4");
}

TEST(ArrayTest, ArrayViewTestValidateFullUnalignedIntegers) {
// Run ends, list view offsets and sizes, and dictionary indices may be in
// unaligned buffers; validation must not read them through aligned pointers
Expand Down
Loading