thouis / numpy-trac-migration

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

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

Open numpy-gitbot opened 11 years ago

numpy-gitbot commented 11 years ago

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)
numpy-gitbot commented 11 years ago

atmention: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.

numpy-gitbot commented 11 years ago

atmention: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.

numpy-gitbot commented 11 years ago

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

numpy-gitbot commented 11 years ago

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