8000 Record arrays with fields that contain multiple objects will segfault on array.resize · Issue #4857 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

Record arrays with fields that contain multiple objects will segfault on array.resize #4857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
fgregg opened this issue Jul 9, 2014 · 2 comments · Fixed by #4889
Closed

Comments

@fgregg
Copy link
fgregg commented Jul 9, 2014

This short script will always segfault for me.

import numpy
dtype = [('multiple objects', object, 2)]
a = numpy.ones(10, dtype=dtype)
a.resize(15,)
print a
@juliantaylor
Copy link
Contributor

is this even the expected result?

In [1]: numpy.zeros(10, dtype=[('multiple objects', object, 2)])
Out[1]: 
array([([0, None],), ([0, None],), ([0, None],), ([0, None],),
       ([0, None],), ([0, None],), ([0, None],), ([0, None],),
       ([0, None],), ([0, None],)], 
      dtype=[('multiple objects', 'O', (2,))])

shouldn't it be all zero and not first zero rest none?

the issue appears as the resize path only sets the first entry and the rest is not memset to 0 to get the None

@juliantaylor
Copy link
Contributor

fix would be something along the lines of this, assuming this behavior is indeed intended:

--- a/numpy/core/src/multiarray/shape.c
+++ b/numpy/core/src/multiarray/shape.c
@@ -328,6 +328,8 @@ _putzero(char *optr, PyObject *zero, PyArray_Descr *dtype)
     else {
         Py_INCREF(zero);
         NPY_COPY_PYOBJECT_PTR(optr, &zero);
+        npy_intp elsize = dtype->elsize;
+        memset(optr + sizeof(PyObject*), 0, elsize - sizeof(PyObject*));
     }
     return;
 }

juliantaylor added a commit to juliantaylor/numpy that referenced this issue Jul 18, 2014
np.zeros(2, dtype=[('k', object, 2)]) did only initialize the first
element to zero while the rest stayed None.

    In [1]: numpy.zeros(2, dtype=[('k', object, 2)])
    Out[1]:
    array([([0, None],), ([0, None],)],
          dtype=[('k', 'O', (2,))])

This is a surprising and likely not intended behavior which is fixed
here.
The changed function PyArray_FillObjectArray is only used with None or
zero inputs from numpy, though as its part of the API it could affect
third parties but this is not very likely.

Additionally the memory after the first element was not initialized when
the object was resized.
Closes numpygh-4857
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants
0