Skip to content

bpo-45126: Ensure sqlite3.Connection is unusable if __init__ fails #29160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion Lib/sqlite3/test/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import weakref
import functools
from test import support

from test.support.os_helper import temp_dir
from .test_dbapi import managed_connect

class RegressionTests(unittest.TestCase):
Expand Down Expand Up @@ -486,6 +486,19 @@ def test_executescript_step_through_select(self):
con.executescript("select step(t) from t")
self.assertEqual(steps, values)

def test_connection_bad_reinit(self):
cx = sqlite.connect(":memory:")
with cx:
cx.execute("create table t(t)")
with temp_dir() as db:
self.assertRaisesRegex(sqlite.OperationalError,
"unable to open database file",
cx.__init__, db)
self.assertRaisesRegex(sqlite.ProgrammingError,
"Base Connection.__init__ not called",
cx.executemany, "insert into t values(?)",
((v,) for v in range(3)))


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Prevent segfault if :class:`sqlite3.Connection` reinitialisation fails.

.. warning::

:class:`sqlite3.Connection` is not adviced, as it may produce undesired
side-effects. Creating a new object is preferred to reinitialisation.
21 changes: 13 additions & 8 deletions Modules/_sqlite/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ pysqlite_connection_init_impl(pysqlite_Connection *self,
Py_INCREF(&PyUnicode_Type);
Py_XSETREF(self->text_factory, (PyObject*)&PyUnicode_Type);

self->db = NULL;
Py_BEGIN_ALLOW_THREADS
rc = sqlite3_open_v2(database, &self->db,
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
Expand All @@ -167,38 +168,38 @@ pysqlite_connection_init_impl(pysqlite_Connection *self,

if (rc != SQLITE_OK) {
_pysqlite_seterror(state, self->db);
return -1;
goto error;
}

if (!isolation_level) {
isolation_level = PyUnicode_FromString("");
if (!isolation_level) {
return -1;
goto error;
}
} else {
Py_INCREF(isolation_level);
}
Py_CLEAR(self->isolation_level);
if (pysqlite_connection_set_isolation_level(self, isolation_level, NULL) != 0) {
Py_DECREF(isolation_level);
return -1;
goto error;
}
Py_DECREF(isolation_level);

self->statement_cache = new_statement_cache(self, cached_statements);
if (self->statement_cache == NULL) {
return -1;
goto error;
}
if (PyErr_Occurred()) {
return -1;
goto error;
}

self->created_cursors = 0;

/* Create list of weak references to cursors */
self->cursors = PyList_New(0);
if (self->cursors == NULL) {
return -1;
goto error;
}

self->detect_types = detect_types;
Expand All @@ -222,12 +223,16 @@ pysqlite_connection_init_impl(pysqlite_Connection *self,
self->NotSupportedError = state->NotSupportedError;

if (PySys_Audit("sqlite3.connect/handle", "O", self) < 0) {
return -1;
goto error;
}

self->initialized = 1;

return 0;

error:
self->initialized = 0;
self->db = 0;
return -1;
}

static void
Expand Down