Skip to content

Commit

Permalink
Added full test validation
Browse files Browse the repository at this point in the history
  • Loading branch information
WillAyd committed Feb 12, 2025
1 parent b29ab56 commit 75ddddd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
28 changes: 28 additions & 0 deletions src/nanoarrow/common/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,34 @@ static int ArrowArrayViewValidateFull(struct ArrowArrayView* array_view,
}
}

if (array_view->storage_type == NANOARROW_TYPE_LIST_VIEW ||
array_view->storage_type == NANOARROW_TYPE_LARGE_LIST_VIEW) {
int64_t child_len = array_view->children[0]->length;

struct ArrowBufferView offsets, sizes;
offsets.data.data = array_view->buffer_views[1].data.data;
sizes.data.data = array_view->buffer_views[2].data.data;

for (int64_t i = 0; i < array_view->length; i++) {
int64_t offset, size;
if (array_view->storage_type == NANOARROW_TYPE_LIST_VIEW) {
offset = offsets.data.as_int32[i];
size = sizes.data.as_int32[i];
} else {
offset = offsets.data.as_int64[i];
size = sizes.data.as_int64[i];
}

if ((offset + size) > child_len) {
ArrowErrorSet(error,
"Offset: %" PRId64 " + size: %" PRId64 " at index: %" PRId64
" exceeds length of child view: %" PRId64,
offset, size, i, child_len);
return EINVAL;
}
}
}

// Recurse for children
for (int64_t i = 0; i < array_view->n_children; i++) {
NANOARROW_RETURN_NOT_OK(ArrowArrayViewValidateFull(array_view->children[i], error));
Expand Down
32 changes: 12 additions & 20 deletions src/nanoarrow/common/array_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1592,18 +1592,14 @@ TEST(ArrayTest, ArrayTestAppendToListViewArray) {
"Expected 1 child of list_view array but found 0 child arrays");
array.n_children = 1;

// Make sure final child size is checked at finish
// TODO: this may be an expensive check with LIST_VIEW types
/*
// Make sure size + offset is always within bounds of child array
array.children[0]->length = array.children[0]->length - 1;
EXPECT_EQ(ArrowArrayFinishBuildingDefault(&array, &error), EINVAL);
EXPECT_STREQ(
ArrowErrorMessage(&error),
"Expected child of list_view array to have length >= 3 but found array with "
"length 2");
EXPECT_EQ(ArrowArrayFinishBuilding(&array, NANOARROW_VALIDATION_LEVEL_FULL, &error),
EINVAL);
EXPECT_STREQ(ArrowErrorMessage(&error),
"Offset: 3 + size: 2 at index: 4 exceeds length of child view: 4");
array.children[0]->length = array.children[0]->length + 1;
*/

EXPECT_EQ(ArrowArrayFinishBuildingDefault(&array, &error), NANOARROW_OK);

#if defined(NANOARROW_BUILD_TESTS_WITH_ARROW)
Expand Down Expand Up @@ -1673,18 +1669,14 @@ TEST(ArrayTest, ArrayTestAppendToLargeListViewArray) {
"Expected 1 child of large_list_view array but found 0 child arrays");
array.n_children = 1;

// Make sure final child size is checked at finish
// TODO: this may be an expensive check with LIST_VIEW types
/*
// Make sure size + offset is always within bounds of child array
array.children[0]->length = array.children[0]->length - 1;
EXPECT_EQ(ArrowArrayFinishBuildingDefault(&array, &error), EINVAL);
EXPECT_STREQ(
ArrowErrorMessage(&error),
"Expected child of list_view array to have length >= 3 but found array with "
"length 2");
EXPECT_EQ(ArrowArrayFinishBuilding(&array, NANOARROW_VALIDATION_LEVEL_FULL, &error),
EINVAL);
EXPECT_STREQ(ArrowErrorMessage(&error),
"Offset: 3 + size: 2 at index: 4 exceeds length of child view: 4");
array.children[0]->length = array.children[0]->length + 1;
*/

EXPECT_EQ(ArrowArrayFinishBuildingDefault(&array, &error), NANOARROW_OK);

#if defined(NANOARROW_BUILD_TESTS_WITH_ARROW)
Expand Down

0 comments on commit 75ddddd

Please sign in to comment.