8000 BUG: core: fix crash when unpickling data on Py3 under non-latin1 enc… · numpy/numpy@cd062f5 · GitHub
[go: up one dir, main page]

Skip to content

Commit cd062f5

Browse files
committed
BUG: core: fix crash when unpickling data on Py3 under non-latin1 encoding
1 parent 88cf0e4 commit cd062f5

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

numpy/core/src/multiarray/methods.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1670,6 +1670,13 @@ array_setstate(PyArrayObject *self, PyObject *args)
16701670
tmp = PyUnicode_AsLatin1String(rawdata);
16711671
Py_DECREF(rawdata);
16721672
rawdata = tmp;
1673+
if (tmp == NULL) {
1674+
/* More informative error message */
1675+
PyErr_SetString(PyExc_ValueError,
1676+
("Failed to encode latin1 string when unpickling a Numpy array. "
1677+
"pickle.load(a, encoding='latin1') is assumed."));
1678+
return NULL;
1679+
}
16731680
}
16741681
#endif
16751682

numpy/core/tests/test_regression.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1794,6 +1794,22 @@ def test_pickle_bytes_overwrite(self):
17941794
bytestring = "\x01 ".encode('ascii')
17951795
assert_equal(bytestring[0:1], '\x01'.encode('ascii'))
17961796

1797+
def test_pickle_py2_array_latin1_hack(self):
1798+
# Check that unpickling hacks in Py3 that support
1799+
# encoding='latin1' work correctly.
1800+
1801+
# Python2 output for pickle.dumps(numpy.array([129], dtype='b'))
1802+
data = asbytes("cnumpy.core.multiarray\n_reconstruct\np0\n(cnumpy\nndarray\np1\n(I0\n"
1803+
"tp2\nS'b'\np3\ntp4\nRp5\n(I1\n(I1\ntp6\ncnumpy\ndtype\np7\n(S'i1'\np8\n"
1804+
"I0\nI1\ntp9\nRp10\n(I3\nS'|'\np11\nNNNI-1\nI-1\nI0\ntp12\nbI00\nS'\\x81'\n"
1805+
"p13\ntp14\nb.")
1806+
if sys.version_info[0] >= 3:
1807+
# This should work:
1808+
result = pickle.loads(data, encoding='latin1')
1809+
assert_array_equal(result, np.array([129], dtype='b'))
1810+
# Should not segfault:
1811+
assert_raises(Exception, pickle.loads, data, encoding='koi8-r')
1812+
17971813
def test_structured_type_to_object(self):
17981814
a_rec = np.array([(0, 1), (3, 2)], dtype='i4,i8')
17991815
a_obj = np.empty((2,), dtype=object)

0 commit comments

Comments
 (0)
0