Skip to content

Commit ca2d99d

Browse files
bpo-41815: SQLite: segfault if backup called on closed database (GH-22322)
GH- [bpo-41815](): SQLite: fix segfault if backup called on closed database Attempting to backup a closed database will trigger segfault: ```python import sqlite3 target = sqlite3.connect(':memory:') source = sqlite3.connect(':memory:') source.close() source.backup(target) ``` (cherry picked from commit bfee9fa) Co-authored-by: Peter McCormick <[email protected]>
1 parent 488e3eb commit ca2d99d

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

Lib/sqlite3/test/backup.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ def test_bad_target_closed_connection(self):
3636
with self.assertRaises(sqlite.ProgrammingError):
3737
self.cx.backup(bck)
3838

39+
def test_bad_source_closed_connection(self):
40+
bck = sqlite.connect(':memory:')
41+
source = sqlite.connect(":memory:")
42+
source.close()
43+
with self.assertRaises(sqlite.ProgrammingError):
44+
source.backup(bck)
45+
3946
def test_bad_target_in_transaction(self):
4047
bck = sqlite.connect(':memory:')
4148
bck.execute('CREATE TABLE bar (key INTEGER)')
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix SQLite3 segfault when backing up closed database. Patch contributed by
2+
Peter David McCormick.

Modules/_sqlite/connection.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,6 +1523,10 @@ pysqlite_connection_backup(pysqlite_Connection *self, PyObject *args, PyObject *
15231523
sleep_ms = (int)ms;
15241524
}
15251525

1526+
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1527+
return NULL;
1528+
}
1529+
15261530
if (!pysqlite_check_connection((pysqlite_Connection *)target)) {
15271531
return NULL;
15281532
}

0 commit comments

Comments
 (0)