Skip to content

gh-149738: Fix segmentation fault bug in sqllite3#149754

Open
sepehr-rs wants to merge 12 commits into
python:mainfrom
sepehr-rs:fix-149738
Open

gh-149738: Fix segmentation fault bug in sqllite3#149754
sepehr-rs wants to merge 12 commits into
python:mainfrom
sepehr-rs:fix-149738

Conversation

@sepehr-rs
Copy link
Copy Markdown

@sepehr-rs sepehr-rs commented May 13, 2026

Fixes #149738.

>>> import sqlite3
>>> db = sqlite3.connect(":memory:")
>>> del db.row_factory
>>> db.execute("test")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    db.execute("test")
    ~~~~~~~~~~^^^^^^^^
sqlite3.OperationalError: near "test": syntax error
>>> 

Copy link
Copy Markdown
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

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

We need tests and check what happens on the created connection after deletion (check that there is still a row factory on the newly created cursor)

@sepehr-rs
Copy link
Copy Markdown
Author

sepehr-rs commented May 13, 2026

Thanks for your comment and review, @picnixz!
I’ve added the tests as requested and would appreciate any feedback.

P.S. The Read the Docs build is failing, but the logs indicate it’s due to an external issue unrelated to my changes.

Copy link
Copy Markdown
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

I would prefer using connection_getset instead of connection_members for row_factory and text_factory to raise an exception if the developer tries to delete the attribute (set it to NULL). Add get/set on these attributes.

@sepehr-rs
Copy link
Copy Markdown
Author

Hi @vstinner! Thank you for your kind review. Sorry for the delay, GitHub access has been unreliable here recently.
I’ve made the requested changes and would appreciate your feedback when you have a chance.

Comment thread Modules/_sqlite/connection.c
Comment thread Modules/_sqlite/connection.c Outdated
Comment thread Lib/test/test_sqlite3/test_factory.py
Comment thread Misc/NEWS.d/next/Core_and_Builtins/2026-05-13-06-54-41.gh-issue-149738.4BLFoH.rst Outdated
@vstinner vstinner added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels May 21, 2026
Copy link
Copy Markdown
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

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

Plesse correct undefined behaviors by changing the getset signatures. There should be no cast to getter/setter.

}

static int
connection_set_text_factory(pysqlite_Connection *self, PyObject *value, void *closure)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The signature is incorrect, it must be (PyObject*, PyObject*, void*) and you must cast self in the body.

@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 27, 2026

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@@ -0,0 +1,2 @@
:mod:`sqlite3`: Disallow removing ``row_factory`` and ``text_factory`` attributes
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Pleas update the docs of salite3.rst as well to indicate this change using a versionchanged directive.

}

static PyObject *
connection_get_row_factory(pysqlite_Connection *self, void *closure)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Those getsets must be using a critical section as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting changes needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

deleting sqlite3 row_factory causes segfault

3 participants