Skip to content
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

bpo-31746: Prevent segfaults with uninitialised sqlite3.Connection objects #27431

Merged
Merged
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
21 changes: 21 additions & 0 deletions Lib/sqlite3/test/dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,26 @@ def test_drop_unused_refs(self):
self.assertEqual(cu.fetchone()[0], n)


class UninitialisedConnectionTests(unittest.TestCase):
def setUp(self):
self.cx = sqlite.Connection.__new__(sqlite.Connection)

def test_uninit_operations(self):
funcs = (
lambda: self.cx.isolation_level,
lambda: self.cx.total_changes,
lambda: self.cx.in_transaction,
lambda: self.cx.iterdump(),
lambda: self.cx.cursor(),
lambda: self.cx.close(),
)
for func in funcs:
with self.subTest(func=func):
self.assertRaisesRegex(sqlite.ProgrammingError,
"Base Connection.__init__ not called",
func)


class OpenTests(unittest.TestCase):
_sql = "create table test(id integer)"

Expand Down Expand Up @@ -951,6 +971,7 @@ def suite():
ModuleTests,
SqliteOnConflictTests,
ThreadTests,
UninitialisedConnectionTests,
]
return unittest.TestSuite(
[unittest.TestLoader().loadTestsFromTestCase(t) for t in tests]
Expand Down
30 changes: 23 additions & 7 deletions Modules/_sqlite/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ pysqlite_connection_init_impl(pysqlite_Connection *self,

const char *database = PyBytes_AsString(database_obj);

self->initialized = 1;

self->begin_statement = NULL;

Py_CLEAR(self->statement_cache);
Expand Down Expand Up @@ -144,7 +142,7 @@ pysqlite_connection_init_impl(pysqlite_Connection *self,
Py_INCREF(isolation_level);
}
Py_CLEAR(self->isolation_level);
if (pysqlite_connection_set_isolation_level(self, isolation_level, NULL) < 0) {
if (pysqlite_connection_set_isolation_level(self, isolation_level, NULL) != 0) {
Py_DECREF(isolation_level);
return -1;
}
Expand Down Expand Up @@ -193,6 +191,8 @@ pysqlite_connection_init_impl(pysqlite_Connection *self,
return -1;
}

self->initialized = 1;

return 0;
}

Expand Down Expand Up @@ -370,6 +370,13 @@ pysqlite_connection_close_impl(pysqlite_Connection *self)
return NULL;
}

if (!self->initialized) {
pysqlite_state *state = pysqlite_get_state(NULL);
PyErr_SetString(state->ProgrammingError,
"Base Connection.__init__ not called.");
return NULL;
}
Comment on lines +373 to +378
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cannot use pysqlite_check_connection here, since it also checks self->db. Calling close() twice should still be a no-op.


pysqlite_do_all_statements(self, ACTION_FINALIZE, 1);
connection_close(self);

Expand Down Expand Up @@ -1257,6 +1264,9 @@ int pysqlite_check_thread(pysqlite_Connection* self)

static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
{
if (!pysqlite_check_connection(self)) {
return NULL;
}
return Py_NewRef(self->isolation_level);
}

Expand Down Expand Up @@ -1288,11 +1298,17 @@ pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* iso
return -1;
}
if (isolation_level == Py_None) {
PyObject *res = pysqlite_connection_commit(self, NULL);
if (!res) {
return -1;
/* We might get called during connection init, so we cannot use
* pysqlite_connection_commit() here. */
if (self->db && !sqlite3_get_autocommit(self->db)) {
int rc;
Py_BEGIN_ALLOW_THREADS
rc = sqlite3_exec(self->db, "COMMIT", NULL, NULL, NULL);
Py_END_ALLOW_THREADS
if (rc != SQLITE_OK) {
return _pysqlite_seterror(self->db);
}
}
Py_DECREF(res);

self->begin_statement = NULL;
} else {
Expand Down