thouis / numpy-trac-migration

numpy Trac to github issues migration
2 stars 3 forks source link

Division by zero when creating array from N-dimensional new-style-buffer with zero length and no strides (Trac #2197) #5987

Open numpy-gitbot opened 11 years ago

numpy-gitbot commented 11 years ago

Original ticket http://projects.scipy.org/numpy/ticket/2197 on 2012-08-08 by trac user batavus, assigned to unknown.

I believe there's a bug in function _array_from_buffer_3118 in file numpy/numpy/core/src/multiarray/ctors.c

Consider the code which currently starts at line 1228

    if (view->strides != NULL) {
        for (k = 0; k < nd; ++k) {
            strides[k] = view->strides[k];
        }
    }
    else {
        d = view->len;
        for (k = 0; k < nd; ++k) {
            d /= view->shape[k];
            strides[k] = d;
        }
    }

if strides is NULL and shape is something containing at least on zero (e.g. [0] or [1,0,2]) such that view->len is zero, there will be a division 0 by 0 in "d /= view->shape[k];"

I suggest replacing the line

d /= view->shape[k];

by

if (view->shape[k]) d /= view->shape[k];

This should resolve the problem and be consistent with how strides is defined in numpy for such cases. Also, it's probably a good idea to add a test for this issue.