You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
memory error with strings in structured arrays #473
The following code writes to memory blocks beyond the limits of the array, resulting in memory errors in both released and dev version of numpy (I've tried 1.6.2 and 1.8.0.dev):
In [1]: import numpy as np
In [2]: dtype = [('I', int), ('S', str)]
In [3]: x = np.zeros(3, dtype=dtype)
In [4]: x['S'] = ['a', 'b', 'c']
The problem (I believe) is that passing str to dtype results in a zero-length string, and this is not checked on assignment.
By taking the example in this issue a little further we demonstrate the root of this issue. For some reason when you call np.zeros (and probably other functions) with a zero-width string type, it replaces the dtype with a one-width string (in the case of unicode this means 'U1', so 4 bytes):
Unexpected, but whatever. If backward compatibility is a concern I wouldn't suggest changing this. However, when using a structured dtype that includes a zero width string no such conversion occurs:
Looks like there will be a 1.10.1 real soon now on account of the reported problems with msvc 2008, so it would be good to get a fix in. I assume this impacts astropy?
@charris Fortunately this impacts astropy only tangentially. I've never run into it myself. It could in principle though. Honestly #2196 is more impactful in that it's pretty inconvenient. Though I can imagine a case where this bug would bite too in certain FITS files (albeit obscure).
So I'll investigate how difficult this is to fix, and it would be great to get a fix in 1.10.1 if I can come up with a reasonable patch. But it's not that important either.
Well, this bug is involved in quite a few segfault issues which would be nice to fix.
@embray, that sounds like the problem, nice find. Do you happen to know where the type conversion happens? I think I looked for it once but didn't find it.
Activity
BUG: solve issue numpy#2649.
BUG: fix 2649.
embray commentedon Oct 8, 2015
By taking the example in this issue a little further we demonstrate the root of this issue. For some reason when you call
np.zeros(and probably other functions) with a zero-width string type, it replaces the dtype with a one-width string (in the case of unicode this means 'U1', so 4 bytes):Unexpected, but whatever. If backward compatibility is a concern I wouldn't suggest changing this. However, when using a structured dtype that includes a zero width string no such conversion occurs:
But when accessing the 'S' field, the array returned again promotes the dtype to 'S1':
This is definitely a bug and should be changed.
charris commentedon Oct 8, 2015
Looks like there will be a 1.10.1 real soon now on account of the reported problems with msvc 2008, so it would be good to get a fix in. I assume this impacts astropy?
embray commentedon Oct 8, 2015
@charris Fortunately this impacts astropy only tangentially. I've never run into it myself. It could in principle though. Honestly #2196 is more impactful in that it's pretty inconvenient. Though I can imagine a case where this bug would bite too in certain FITS files (albeit obscure).
So I'll investigate how difficult this is to fix, and it would be great to get a fix in 1.10.1 if I can come up with a reasonable patch. But it's not that important either.
ahaldane commentedon Oct 8, 2015
Well, this bug is involved in quite a few segfault issues which would be nice to fix.
@embray, that sounds like the problem, nice find. Do you happen to know where the type conversion happens? I think I looked for it once but didn't find it.
embray commentedon Oct 8, 2015
It's in
PyArray_NewFromDescr_int. There are a few different solutions to this I'm thinking of. Will try one and see how it pans out...BUG: Fix numerous bugs related to zero-width string arrays (numpy#473, …
ahaldane commentedon Jun 2, 2016
Fixed by #6430