Copilot commented on code in PR #50817:
URL: https://github.com/apache/arrow/pull/50817#discussion_r4447262614


##########
cpp/src/arrow/sparse_tensor.cc:
##########
@@ -123,6 +125,58 @@ Status MakeSparseTensorFromTensor(const Tensor& tensor,
 
 namespace {
 
+// Callers validate vector ranks, buffers, and indptr lengths before 
inspecting values.
+Status ValidateSparseIndexBounds(const Tensor& indices, int64_t dimension_size,
+                                 const char* type_name) {
+  return VisitType(*indices.type(), [&](const auto& type) -> Status {
+    using IndexType = std::decay_t<decltype(type)>;
+    if constexpr (is_signed_integer(IndexType::type_id) ||
+                  std::is_same_v<IndexType, UInt8Type> ||
+                  std::is_same_v<IndexType, UInt16Type> ||
+                  std::is_same_v<IndexType, UInt32Type>) {
+      const auto* values =
+          reinterpret_cast<const typename 
IndexType::c_type*>(indices.raw_data());
+      for (int64_t i = 0; i < indices.shape()[0]; ++i) {
+        const int64_t index = values[i];
+        if (index < 0 || index >= dimension_size) {
+          return Status::Invalid(type_name, " index is out of bounds");
+        }
+      }
+      return Status::OK();
+    } else {
+      return Status::Invalid("Unsupported sparse index value type: ", 
type.ToString());
+    }
+  });
+}
+
+Status ValidateSparseIndptr(const Tensor& indptr, int64_t indices_length,
+                            const char* type_name) {
+  return VisitType(*indptr.type(), [&](const auto& type) -> Status {

Review Comment:
   An int64 indices tensor with shape `{2}`, stride `{0}`, and an 8-byte buffer 
passes `Tensor::Validate()`, but this packed scan reads `values[1]` past the 
buffer during CSX/CSF validation, including `Sparse*Tensor::Make()`. 
`ValidateSparseIndptr()` makes the same assumption when constructing its span. 
Reject non-contiguous vectors in both helpers before reading them, since the 
dense converters also require packed storage.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to