Skip to content

Some arrays for which arr.size * arr.dtype.itemsize > max_ssize_t currently not recognized as invalid #7451

Closed

Description

@jaimefrio

The example is a little convoluted, but fails consistently:

import numpy as np
from numpy.lib.stride_tricks import as_strided
a = np.ones((1, 1))
len_ = np.iinfo(np.intp).max // 2 + 1
b = as_strided(a, shape=(1, len_), strides=(0, 0))
# next line segfaults
b.ravel()

Not sure what may be going on, but it sure doesn't happen if the strided view is much smaller.

Activity

  1. mhvk commented on Mar 22, 2016

    @mhvk
    Contributor

    Odd, does not segfault for me (1.10.4, 1.11.0.dev0+2ec0db0; Debian amd64).

  2. jaimefrio commented on Mar 23, 2016

    @jaimefrio
    MemberAuthor

    Of course you don't, because I left the offending line out: a call to b.ravel() at the end... Have edited my original post.

  3. seberg commented on Mar 23, 2016

    @seberg
    Member

    There is another odd thing here (and I get a MemoryError if I avoid it). The array should not be created in the first place. I guess it might be a separate issue though. For the original array arr.size * arr.dtype.itemsize is larger then ssize_t here (my old 1.8.2 fails on b = line).
    AFAIK, that is not considered a valid array.

  4. mhvk commented on Mar 23, 2016

    @mhvk
    Contributor

    Of course you don't, because I left the offending line out: a call to b.ravel() at the end... Have edited my original post.

    Indeed, now it "works"...

  5. seberg commented on Mar 23, 2016

    @seberg
    Member

    Git blame suggests the offending commit is 1475cfa, the sd * size later on, will go off into integer wraparound and should be checked (possibly start off with size = sd, maybe with an sd > 0 check -- not sure that can happen or not, somewhat think not, but...). Anyway, this also segfaults for copy, etc. as well, so nothing to do with ravel, just with malloc'ing empty arrays when the array is larger then legally possible.

  6. changed the title [-]Segfault with np.ravel[/-] [+]Some arrays for which `arr.size * arr.dtype.itemsize > max_ssize_t` currently not recognized as invalid[/+] on Mar 23, 2016
  7. jaimefrio commented on Mar 23, 2016

    @jaimefrio
    MemberAuthor

    I guess we have a winner then... The following fails with a memory error:

    from numpy.lib.stride_tricks import as_strided
    a = np.ones((1, 1))
    len_ = np.iinfo(np.intp).max // 2
    b = as_strided(a, shape=(1, len_), strides=(0, 0))
    b.ravel()

    The difference is that (np.iinfo(np.intp).max // 2 + 1) * 8 is a power of two, so when it overflows an int64, it results in a calculated array size of zero bytes. np.iinfo(np.intp).max // 2 * 8 is a power of two minus one, and thus has a binary representation full of ones, so even though it also overflows, it results in some ridiculously large amount of memory that cannot possibly be allocated.

  8. jaimefrio commented on Mar 23, 2016

    @jaimefrio
    MemberAuthor

    Ah, hadn't seen your message when I sent mine. Yes, that seems to be it.

  9. added a commit that references this issue on Jul 10, 2016
    af0beed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions