@@ -2332,9 +2332,6 @@ Result<std::shared_ptr<SparseIndex>> ReadSparseCSXIndex(
23322332 if (shape.size () != 2 ) {
23332333 return Status::Invalid (" Invalid shape length for a sparse matrix" );
23342334 }
2335- if (non_zero_length < 0 ) {
2336- return Status::Invalid (" Invalid non-zero length for a sparse matrix" );
2337- }
23382335
23392336 auto * sparse_index = sparse_tensor->sparseIndex_as_SparseMatrixIndexCSX ();
23402337
@@ -2404,8 +2401,16 @@ Result<std::shared_ptr<SparseIndex>> ReadSparseCSFIndex(
24042401 io::RandomAccessFile* file) {
24052402 auto * sparse_index = sparse_tensor->sparseIndex_as_SparseTensorIndexCSF ();
24062403 const auto ndim = static_cast <int64_t >(shape.size ());
2404+ if (ndim < 1 ) {
2405+ return Status::Invalid (" Invalid number of dimensions for a CSF sparse tensor" );
2406+ }
24072407 auto * indptr_buffers = sparse_index->indptrBuffers ();
24082408 auto * indices_buffers = sparse_index->indicesBuffers ();
2409+ if (static_cast <int64_t >(indptr_buffers->size ()) != ndim - 1 ||
2410+ static_cast <int64_t >(indices_buffers->size ()) != ndim) {
2411+ return Status::Invalid (
2412+ " Sparse CSF index buffer counts are inconsistent with the tensor shape" );
2413+ }
24092414 std::vector<std::shared_ptr<Buffer>> indptr_data (ndim - 1 );
24102415 std::vector<std::shared_ptr<Buffer>> indices_data (ndim);
24112416
@@ -2414,6 +2419,10 @@ Result<std::shared_ptr<SparseIndex>> ReadSparseCSFIndex(
24142419
24152420 RETURN_NOT_OK (internal::GetSparseCSFIndexMetadata (
24162421 sparse_index, &axis_order, &indices_size, &indptr_type, &indices_type));
2422+ if (static_cast <int64_t >(axis_order.size ()) != ndim) {
2423+ return Status::Invalid (
2424+ " Sparse CSF axisOrder length is inconsistent with the tensor shape" );
2425+ }
24172426 for (int i = 0 ; i < static_cast <int >(indptr_buffers->size ()); ++i) {
24182427 ARROW_ASSIGN_OR_RAISE (indptr_data[i], file->ReadAt (indptr_buffers->Get (i)->offset (),
24192428 indptr_buffers->Get (i)->length (),
0 commit comments