diff --git a/cpp/src/arrow/ipc/metadata_internal.cc b/cpp/src/arrow/ipc/metadata_internal.cc index 7f2a47b06947..4923be553f5a 100644 --- a/cpp/src/arrow/ipc/metadata_internal.cc +++ b/cpp/src/arrow/ipc/metadata_internal.cc @@ -1526,10 +1526,19 @@ Status GetSparseCSFIndexMetadata(const flatbuf::SparseTensorIndexCSF* sparse_ind RETURN_NOT_OK(IntFromFlatbuffer(sparse_index->indptrType(), indptr_type)); RETURN_NOT_OK(IntFromFlatbuffer(sparse_index->indicesType(), indices_type)); - const int ndim = static_cast(sparse_index->axisOrder()->size()); + auto* fb_axis_order = sparse_index->axisOrder(); + auto* fb_indices_buffers = sparse_index->indicesBuffers(); + if (fb_axis_order == nullptr || fb_indices_buffers == nullptr || + fb_axis_order->size() != fb_indices_buffers->size()) { + return Status::Invalid( + "Inconsistent CSF sparse index: axisOrder and indicesBuffers have different " + "lengths"); + } + + const int ndim = static_cast(fb_axis_order->size()); for (int i = 0; i < ndim; ++i) { - axis_order->push_back(sparse_index->axisOrder()->Get(i)); - indices_size->push_back(sparse_index->indicesBuffers()->Get(i)->length()); + axis_order->push_back(fb_axis_order->Get(i)); + indices_size->push_back(fb_indices_buffers->Get(i)->length()); } return Status::OK(); diff --git a/cpp/src/arrow/ipc/reader.cc b/cpp/src/arrow/ipc/reader.cc index 512305d6570a..0b66f4e5f67b 100644 --- a/cpp/src/arrow/ipc/reader.cc +++ b/cpp/src/arrow/ipc/reader.cc @@ -2374,6 +2374,13 @@ Result> ReadSparseCSFIndex( const auto ndim = static_cast(shape.size()); auto* indptr_buffers = sparse_index->indptrBuffers(); auto* indices_buffers = sparse_index->indicesBuffers(); + if (ndim < 1 || indptr_buffers == nullptr || indices_buffers == nullptr || + static_cast(indptr_buffers->size()) != ndim - 1 || + static_cast(indices_buffers->size()) != ndim) { + return Status::Invalid( + "Inconsistent CSF sparse index: buffer counts do not match the number of " + "dimensions"); + } std::vector> indptr_data(ndim - 1); std::vector> indices_data(ndim);