Skip to content

memory corruption by ndarray_instance.data assignment (data should be made readonly) (Trac #1275) #1873

Closed

Description

@thouis

Original ticket http://projects.scipy.org/numpy/ticket/1275 on 2009-10-25 by trac user mcieslik, assigned to unknown.

It's probably not a bug, but I'd like to ask for clarification. I try to make two arrays share memory. I create big array from small arrays (this involves a copy e.g. by vstack). Create slice/views of the big array corresponding to the small arrays and do a array.data assignment i.e.:

small_array.data = big_array[some_slice].data

This works, both arrays can now modify the same memory, but is not safe as it can lead to memory corruption (see below). My first question: is this considered very very bad practice and if so how to point an ndarray instance to a new memory safely.

In [1]: import numpy as np
In [2]: zeros = np.zeros((2,2))
In [3]: double_zeros = np.vstack((zeros, zeros))
In [4]: double_zeros
Out[4]:             
array([[ 0.,  0.],  
       [ 0.,  0.],  
       [ 0.,  0.],  
       [ 0.,  0.]]) 
In [5]: zeros.data = double_zeros[0:3].data
In [6]: double_double_zeros = np.vstack((double_zeros,double_zeros))
In [7]: double_double_zeros
Out[7]:                    
array([[ 0.,  0.],         
       [ 0.,  0.],         
       [ 0.,  0.],         
       [ 0.,  0.],         
       [ 0.,  0.],         
       [ 0.,  0.],
       [ 0.,  0.],
       [ 0.,  0.]])
In [8]: double_zeros.data = double_double_zeros[0:5].data
In [9]: zeros
Out[9]:
array([[  1.25794259e-312,   9.42758674e-317],
       [  1.58101007e-322,   2.37151510e-322]])

In [10]: zeros[0][0] = 1.1
In [11]: zeros
Out[11]:
array([[  9.88131292e-324,   3.95252517e-323],
       [  4.94065646e-324,   2.42092166e-322]])
In [12]: zeros[0][0] = 2
In [13]: *** glibc detected *** /usr/bin/python: corrupted double-linked list: 0x000000000117dab0 ***
(this kills the interpreter)

Activity

  1. thouis commented on Oct 19, 2012

    @thouis
    ContributorAuthor

    @rgommers wrote on 2010-07-18

    Asking this on the mailing list will probably get you an answer. Here it probably won't any time soon.

  2. thouis commented on Oct 19, 2012

    @thouis
    ContributorAuthor

    @mwiebe wrote on 2011-03-25

    This looks like a bug to me - data should be a read-only attribute of the array class. This crashes for me in 1.6 beta.

  3. thouis commented on Oct 19, 2012

    @thouis
    ContributorAuthor

    Milestone changed to NumPy 2.0 by @mwiebe on 2011-03-25

  4. thouis commented on Oct 19, 2012

    @thouis
    ContributorAuthor

    Title changed from memory corruption by ndarray_instance.data assignment to memory corruption by ndarray_instance.data assignment (data should be made readonly) by @mwiebe on 2011-03-25

  5. charris commented on Feb 16, 2014

    @charris
    Member

    Oh, that is very, very bad ;) And we should not allow it.

  6. added a commit that references this issue on Jun 11, 2015
    a2e62e8
  7. jaimefrio commented on Mar 27, 2016

    @jaimefrio
    Member

    Fixed by #7275, at least whenever the deprecation is made an actual error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions