]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #27897: Fixed possible crash in sqlite3.Connection.create_collation()
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 26 Sep 2016 21:10:03 +0000 (00:10 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Mon, 26 Sep 2016 21:10:03 +0000 (00:10 +0300)
if pass invalid string-like object as a name.  Patch by Xiang Zhang.

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

index cafff932b4da60f0e61e7eb1288a1391ce55480e..f8ef4d88f3785508819a3333cb72eb33ec35928e 100644 (file)
@@ -25,6 +25,11 @@ import unittest
 import sqlite3 as sqlite
 
 class CollationTests(unittest.TestCase):
+    def CheckCreateCollationNotString(self):
+        con = sqlite.connect(":memory:")
+        with self.assertRaises(TypeError):
+            con.create_collation(None, lambda x, y: (x > y) - (x < y))
+
     def CheckCreateCollationNotCallable(self):
         con = sqlite.connect(":memory:")
         with self.assertRaises(TypeError) as cm:
@@ -36,6 +41,23 @@ class CollationTests(unittest.TestCase):
         with self.assertRaises(sqlite.ProgrammingError):
             con.create_collation("collä", lambda x, y: (x > y) - (x < y))
 
+    def CheckCreateCollationBadUpper(self):
+        class BadUpperStr(str):
+            def upper(self):
+                return None
+        con = sqlite.connect(":memory:")
+        mycoll = lambda x, y: -((x > y) - (x < y))
+        con.create_collation(BadUpperStr("mycoll"), mycoll)
+        result = con.execute("""
+            select x from (
+            select 'a' as x
+            union
+            select 'b' as x
+            ) order by x collate mycoll
+            """).fetchall()
+        self.assertEqual(result[0][0], 'b')
+        self.assertEqual(result[1][0], 'a')
+
     @unittest.skipIf(sqlite.sqlite_version_info < (3, 2, 1),
                      'old SQLite versions crash on this test')
     def CheckCollationIsUsed(self):
index 84455c070a57116136e1465bf89dd7bf3da546d9..ddaf9475d26ab4d71d43fbd748c1dc3236545958 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -80,6 +80,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #27897: Fixed possible crash in sqlite3.Connection.create_collation()
+  if pass invalid string-like object as a name.  Patch by Xiang Zhang.
+
 - Issue #18893: Fix invalid exception handling in Lib/ctypes/macholib/dyld.py.
   Patch by Madison May.
 
index 3c52108c10ea4f4beb728b9c132166bc788fc26f..db979f521e72ee998433a59c8f102b92b8a215a6 100644 (file)
@@ -1523,11 +1523,13 @@ pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
         goto finally;
     }
 
-    if (!PyArg_ParseTuple(args, "O!O:create_collation(name, callback)", &PyUnicode_Type, &name, &callable)) {
+    if (!PyArg_ParseTuple(args, "UO:create_collation(name, callback)",
+                          &name, &callable)) {
         goto finally;
     }
 
-    uppercase_name = _PyObject_CallMethodId(name, &PyId_upper, "");
+    uppercase_name = _PyObject_CallMethodIdObjArgs((PyObject *)&PyUnicode_Type,
+                                                   &PyId_upper, name, NULL);
     if (!uppercase_name) {
         goto finally;
     }