8000 BUG: don't create array with invalid memory in where by juliantaylor · Pull Request #8964 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: don't create array with invalid memory in where #8964

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

Merged
merged 2 commits into from
May 6, 2017

Conversation

juliantaylor
Copy link
Contributor

When creating the tuple view of np.where, make sure the arrays point to
valid memory when they are empty.
Numpy assumes that even empty arrays have valid memory, e.g. in the
source stride == 0 assignments.
Closes gh-8922.

When creating the tuple view of np.where, make sure the arrays point to
valid memory when they are empty.
Numpy assumes that even empty arrays have valid memory, e.g. in the
source stride == 0 assignments.
Closes numpygh-8922.
@@ -6619,6 +6619,14 @@ def test_string(self):
assert_equal(np.where(True, a, b), "abcd")
assert_equal(np.where(False, b, a), "abcd")

def test_empty_result(self):
# pass empty where result through an assignment which reads the data of
# empty arrays, error detectable with valgrind, see gh-8922
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the real bug here that data is being read from an empty array?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

arguably yes, but doing so helps finding these bugs

@juliantaylor
Copy link
Contributor Author

We could also put a N > 0 guard onto https://github.com/numpy/numpy/blob/master/numpy/core/src/multiarray/lowlevel_strided_loops.c.src#L219 which might be the only place we actually are trying to read from empty arrays.
But this would prevent is from finding these types of bugs.

@eric-wieser
Copy link
Member
eric-wieser commented Apr 20, 2017

It seems to be that it should be perfectly legal to invoke PyArray_New with an empty shape and an invalid data pointer, with the understanding that no memory is allowed to be read from the underlying buffer.

Given that it's public API, this patch doesn't really do anything other that hide the issue in the first place. Third-party code could continue to call the constructor in this way.

@eric-wieser
Copy link
Member

IMO, the bug is either that @prefix@_@oper@_size@elsize@_srcstride0 is breaking contract when passed N == 0, or possibly that @prefix@_@oper@_size@elsize@_srcstride0 should not even have been called when N == 0

@juliantaylor
Copy link
Contributor Author

hm maybe it is better to just fix the function we know does it.

@eric-wieser
Copy link
Member

Presumably, something like np.ndarray('', strides=(1000, 1000), shape=(0,0)) could also trigger a similar error?

@juliantaylor
Copy link
Contributor Author

No as numpy gives empty arrays valid (uninitialized) memory for one block regardless. Though this should also trigger in that case harmless valgrind warnings.

@@ -206,6 +206,9 @@ static NPY_GCC_OPT_3 void
#else
npy_uint64 temp0, temp1;
#endif
if (N == 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could probably do with a comment along the lines of "dereferencing src when N == 0 is not allowed

@juliantaylor juliantaylor merged commit 25a6dfb into numpy:master May 6, 2017
@juliantaylor juliantaylor deleted the empty-read branch May 6, 2017 19:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0