Skip to content

Commit 66e6317

Browse files
committed
Issue python#15770: Check invalid arguments in test function. Patch by Victor Stinner.
1 parent f21587e commit 66e6317

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

‎Lib/test/test_buffer.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,8 @@ def test_ndarray_exceptions(self):
12121212
self.assertRaises(TypeError, get_contiguous, nd, PyBUF_READ, 961)
12131213
self.assertRaises(UnicodeEncodeError, get_contiguous, nd, PyBUF_READ,
12141214
'\u2007')
1215+
self.assertRaises(ValueError, get_contiguous, nd, PyBUF_READ, 'Z')
1216+
self.assertRaises(ValueError, get_contiguous, nd, 255, 'A')
12151217

12161218
# cmp_contig()
12171219
nd = ndarray([1], shape=[1])

‎Modules/_testbuffer.c‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2362,6 +2362,13 @@ get_ascii_order(PyObject *order)
23622362

23632363
ord = PyBytes_AS_STRING(ascii_order)[0];
23642364
Py_DECREF(ascii_order);
2365+
2366+
if (ord != 'C' && ord != 'F' && ord != 'A') {
2367+
PyErr_SetString(PyExc_ValueError,
2368+
"invalid order, must be C, F or A");
2369+
return CHAR_MAX;
2370+
}
2371+
23652372
return ord;
23662373
}
23672374

@@ -2384,15 +2391,20 @@ get_contiguous(PyObject *self, PyObject *args)
23842391
"buffertype must be PyBUF_READ or PyBUF_WRITE");
23852392
return NULL;
23862393
}
2394+
23872395
type = PyLong_AsLong(buffertype);
23882396
if (type == -1 && PyErr_Occurred()) {
23892397
return NULL;
23902398
}
2399+
if (type != PyBUF_READ && type != PyBUF_WRITE) {
2400+
PyErr_SetString(PyExc_ValueError,
2401+
"invalid buffer type");
2402+
return NULL;
2403+
}
23912404

23922405
ord = get_ascii_order(order);
2393-
if (ord == CHAR_MAX) {
2406+
if (ord == CHAR_MAX)
23942407
return NULL;
2395-
}
23962408

23972409
return PyMemoryView_GetContiguous(obj, (int)type, ord);
23982410
}

0 commit comments

Comments
 (0)