Skip to content

Commit d60ea08

Browse files
committed
[C++] Add regression test for SparseCSFIndex::Equals segfault
1 parent 2d07b38 commit d60ea08

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

cpp/src/arrow/sparse_tensor_test.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,4 +1676,29 @@ TEST(TestSparseCSFMatrixForUInt64Index, Make) {
16761676
ASSERT_RAISES(Invalid, SparseCSFTensor::Make(dense_tensor, uint64()));
16771677
}
16781678

1679+
TEST(TestSparseCSFIndex, EqualsMismatchedDimensions) {
1680+
std::vector<int64_t> axis_order_4d = {0, 1, 2, 3};
1681+
std::vector<int64_t> axis_order_2d = {0, 1};
1682+
1683+
// Create empty tensors for indptr/indices
1684+
auto empty_buffer = std::make_shared<Buffer>(nullptr, 0);
1685+
auto tensor_4d = std::make_shared<Tensor>(int32(), empty_buffer, std::vector<int64_t>{0});
1686+
auto tensor_2d = std::make_shared<Tensor>(int32(), empty_buffer, std::vector<int64_t>{0});
1687+
1688+
// Create a 4D index
1689+
std::vector<std::shared_ptr<Tensor>> indptr_4d = {tensor_4d, tensor_4d, tensor_4d};
1690+
std::vector<std::shared_ptr<Tensor>> indices_4d = {tensor_4d, tensor_4d, tensor_4d, tensor_4d};
1691+
auto si_4d = std::make_shared<SparseCSFIndex>(indptr_4d, indices_4d, axis_order_4d);
1692+
1693+
// Create a 2D index
1694+
std::vector<std::shared_ptr<Tensor>> indptr_2d = {tensor_2d};
1695+
std::vector<std::shared_ptr<Tensor>> indices_2d = {tensor_2d, tensor_2d};
1696+
auto si_2d = std::make_shared<SparseCSFIndex>(indptr_2d, indices_2d, axis_order_2d);
1697+
1698+
// Before fix: This would segfault.
1699+
// After fix: Returns false safely.
1700+
ASSERT_FALSE(si_4d->Equals(*si_2d));
1701+
ASSERT_FALSE(si_2d->Equals(*si_4d));
1702+
}
1703+
16791704
} // namespace arrow

0 commit comments

Comments
 (0)