dmitry-chirkov-dremio commented on code in PR #52451:
URL: https://github.com/apache/arrow/pull/52451#discussion_r4334113162


##########
cpp/src/arrow/compute/kernels/scalar_string_ascii.cc:
##########
@@ -2642,7 +2642,11 @@ struct SliceBytesTransform : StringSliceTransformBase {
       // on the resulting slice lengths, so return a worst case estimate.
       return input_bytes;
     }
-    int64_t max_slice_bytes = (opt.stop - opt.start + opt.step - 1) / opt.step;
+    // SliceOptions uses INT64_MIN/MAX for open-ended bounds. Clamp both bounds
+    // before arithmetic so equivalent out-of-range indices cannot overflow 
the size estimate.
+    const int64_t start = std::clamp(opt.start, -input_bytes, input_bytes);
+    const int64_t stop = std::clamp(opt.stop, -input_bytes, input_bytes);
+    int64_t max_slice_bytes = (stop - start + opt.step - 1) / opt.step;

Review Comment:
   Check: input "abc", start=1, the default stop, and step=INT64_MAX
   
   The code around here is short, but the arithmetic is fragile. See if you can 
separate out boundary checks although this might as well be "one last boundary 
to enforce"...



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