8000 BUG: Fix segfault in PyArray_OrderConverter · numpy/numpy@163a6f9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 163a6f9

Browse files
committed
BUG: Fix segfault in PyArray_OrderConverter
This fixes a bug in PyArray_OrderConverter where if a unicode string is passed in which cannot be converted to ASCII then Py_DECREF would be called on a null pointer. Fixes #7475
1 parent 75c5af3 commit 163a6f9

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

numpy/core/src/multiarray/conversion_utils.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,13 @@ PyArray_OrderConverter(PyObject *object, NPY_ORDER *val)
535535
PyObject *tmp;
536536
int ret;
537537
tmp = PyUnicode_AsASCIIString(object);
538+
if (tmp == NULL) {
539+
PyErr_SetString(PyExc_ValueError, "Invalid unicode string passed in "
540+
"for the array ordering. "
541+
"Please pass in 'C', 'F', 'A' "
542+
"or 'K' instead");
543+
return NPY_FAIL;
544+
}
538545
ret = PyArray_OrderConverter(tmp, val);
539546
Py_DECREF(tmp);
540547
return ret;

numpy/core/tests/test_multiarray.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6437,6 +6437,10 @@ def test_null_inside_ustring_array_is_truthy(self):
64376437
a[0] = ' \0 \0'
64386438
self.assertTrue(a)
64396439

6440+
def test_orderconverter_with_nonASCII_unicode_ordering():
6441+
# gh-7475
6442+
a = np.arange(5)
6443+
assert_raises(ValueError, a.flatten, order=u'\xe2')
64406444

64416445
if __name__ == "__main__":
64426446
run_module_suite()

0 commit comments

Comments
 (0)
0