GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-31746: Fixed Segfaults in the sqlite module when uninitialized. by lielfr · Pull Request #3946 · python/cpython

bpo-31746: Fixed Segfaults in the sqlite module when uninitialized. #3946

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.

Sign up for GitHub

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
lielfr wants to merge 9 commits into python:main from lielfr:fix-issue-31746

Uh oh!

There was an error while loading. Please reload this page.

Closed

bpo-31746: Fixed Segfaults in the sqlite module when uninitialized. #3946

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
9 commits
Select commit Hold shift + click to select a range
bbe3a0e
bpo-31746: Fixed Segfaults in the sqlite module when uninitialized.
lielfr Oct 10, 2017
b57a6af
Added regression tests, changed RuntimeError to ProgrammingError, and…
lielfr Oct 11, 2017
9955edf
Fixing typo, identation and tests according to reviews.
lielfr Oct 13, 2017
c0920c8
The first example code doesn't crash anymore.
lielfr Jun 3, 2019
77b2946
No more crashes from #31746
lielfr Jun 3, 2019
0fcd075
Fixed C whitespace error
lielfr Jun 3, 2019
d8b4dd6
Fixing the files according to the feedback in the PR
lielfr Apr 28, 2021
51e7a9f
Adding whitespace at the end of regression.py
lielfr Apr 28, 2021
84326a2
Fixing unnecessary extra whitespace in regression.py
lielfr Apr 28, 2021
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

27 changes: 27 additions & 0 deletions 27 Lib/sqlite3/test/regression.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,33 @@ def test_return_empty_bytestring(self):
val = cur.fetchone()[0]
self.assertEqual(val, b'')

def test_uninitialized_isolation_level(self):
"""
Previously trying to get the isolation level of an uninitialized Connection
caused a segfault, now it should just return None.
Comment thread

@orenmn orenmn Oct 11, 2017

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Other getters raise a ProgrammingError when they are called on an uninitialized Connection object.
IMHO this should be the same here.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
"""
conn = sqlite.Connection.__new__(sqlite.Connection)
with self.assertRaises(sqlite.ProgrammingError):
conn.isolation_level

def test_cursor_invalid_isolation_level(self):
"""
When trying to call conn.cursor() when conn is a Connection object that
was not initialized properly, it caused a segfault. Now it should raise
a ProgrammingError.
"""
conn = sqlite.Connection.__new__(sqlite.Connection)
self.assertRaises(ValueError, conn.__init__, '', isolation_level='invalid isolation level')
self.assertRaises(sqlite.ProgrammingError, conn.cursor)


def test_close_invalid_connection(self):
"""
Trying to call close() on a connection which was not initialized properly,
it caused a segfault. Now it should raise a ProgrammingError.
"""
conn = sqlite.Connection.__new__(sqlite.Connection)
self.assertRaises(sqlite.ProgrammingError, conn.close)

Comment thread
lielfr marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

def suite():
tests = [
Expand Down
31 changes: 31 additions & 0 deletions 31 Modules/_sqlite/connection.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,15 @@ pysqlite_connection_init(pysqlite_Connection *self, PyObject *args,
if (!isolation_level) {
isolation_level = PyUnicode_FromString("");
if (!isolation_level) {
PyErr_SetString(pysqlite_ProgrammingError, "Isolation level could not be set.");
return -1;
}
} else {
Py_INCREF(isolation_level);
}

self->initialized = 1;

Comment thread
lielfr marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

Py_CLEAR(self->isolation_level);
if (pysqlite_connection_set_isolation_level(self, isolation_level, NULL) < 0) {
Py_DECREF(isolation_level);
Expand Down Expand Up @@ -250,8 +254,19 @@ pysqlite_connection_dealloc(pysqlite_Connection *self)
*/
int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor)
{
if (!connection || !connection->cursors) {
PyErr_Format(pysqlite_ProgrammingError,
Comment thread
lielfr marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

"Base Connection.__init__ not called.");
goto error;
}
PyObject* weakref;

if (!connection->cursors) {
PyErr_SetString(pysqlite_ProgrammingError,
"Base Connection.__init__ not called.");
goto error;
}

weakref = PyWeakref_NewRef((PyObject*)cursor, NULL);
if (!weakref) {
goto error;
Expand Down Expand Up @@ -281,6 +296,11 @@ static PyObject *
pysqlite_connection_cursor_impl(pysqlite_Connection *self, PyObject *factory)
/*[clinic end generated code: output=562432a9e6af2aa1 input=4127345aa091b650]*/
{
if (self == NULL) {
return NULL;
}

static char *kwlist[] = {"factory", NULL};
Comment thread
Comment on lines +299 to +303

@erlend-aasland erlend-aasland Apr 30, 2021

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This is not needed if self->initialized is only set if pysqlite_connection_init was successful. Also, it looks like the rebase left some artifacts :)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
PyObject* cursor;

if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Expand Down Expand Up @@ -323,6 +343,12 @@ pysqlite_connection_close_impl(pysqlite_Connection *self)
/*[clinic end generated code: output=a546a0da212c9b97 input=3d58064bbffaa3d3]*/
{
int rc;

if (!self->statements) {
PyErr_SetString(pysqlite_ProgrammingError,
"Base Connection.__init__ not called.");
return NULL;
}
Comment thread
Comment on lines +346 to +351

@erlend-aasland erlend-aasland Apr 30, 2021

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Wouldn't it be better to handle this in pysqlite_do_all_statements, as it has multiple callers? Perhaps pysqlite_do_all_statements could be split in two: do_all_statements and do_all_cursors. Each of them would do a pointer sanity check before looping.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@lielfr lielfr May 2, 2021

Copy link
Copy Markdown
Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Are you sure about splitting pysqlite_do_all_statements into two functions? It seems like it always performs the statements part, but optionally also performs the cursors part.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@erlend-aasland erlend-aasland May 2, 2021

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

No, I'm not sure :) Let's leave it as it is; both pysqlite_do_all_statements and this particular change.

Nit: if (self->statements == NULL) is more explicit, and I believe more consistent with the rest of the code base.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

if (!pysqlite_check_thread(self)) {
return NULL;
Expand Down Expand Up @@ -1225,6 +1251,11 @@ int pysqlite_check_thread(pysqlite_Connection* self)

static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
{
if (!self || !self->isolation_level) {
PyErr_Format(pysqlite_ProgrammingError,
"Object is null or isolation_level is uninitialized.");
return 0;
}
Comment thread
Comment on lines +1254 to +1258

@erlend-aasland erlend-aasland Apr 30, 2021

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

If self->initialized is only set if pysqlite_connection_init was successful, this can be reduced to:

Suggested change
if (!self || !self->isolation_level) {
PyErr_Format(pysqlite_ProgrammingError,
"Object is null or isolation_level is uninitialized.");
return 0;
}
if (!pysqlite_check_connection(self)) {
return NULL;
}

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
return Py_NewRef(self->isolation_level);
}

Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.