[Python-checkins] cpython: sqlite: raise an OverflowError if a string or a BLOB is longer than INT_MAX

victor.stinner python-checkins at python.org
Mon Nov 18 01:56:51 CET 2013


http://hg.python.org/cpython/rev/40d25b2b93f0
changeset:   87234:40d25b2b93f0
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Mon Nov 18 01:36:29 2013 +0100
summary:
  sqlite: raise an OverflowError if a string or a BLOB is longer than INT_MAX
bytes

Fix compiler warnings on Windows 64-bit

files:
  Modules/_sqlite/statement.c |  24 ++++++++++++++++--------
  1 files changed, 16 insertions(+), 8 deletions(-)


diff --git a/Modules/_sqlite/statement.c b/Modules/_sqlite/statement.c
--- a/Modules/_sqlite/statement.c
+++ b/Modules/_sqlite/statement.c
@@ -132,18 +132,26 @@
             break;
         case TYPE_UNICODE:
             string = _PyUnicode_AsStringAndSize(parameter, &buflen);
-            if (string != NULL)
-                rc = sqlite3_bind_text(self->st, pos, string, buflen, SQLITE_TRANSIENT);
-            else
-                rc = -1;
+            if (string == NULL)
+                return -1;
+            if (buflen > INT_MAX) {
+                PyErr_SetString(PyExc_OverflowError,
+                                "string longer than INT_MAX bytes");
+                return -1;
+            }
+            rc = sqlite3_bind_text(self->st, pos, string, (int)buflen, SQLITE_TRANSIENT);
             break;
         case TYPE_BUFFER:
-            if (PyObject_AsCharBuffer(parameter, &buffer, &buflen) == 0) {
-                rc = sqlite3_bind_blob(self->st, pos, buffer, buflen, SQLITE_TRANSIENT);
-            } else {
+            if (PyObject_AsCharBuffer(parameter, &buffer, &buflen) != 0) {
                 PyErr_SetString(PyExc_ValueError, "could not convert BLOB to buffer");
-                rc = -1;
+                return -1;
             }
+            if (buflen > INT_MAX) {
+                PyErr_SetString(PyExc_OverflowError,
+                                "BLOB longer than INT_MAX bytes");
+                return -1;
+            }
+            rc = sqlite3_bind_blob(self->st, pos, buffer, buflen, SQLITE_TRANSIENT);
             break;
         case TYPE_UNKNOWN:
             rc = -1;

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


More information about the Python-checkins mailing list