[Python-checkins] r80349 - in python/branches/py3k: Lib/sqlite3/test/regression.py Misc/NEWS Modules/_sqlite/connection.c

victor.stinner python-checkins at python.org
Thu Apr 22 13:23:23 CEST 2010


Author: victor.stinner
Date: Thu Apr 22 13:23:23 2010
New Revision: 80349

Log:
Issue #8195: Fix a crash in sqlite Connection.create_collation() if the
collation name contains a surrogate character.


Modified:
   python/branches/py3k/Lib/sqlite3/test/regression.py
   python/branches/py3k/Misc/NEWS
   python/branches/py3k/Modules/_sqlite/connection.c

Modified: python/branches/py3k/Lib/sqlite3/test/regression.py
==============================================================================
--- python/branches/py3k/Lib/sqlite3/test/regression.py	(original)
+++ python/branches/py3k/Lib/sqlite3/test/regression.py	Thu Apr 22 13:23:23 2010
@@ -274,6 +274,13 @@
         """
         self.assertRaises(sqlite.Warning, self.con, 1)
 
+    def CheckCollation(self):
+        def collation_cb(a, b):
+            return 1
+        self.assertRaises(sqlite.ProgrammingError, self.con.create_collation,
+            # Lone surrogate cannot be encoded to the default encoding (utf8)
+            "\uDC80", collation_cb)
+
 def suite():
     regression_suite = unittest.makeSuite(RegressionTests, "Check")
     return unittest.TestSuite((regression_suite,))

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Thu Apr 22 13:23:23 2010
@@ -323,6 +323,9 @@
 Library
 -------
 
+- Issue #8195: Fix a crash in sqlite Connection.create_collation() if the
+  collation name contains a surrogate character.
+
 - Issue #8484: Load all ciphers and digest algorithms when initializing
   the _ssl extension, such that verification of some SSL certificates
   doesn't fail because of an "unknown algorithm".

Modified: python/branches/py3k/Modules/_sqlite/connection.c
==============================================================================
--- python/branches/py3k/Modules/_sqlite/connection.c	(original)
+++ python/branches/py3k/Modules/_sqlite/connection.c	Thu Apr 22 13:23:23 2010
@@ -1374,7 +1374,9 @@
     PyObject* uppercase_name = 0;
     PyObject* name;
     PyObject* retval;
-    char* chk;
+    Py_UNICODE* chk;
+    Py_ssize_t i, len;
+    char *uppercase_name_str;
     int rc;
 
     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
@@ -1390,19 +1392,24 @@
         goto finally;
     }
 
-    chk = _PyUnicode_AsString(uppercase_name);
-    while (*chk) {
+    len = PyUnicode_GET_SIZE(uppercase_name);
+    chk = PyUnicode_AS_UNICODE(uppercase_name);
+    for (i=0; i<len; i++, chk++) {
         if ((*chk >= '0' && *chk <= '9')
          || (*chk >= 'A' && *chk <= 'Z')
          || (*chk == '_'))
         {
-            chk++;
+            continue;
         } else {
             PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
             goto finally;
         }
     }
 
+    uppercase_name_str = _PyUnicode_AsString(uppercase_name);
+    if (!uppercase_name_str)
+        goto finally;
+
     if (callable != Py_None && !PyCallable_Check(callable)) {
         PyErr_SetString(PyExc_TypeError, "parameter must be callable");
         goto finally;
@@ -1417,7 +1424,7 @@
     }
 
     rc = sqlite3_create_collation(self->db,
-                                  _PyUnicode_AsString(uppercase_name),
+                                  uppercase_name_str,
                                   SQLITE_UTF8,
                                   (callable != Py_None) ? callable : NULL,
                                   (callable != Py_None) ? pysqlite_collation_callback : NULL);


More information about the Python-checkins mailing list