]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-44304: Fix crash in the sqlite3 module when the GC clears Statement objects ...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 5 Jun 2021 03:09:40 +0000 (20:09 -0700)
committerGitHub <noreply@github.com>
Sat, 5 Jun 2021 03:09:40 +0000 (20:09 -0700)
(cherry picked from commit fa106a685c1f199aca5be5c2d0277a14cc9057bd)

Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
Lib/sqlite3/test/userfunctions.py
Misc/NEWS.d/next/Core and Builtins/2021-06-05-02-34-57.bpo-44304._MAoPc.rst [new file with mode: 0644]
Modules/_sqlite/statement.c

index 6f57d1911b24d8cceeefe7d77b3f754f857bc0b8..429089072496edc4927e4ea56bf6eb23a38aa35e 100644 (file)
@@ -23,6 +23,7 @@
 
 import unittest
 import unittest.mock
+import gc
 import sqlite3 as sqlite
 
 def func_returntext():
@@ -322,6 +323,22 @@ class FunctionTests(unittest.TestCase):
         with self.assertRaises(TypeError):
             self.con.create_function("deterministic", 0, int, True)
 
+    def test_function_destructor_via_gc(self):
+        # See bpo-44304: The destructor of the user function can
+        # crash if is called without the GIL from the gc functions
+        dest = sqlite.connect(':memory:')
+        def md5sum(t):
+            return
+
+        dest.create_function("md5", 1, md5sum)
+        x = dest("create table lang (name, first_appeared)")
+        del md5sum, dest
+
+        y = [x]
+        y.append(y)
+
+        del x,y
+        gc.collect()
 
 class AggregateTests(unittest.TestCase):
     def setUp(self):
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-06-05-02-34-57.bpo-44304._MAoPc.rst b/Misc/NEWS.d/next/Core and Builtins/2021-06-05-02-34-57.bpo-44304._MAoPc.rst
new file mode 100644 (file)
index 0000000..89104e8
--- /dev/null
@@ -0,0 +1,2 @@
+Fix a crash in the :mod:`sqlite3` module that happened when the garbage
+collector clears :class:`sqlite.Statement` objects. Patch by Pablo Galindo
index e6dc4fd89528d22eb22984fad05166bd6e2f0376..cf7fba6ce9a24cdd9b644197a597dc5f569c2408 100644 (file)
@@ -397,6 +397,10 @@ stmt_dealloc(pysqlite_Statement *self)
     if (self->in_weakreflist != NULL) {
         PyObject_ClearWeakRefs((PyObject*)self);
     }
+    if (self->st) {
+        sqlite3_finalize(self->st);
+        self->st = 0;
+    }
     tp->tp_clear((PyObject *)self);
     tp->tp_free(self);
     Py_DECREF(tp);
@@ -405,13 +409,6 @@ stmt_dealloc(pysqlite_Statement *self)
 static int
 stmt_clear(pysqlite_Statement *self)
 {
-    if (self->st) {
-        Py_BEGIN_ALLOW_THREADS
-        sqlite3_finalize(self->st);
-        Py_END_ALLOW_THREADS
-        self->st = 0;
-    }
-
     Py_CLEAR(self->sql);
     return 0;
 }