pitrou commented on a change in pull request #9395:
URL: https://github.com/apache/arrow/pull/9395#discussion_r571938500



##########
File path: cpp/src/arrow/tensor.cc
##########
@@ -127,14 +162,31 @@ Status CheckTensorStridesValidity(const 
std::shared_ptr<Buffer>& data,
     return Status::OK();
   }
 
-  std::vector<int64_t> last_index(shape);
-  const int64_t n = static_cast<int64_t>(shape.size());
-  for (int64_t i = 0; i < n; ++i) {
-    --last_index[i];
+  // Check the largest offset can be computed without overflow
+  const size_t ndim = shape.size();
+  int64_t largest_offset = 0;
+  for (size_t i = 0; i < ndim; ++i) {
+    if (shape[i] == 0) continue;
+
+    int64_t dim_offset;
+    if (!internal::MultiplyWithOverflow(shape[i] - 1, strides[i], 
&dim_offset)) {
+      if (dim_offset <= 0) {
+        // Ignore the negative dim_offset here because we are interested in 
only the
+        // largest offset

Review comment:
       That doesn't seem right. What if all strides are negative?
   It seems you must maintain two separate offsets: a largest positive one, and 
a largest negative one.

##########
File path: cpp/src/arrow/tensor_test.cc
##########
@@ -152,6 +243,22 @@ TEST(TestTensor, MakeFailureCases) {
   // negative items in shape
   ASSERT_RAISES(Invalid, Tensor::Make(float64(), data, {-3, 6}));
 
+  // overflow in stride computation
+  constexpr uint64_t total_length =
+      1 + static_cast<uint64_t>(std::numeric_limits<int64_t>::max());
+  EXPECT_RAISES_WITH_MESSAGE_THAT(
+      Invalid,
+      testing::HasSubstr(
+          "Row-major strides computed from shape would not fit in 64-bit 
integer"),
+      Tensor::Make(float64(), data, {2, 2, static_cast<int64_t>(total_length / 
4)}));
+
+  // overflow by negative multiplication in strides validity check
+  EXPECT_RAISES_WITH_MESSAGE_THAT(
+      Invalid, testing::HasSubstr("would not fit in 64-bit integer"),
+      Tensor::Make(
+          float64(), data, {0, 4, 0},
+          {sizeof(double), -static_cast<int64_t>(total_length / 2), 
sizeof(double)}));

Review comment:
       What if, for example, `shape = {3, total_length / 3}` and `strides = 
{-total_length / 3, -1}`?




----------------------------------------------------------------
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.

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


Reply via email to