]> granicus.if.org Git - python/commitdiff
Issue #21147: sqlite3 now raises an exception if the request contains a null
authorSerhiy Storchaka <storchaka@gmail.com>
Thu, 11 Sep 2014 10:29:05 +0000 (13:29 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Thu, 11 Sep 2014 10:29:05 +0000 (13:29 +0300)
character instead of truncate it.  Based on patch by Victor Stinner.

Lib/sqlite3/test/regression.py
Misc/NEWS
Modules/_sqlite/connection.c
Modules/_sqlite/statement.c

index c557ab6785d0c469ad2e1a84819f9c46aac9b19e..eaaaa2c528eb05234bad702a657d504a7f035139 100644 (file)
@@ -336,6 +336,16 @@ class RegressionTests(unittest.TestCase):
                           sqlite.connect, ":memory:", isolation_level=123)
 
 
+    def CheckNullCharacter(self):
+        # Issue #21147
+        con = sqlite.connect(":memory:")
+        self.assertRaises(ValueError, con, "\0select 1")
+        self.assertRaises(ValueError, con, "select 1\0")
+        cur = con.cursor()
+        self.assertRaises(ValueError, cur.execute, " \0select 2")
+        self.assertRaises(ValueError, cur.execute, "select 2\0")
+
+
 def suite():
     regression_suite = unittest.makeSuite(RegressionTests, "Check")
     return unittest.TestSuite((regression_suite,))
index dddcf76abac8bfa9adb517b326fc93929a3430e1..4e72496f8a4a5f17a71b89daf72345d2ed79f4a8 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -32,6 +32,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #21147: sqlite3 now raises an exception if the request contains a null
+  character instead of truncate it.  Based on patch by Victor Stinner.
+
 - Issue #21951: Fixed a crash in Tkinter on AIX when called Tcl command with
   empty string or tuple argument.
 
index 882424b0d90b120e9cd2ddbcaddb304fec8328c0..535464dbf1703d5b592b92b1637c6e883dc36815 100644 (file)
@@ -1261,7 +1261,8 @@ PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, Py
         if (rc == PYSQLITE_TOO_MUCH_SQL) {
             PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
         } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
-            PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string or unicode.");
+            if (PyErr_ExceptionMatches(PyExc_TypeError))
+                PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string.");
         } else {
             (void)pysqlite_statement_reset(statement);
             _pysqlite_seterror(self->db, NULL);
index 66b4a52565247f7567207d7d923caefce42ecaea..34babfd7503bfafce43dd351613f232b08d6f88a 100644 (file)
@@ -63,6 +63,10 @@ int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* con
         rc = PYSQLITE_SQL_WRONG_TYPE;
         return rc;
     }
+    if (strlen(sql_cstr) != (size_t)sql_cstr_len) {
+        PyErr_SetString(PyExc_ValueError, "the query contains a null character");
+        return PYSQLITE_SQL_WRONG_TYPE;
+    }
 
     self->in_weakreflist = NULL;
     Py_INCREF(sql);