Skip to content

Commit 358c8d9

Browse files
committed
Prevent overflow when calculating slice stride
For example, consider the case where the original axis length is `1`, the original stride is `2`, and `step` is `isize::MAX`.
1 parent b953fe5 commit 358c8d9

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/dimension/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,9 @@ pub fn do_slice(dim: &mut usize, stride: &mut usize, slice: Slice) -> isize {
390390
d + if r > 0 { 1 } else { 0 }
391391
};
392392

393-
// Update stride.
394-
*stride = (s * step) as usize;
393+
// Update stride. The additional check is necessary to avoid possible
394+
// overflow in the multiplication.
395+
*stride = if *dim <= 1 { 0 } else { (s * step) as usize };
395396

396397
offset
397398
}

0 commit comments

Comments
 (0)