Skip to content

Commit e12d11d

Browse files
committed
Fix Zip::indexed for the 0-dimensional case
This commit fixes a panic for 0-dimensional, indexed `Zip` instances which results from an out-of-bounds index in a call to `IndexPtr::stride_offset` in `Zip::inner`. Basically, the "stride" for `IndexPtr` is the axis to update, but for the 0-dimensional case, there are no axes, so `IndexPtr::stride_offset` cannot be called without panicking due to the `self.index[stride]` access. The chosen solution is to add a special check to `Zip::apply_core` for the 0-dimensional case. Another possible solution would be to modify the loop of `Zip::inner` such that an offset would not be performed for an index of zero.
1 parent 3a2040d commit e12d11d

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/zip/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -691,12 +691,14 @@ impl<P, D> Zip<P, D>
691691
where
692692
D: Dimension,
693693
{
694-
fn apply_core<F, Acc>(&mut self, acc: Acc, function: F) -> FoldWhile<Acc>
694+
fn apply_core<F, Acc>(&mut self, acc: Acc, mut function: F) -> FoldWhile<Acc>
695695
where
696696
F: FnMut(Acc, P::Item) -> FoldWhile<Acc>,
697697
P: ZippableTuple<Dim = D>,
698698
{
699-
if self.layout.is(CORDER | FORDER) {
699+
if self.dimension.ndim() == 0 {
700+
function(acc, unsafe { self.parts.as_ref(self.parts.as_ptr()) })
701+
} else if self.layout.is(CORDER | FORDER) {
700702
self.apply_core_contiguous(acc, function)
701703
} else {
702704
self.apply_core_strided(acc, function)

tests/azip.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,19 @@ fn test_clone() {
303303
});
304304
}
305305

306+
#[test]
307+
fn test_indices_0() {
308+
let a1 = arr0(3);
309+
310+
let mut count = 0;
311+
Zip::indexed(&a1).apply(|i, elt| {
312+
count += 1;
313+
assert_eq!(i, ());
314+
assert_eq!(*elt, 3);
315+
});
316+
assert_eq!(count, 1);
317+
}
318+
306319
#[test]
307320
fn test_indices_1() {
308321
let mut a1 = Array::default(12);

0 commit comments

Comments
 (0)