[Python-checkins] bpo-41815: SQLite: segfault if backup called on closed database (GH-22322)

Miss Islington (bot) webhook-mailer at python.org
Mon Sep 21 18:05:26 EDT 2020


https://github.com/python/cpython/commit/4ee30c42441c3e5066d5080a22da53675aeae716
commit: 4ee30c42441c3e5066d5080a22da53675aeae716
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2020-09-21T15:05:17-07:00
summary:

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 bfee9fad84531a471fd7864e88947320669f68e2)

Co-authored-by: Peter McCormick <peter at pdmccormick.com>

files:
A Misc/NEWS.d/next/Library/2020-09-19-23-14-54.bpo-41815.RNpuX3.rst
M Lib/sqlite3/test/backup.py
M Modules/_sqlite/connection.c

diff --git a/Lib/sqlite3/test/backup.py b/Lib/sqlite3/test/backup.py
index 903bacf490301..ad1da9721527c 100644
--- a/Lib/sqlite3/test/backup.py
+++ b/Lib/sqlite3/test/backup.py
@@ -36,6 +36,13 @@ def test_bad_target_closed_connection(self):
         with self.assertRaises(sqlite.ProgrammingError):
             self.cx.backup(bck)
 
+    def test_bad_source_closed_connection(self):
+        bck = sqlite.connect(':memory:')
+        source = sqlite.connect(":memory:")
+        source.close()
+        with self.assertRaises(sqlite.ProgrammingError):
+            source.backup(bck)
+
     def test_bad_target_in_transaction(self):
         bck = sqlite.connect(':memory:')
         bck.execute('CREATE TABLE bar (key INTEGER)')
diff --git a/Misc/NEWS.d/next/Library/2020-09-19-23-14-54.bpo-41815.RNpuX3.rst b/Misc/NEWS.d/next/Library/2020-09-19-23-14-54.bpo-41815.RNpuX3.rst
new file mode 100644
index 0000000000000..3560db9bc5d35
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-09-19-23-14-54.bpo-41815.RNpuX3.rst
@@ -0,0 +1,2 @@
+Fix SQLite3 segfault when backing up closed database. Patch contributed by
+Peter David McCormick.
diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c
index 958be7d869794..b80037347f13b 100644
--- a/Modules/_sqlite/connection.c
+++ b/Modules/_sqlite/connection.c
@@ -1514,6 +1514,10 @@ pysqlite_connection_backup(pysqlite_Connection *self, PyObject *args, PyObject *
         sleep_ms = (int)ms;
     }
 
+    if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
+        return NULL;
+    }
+
     if (!pysqlite_check_connection((pysqlite_Connection *)target)) {
         return NULL;
     }



More information about the Python-checkins mailing list