Skip to content

Commit 2c55fa7

Browse files
committed
GH-50304: [C++][IPC] Reject negative sparse tensor shape and non-zero length
Validate dimension sizes and non_zero_length in GetSparseTensorMetadata so malformed IPC SparseTensor messages are rejected before the values flow into buffer-size computations. The now-redundant non-zero-length check in ReadSparseCSXIndex is removed in favour of this central guard.
1 parent e92d5d7 commit 2c55fa7

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

cpp/src/arrow/ipc/metadata_internal.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,6 +1559,9 @@ Status GetSparseTensorMetadata(const Buffer& metadata, std::shared_ptr<DataType>
15591559
auto dim = sparse_tensor->shape()->Get(i);
15601560

15611561
if (shape) {
1562+
if (dim->size() < 0) {
1563+
return Status::Invalid("Invalid sparse tensor dimension size: ", dim->size());
1564+
}
15621565
shape->push_back(dim->size());
15631566
}
15641567

@@ -1569,6 +1572,10 @@ Status GetSparseTensorMetadata(const Buffer& metadata, std::shared_ptr<DataType>
15691572
}
15701573

15711574
if (non_zero_length) {
1575+
if (sparse_tensor->non_zero_length() < 0) {
1576+
return Status::Invalid("Invalid sparse tensor non-zero length: ",
1577+
sparse_tensor->non_zero_length());
1578+
}
15721579
*non_zero_length = sparse_tensor->non_zero_length();
15731580
}
15741581

cpp/src/arrow/ipc/reader.cc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2339,9 +2339,6 @@ Result<std::shared_ptr<SparseIndex>> ReadSparseCSXIndex(
23392339
if (shape.size() != 2) {
23402340
return Status::Invalid("Invalid shape length for a sparse matrix");
23412341
}
2342-
if (non_zero_length < 0) {
2343-
return Status::Invalid("Invalid non-zero length for a sparse matrix");
2344-
}
23452342

23462343
auto* sparse_index = sparse_tensor->sparseIndex_as_SparseMatrixIndexCSX();
23472344

0 commit comments

Comments
 (0)