Skip to content

Commit 4eae55f

Browse files
committed
Fix indexing of zero-size nonzero-shape Fortran arrays
This is fix (and tests) for indexing a Fortran array that has zero size but has a shape with nonzero elements (e.g. shape `(1, 0)`). The bug was in `indices_iter_f()`, but this caused segfaults in `ArrayBase::from_shape_fn()`.
1 parent 1ed09a8 commit 4eae55f

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

‎src/indexes.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ pub fn indices_iter_f<E>(shape: E) -> IndicesIterF<E::Dim>
212212
let dim = shape.into_dimension();
213213
let zero = dim.zero_index();
214214
IndicesIterF {
215-
has_remaining: zero != dim,
215+
has_remaining: dim.size_checked() != Some(0),
216216
index: zero,
217217
dim: dim,
218218
}

‎tests/array-construct.rs‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
1+
#[macro_use]
2+
extern crate defmac;
23
extern crate ndarray;
34

45
use ndarray::prelude::*;
@@ -83,6 +84,21 @@ fn test_from_fn_f() {
8384
}
8485
}
8586

87+
#[test]
88+
fn test_from_fn_f_with_zero() {
89+
defmac!(test_from_fn_f_with_zero shape => {
90+
let a = Array::from_shape_fn(shape.f(), |i| i);
91+
assert_eq!(a.len(), 0);
92+
assert_eq!(a.shape(), &shape);
93+
});
94+
test_from_fn_f_with_zero!([0]);
95+
test_from_fn_f_with_zero!([0, 1]);
96+
test_from_fn_f_with_zero!([2, 0]);
97+
test_from_fn_f_with_zero!([0, 1, 2]);
98+
test_from_fn_f_with_zero!([2, 0, 1]);
99+
test_from_fn_f_with_zero!([1, 2, 0]);
100+
}
101+
86102
#[test]
87103
fn test_from_fn_f3() {
88104
let a = Array::from_shape_fn((4, 2, 7).f(), |i| i);

0 commit comments

Comments
 (0)