Kaydet (Commit) e56ab746 authored tarafından Oren Milman's avatar Oren Milman Kaydeden (comit) Victor Stinner

bpo-31770: Prevent a crash and refleaks when calling sqlite3.Cursor.__init__()…

bpo-31770: Prevent a crash and refleaks when calling sqlite3.Cursor.__init__() more than once (#3968)
üst ad455cd9
...@@ -24,6 +24,8 @@ ...@@ -24,6 +24,8 @@
import datetime import datetime
import unittest import unittest
import sqlite3 as sqlite import sqlite3 as sqlite
import weakref
from test import support
class RegressionTests(unittest.TestCase): class RegressionTests(unittest.TestCase):
def setUp(self): def setUp(self):
...@@ -376,6 +378,22 @@ class RegressionTests(unittest.TestCase): ...@@ -376,6 +378,22 @@ class RegressionTests(unittest.TestCase):
counter += 1 counter += 1
self.assertEqual(counter, 3, "should have returned exactly three rows") self.assertEqual(counter, 3, "should have returned exactly three rows")
def CheckBpo31770(self):
"""
The interpreter shouldn't crash in case Cursor.__init__() is called
more than once.
"""
def callback(*args):
pass
con = sqlite.connect(":memory:")
cur = sqlite.Cursor(con)
ref = weakref.ref(cur, callback)
cur.__init__(con)
del cur
# The interpreter shouldn't crash when ref is collected.
del ref
support.gc_collect()
def suite(): def suite():
regression_suite = unittest.makeSuite(RegressionTests, "Check") regression_suite = unittest.makeSuite(RegressionTests, "Check")
......
Prevent a crash when calling the ``__init__()`` method of a
``sqlite3.Cursor`` object more than once. Patch by Oren Milman.
...@@ -39,21 +39,20 @@ static int pysqlite_cursor_init(pysqlite_Cursor* self, PyObject* args, PyObject* ...@@ -39,21 +39,20 @@ static int pysqlite_cursor_init(pysqlite_Cursor* self, PyObject* args, PyObject*
} }
Py_INCREF(connection); Py_INCREF(connection);
self->connection = connection; Py_XSETREF(self->connection, connection);
self->statement = NULL; Py_CLEAR(self->statement);
self->next_row = NULL; Py_CLEAR(self->next_row);
self->in_weakreflist = NULL;
self->row_cast_map = PyList_New(0); Py_XSETREF(self->row_cast_map, PyList_New(0));
if (!self->row_cast_map) { if (!self->row_cast_map) {
return -1; return -1;
} }
Py_INCREF(Py_None); Py_INCREF(Py_None);
self->description = Py_None; Py_XSETREF(self->description, Py_None);
Py_INCREF(Py_None); Py_INCREF(Py_None);
self->lastrowid= Py_None; Py_XSETREF(self->lastrowid, Py_None);
self->arraysize = 1; self->arraysize = 1;
self->closed = 0; self->closed = 0;
...@@ -62,7 +61,7 @@ static int pysqlite_cursor_init(pysqlite_Cursor* self, PyObject* args, PyObject* ...@@ -62,7 +61,7 @@ static int pysqlite_cursor_init(pysqlite_Cursor* self, PyObject* args, PyObject*
self->rowcount = -1L; self->rowcount = -1L;
Py_INCREF(Py_None); Py_INCREF(Py_None);
self->row_factory = Py_None; Py_XSETREF(self->row_factory, Py_None);
if (!pysqlite_check_thread(self->connection)) { if (!pysqlite_check_thread(self->connection)) {
return -1; return -1;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment