Skip to content

Commit bfee9fa

Browse files
authored
bpo-41815: SQLite: segfault if backup called on closed database (GH-22322)
# [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) ```
1 parent c8c70e7 commit bfee9fa

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
@@ -35,6 +35,13 @@ def test_bad_target_closed_connection(self):
3535
with self.assertRaises(sqlite.ProgrammingError):
3636
self.cx.backup(bck)
3737

38+
def test_bad_source_closed_connection(self):
39+
bck = sqlite.connect(':memory:')
40+
source = sqlite.connect(":memory:")
41+
source.close()
42+
with self.assertRaises(sqlite.ProgrammingError):
43+
source.backup(bck)
44+
3845
def test_bad_target_in_transaction(self):
3946
bck = sqlite.connect(':memory:')
4047
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
@@ -1514,6 +1514,10 @@ pysqlite_connection_backup(pysqlite_Connection *self, PyObject *args, PyObject *
15141514
sleep_ms = (int)ms;
15151515
}
15161516

1517+
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1518+
return NULL;
1519+
}
1520+
15171521
if (!pysqlite_check_connection((pysqlite_Connection *)target)) {
15181522
return NULL;
15191523
}

0 commit comments

Comments
 (0)