Skip to content

Commit

Permalink
Catch out of bound indices
Browse files Browse the repository at this point in the history
  • Loading branch information
amol- committed Dec 2, 2024
1 parent ec40819 commit fe48db4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cpp/src/arrow/array/array_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,10 @@ TEST_F(TestArray, TestMakeMaskArray) {
ASSERT_OK(mask_array_from_array->ValidateFull());
ASSERT_EQ(mask_array_from_array->length(), 10);
AssertArraysEqual(*mask_array_from_array, *expected);

// Test out of bounds indices
ASSERT_RAISES(IndexError, MakeMaskArray({5, 10}, 8));
ASSERT_RAISES(IndexError, MakeMaskArray(ArrayFromJSON(int64(), "[5, 10]"), 8));
}

TEST_F(TestArray, ExtensionSpanRoundTrip) {
Expand Down
3 changes: 3 additions & 0 deletions cpp/src/arrow/array/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,9 @@ Result<std::shared_ptr<Array>> MakeMaskArrayImpl(
bit_util::SetBitsTo(buffer->mutable_data(), 0, length, false);
for (int64_t i = 0; i < indices->length(); ++i) {
int64_t index = indices->Value(i);
if (index < 0 || index >= length) {
return Status::IndexError("Index out of bounds: ", index);
}
bit_util::SetBit(buffer->mutable_data(), index);
}
return std::make_shared<BooleanArray>(length, buffer);
Expand Down

0 comments on commit fe48db4

Please sign in to comment.