[Python-checkins] cpython: Issue #15770: Check invalid arguments in test function. Patch by Victor Stinner.

stefan.krah python-checkins at python.org
Thu Aug 23 15:58:42 CEST 2012


http://hg.python.org/cpython/rev/fa745ed89b7a
changeset:   78720:fa745ed89b7a
user:        Stefan Krah <skrah at bytereef.org>
date:        Thu Aug 23 15:53:45 2012 +0200
summary:
  Issue #15770: Check invalid arguments in test function. Patch by Victor Stinner.

files:
  Lib/test/test_buffer.py |   2 ++
  Modules/_testbuffer.c   |  18 +++++++++++++++---
  2 files changed, 17 insertions(+), 3 deletions(-)


diff --git a/Lib/test/test_buffer.py b/Lib/test/test_buffer.py
--- a/Lib/test/test_buffer.py
+++ b/Lib/test/test_buffer.py
@@ -1212,6 +1212,8 @@
         self.assertRaises(TypeError, get_contiguous, nd, PyBUF_READ, 961)
         self.assertRaises(UnicodeEncodeError, get_contiguous, nd, PyBUF_READ,
                           '\u2007')
+        self.assertRaises(ValueError, get_contiguous, nd, PyBUF_READ, 'Z')
+        self.assertRaises(ValueError, get_contiguous, nd, 255, 'A')
 
         # cmp_contig()
         nd = ndarray([1], shape=[1])
diff --git a/Modules/_testbuffer.c b/Modules/_testbuffer.c
--- a/Modules/_testbuffer.c
+++ b/Modules/_testbuffer.c
@@ -2362,6 +2362,13 @@
 
     ord = PyBytes_AS_STRING(ascii_order)[0];
     Py_DECREF(ascii_order);
+
+    if (ord != 'C' && ord != 'F' && ord != 'A') {
+        PyErr_SetString(PyExc_ValueError,
+            "invalid order, must be C, F or A");
+        return CHAR_MAX;
+    }
+
     return ord;
 }
 
@@ -2384,16 +2391,21 @@
             "buffertype must be PyBUF_READ or PyBUF_WRITE");
         return NULL;
     }
+
     type = PyLong_AsLong(buffertype);
     if (type == -1 && PyErr_Occurred()) {
         return NULL;
     }
-
-    ord = get_ascii_order(order);
-    if (ord == CHAR_MAX) {
+    if (type != PyBUF_READ && type != PyBUF_WRITE) {
+        PyErr_SetString(PyExc_ValueError,
+            "invalid buffer type");
         return NULL;
     }
 
+    ord = get_ascii_order(order);
+    if (ord == CHAR_MAX)
+        return NULL;
+
     return PyMemoryView_GetContiguous(obj, (int)type, ord);
 }
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list