]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-41815: SQLite: segfault if backup called on closed database (GH-22322)
authorPeter McCormick <peter@pdmccormick.com>
Sun, 20 Sep 2020 03:40:46 +0000 (23:40 -0400)
committerGitHub <noreply@github.com>
Sun, 20 Sep 2020 03:40:46 +0000 (20:40 -0700)
# [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)
```

Lib/sqlite3/test/backup.py
Misc/NEWS.d/next/Library/2020-09-19-23-14-54.bpo-41815.RNpuX3.rst [new file with mode: 0644]
Modules/_sqlite/connection.c

index 2752a4db337ddd88a8af1103f1b1f777455d8ba7..3637c4bb21b6db61982fb770a0667ab7156daab6 100644 (file)
@@ -35,6 +35,13 @@ class BackupTests(unittest.TestCase):
         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 (file)
index 0000000..3560db9
--- /dev/null
@@ -0,0 +1,2 @@
+Fix SQLite3 segfault when backing up closed database. Patch contributed by
+Peter David McCormick.
index f765ba1df246694b9dbcea2cf0827b3dbf85aa64..81fc1335371a6fcbba577e50c557204f2160cac9 100644 (file)
@@ -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;
     }