Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 22805ca

Browse files
Issue #27897: Fixed possible crash in sqlite3.Connection.create_collation()
if pass invalid string-like object as a name. Patch by Xiang Zhang.
2 parents de10dbe + 407ac47 commit 22805ca

3 files changed

Lines changed: 29 additions & 2 deletions

File tree

Lib/sqlite3/test/hooks.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
import sqlite3 as sqlite
2626

2727
class CollationTests(unittest.TestCase):
28+
def CheckCreateCollationNotString(self):
29+
con = sqlite.connect(":memory:")
30+
with self.assertRaises(TypeError):
31+
con.create_collation(None, lambda x, y: (x > y) - (x < y))
32+
2833
def CheckCreateCollationNotCallable(self):
2934
con = sqlite.connect(":memory:")
3035
with self.assertRaises(TypeError) as cm:
@@ -36,6 +41,23 @@ def CheckCreateCollationNotAscii(self):
3641
with self.assertRaises(sqlite.ProgrammingError):
3742
con.create_collation("collä", lambda x, y: (x > y) - (x < y))
3843

44+
def CheckCreateCollationBadUpper(self):
45+
class BadUpperStr(str):
46+
def upper(self):
47+
return None
48+
con = sqlite.connect(":memory:")
49+
mycoll = lambda x, y: -((x > y) - (x < y))
50+
con.create_collation(BadUpperStr("mycoll"), mycoll)
51+
result = con.execute("""
52+
select x from (
53+
select 'a' as x
54+
union
55+
select 'b' as x
56+
) order by x collate mycoll
57+
""").fetchall()
58+
self.assertEqual(result[0][0], 'b')
59+
self.assertEqual(result[1][0], 'a')
60+
3961
@unittest.skipIf(sqlite.sqlite_version_info < (3, 2, 1),
4062
'old SQLite versions crash on this test')
4163
def CheckCollationIsUsed(self):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ Core and Builtins
4141
Library
4242
-------
4343

44+
- Issue #27897: Fixed possible crash in sqlite3.Connection.create_collation()
45+
if pass invalid string-like object as a name. Patch by Xiang Zhang.
46+
4447
- Issue #18893: Fix invalid exception handling in Lib/ctypes/macholib/dyld.py.
4548
Patch by Madison May.
4649

Modules/_sqlite/connection.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,11 +1498,13 @@ pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
14981498
goto finally;
14991499
}
15001500

1501-
if (!PyArg_ParseTuple(args, "O!O:create_collation(name, callback)", &PyUnicode_Type, &name, &callable)) {
1501+
if (!PyArg_ParseTuple(args, "UO:create_collation(name, callback)",
1502+
&name, &callable)) {
15021503
goto finally;
15031504
}
15041505

1505-
uppercase_name = _PyObject_CallMethodId(name, &PyId_upper, NULL);
1506+
uppercase_name = _PyObject_CallMethodIdObjArgs((PyObject *)&PyUnicode_Type,
1507+
&PyId_upper, name, NULL);
15061508
if (!uppercase_name) {
15071509
goto finally;
15081510
}

0 commit comments

Comments
 (0)