Skip to content

Commit

Permalink
Fix Zip::indexed for the 0-dimensional case
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jturner314 committed Dec 14, 2020
1 parent 3a2040d commit e12d11d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/zip/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,12 +691,14 @@ impl<P, D> Zip<P, D>
where
D: Dimension,
{
fn apply_core<F, Acc>(&mut self, acc: Acc, function: F) -> FoldWhile<Acc>
fn apply_core<F, Acc>(&mut self, acc: Acc, mut function: F) -> FoldWhile<Acc>
where
F: FnMut(Acc, P::Item) -> FoldWhile<Acc>,
P: ZippableTuple<Dim = D>,
{
if self.layout.is(CORDER | FORDER) {
if self.dimension.ndim() == 0 {
function(acc, unsafe { self.parts.as_ref(self.parts.as_ptr()) })
} else if self.layout.is(CORDER | FORDER) {
self.apply_core_contiguous(acc, function)
} else {
self.apply_core_strided(acc, function)
Expand Down
13 changes: 13 additions & 0 deletions tests/azip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,19 @@ fn test_clone() {
});
}

#[test]
fn test_indices_0() {
let a1 = arr0(3);

let mut count = 0;
Zip::indexed(&a1).apply(|i, elt| {
count += 1;
assert_eq!(i, ());
assert_eq!(*elt, 3);
});
assert_eq!(count, 1);
}

#[test]
fn test_indices_1() {
let mut a1 = Array::default(12);
Expand Down

0 comments on commit e12d11d

Please sign in to comment.