Title: Null pointer dereference in `cursor.fetchone` after re-entrant `text_factory` closes connection · Issue #143662 · python/cpython · GitHub
Open Graph Title: Null pointer dereference in `cursor.fetchone` after re-entrant `text_factory` closes connection · Issue #143662 · python/cpython
X Title: Null pointer dereference in `cursor.fetchone` after re-entrant `text_factory` closes connection · Issue #143662 · python/cpython
Description: A user-provided text_factory runs while _pysqlite_fetch_one_row converts TEXT columns. If it calls Connection.close, connection->db is set to NULL, but pysqlite_cursor_iternext still calls sqlite3_changes(self->connection->db) once itera...
Open Graph Description: A user-provided text_factory runs while _pysqlite_fetch_one_row converts TEXT columns. If it calls Connection.close, connection->db is set to NULL, but pysqlite_cursor_iternext still calls sqlite3_...
X Description: A user-provided text_factory runs while _pysqlite_fetch_one_row converts TEXT columns. If it calls Connection.close, connection->db is set to NULL, but pysqlite_cursor_iternext still calls sqlit...
Opengraph URL: https://github.com/python/cpython/issues/143662
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Null pointer dereference in `cursor.fetchone` after re-entrant `text_factory` closes connection","articleBody":"A user-provided `text_factory` runs while `_pysqlite_fetch_one_row` converts TEXT columns. If it calls `Connection.close`, `connection->db` is set to `NULL`, but `pysqlite_cursor_iternext` still calls `sqlite3_changes(self->connection->db)` once iteration advances. The SQLite helper dereferences the cleared pointer and segfaults.\n\n**Proof of Concept:**\n\n```python\nimport sqlite3\n\nconn = sqlite3.connect(\":memory:\")\nconn.execute(\"create table t(x)\")\ncur = conn.cursor()\ncur.execute(\"insert into t values ('a') returning x\")\n\ndef text_factory(val, _conn=conn):\n if not getattr(text_factory, \"armed\", False):\n text_factory.armed = True\n _conn.close()\n return \"x\"\n\nconn.text_factory = text_factory\ncur.fetchone()\n```\n\n**Vulnerable Code Snippet:**\n\n\u003cdetails>\n\u003csummary>Click to expand\u003c/summary>\n\n```c\n/* Buggy Re-entrant Path */\nstatic PyObject *\npysqlite_cursor_fetchone_impl(pysqlite_Cursor *self)\n{\n return pysqlite_cursor_iternext((PyObject *)self);\n}\n\nstatic PyObject *\npysqlite_cursor_iternext(PyObject *op)\n{\n pysqlite_Cursor *self = _pysqlite_Cursor_CAST(op);\n /* ... */\n PyObject *row = _pysqlite_fetch_one_row(self);\n /* ... */\n if (rc == SQLITE_DONE) {\n if (self->statement->is_dml) {\n sqlite3 *db = self->connection->db; /* crashing pointer derived */\n self->rowcount = (long)sqlite3_changes(db); /* Crash site */\n }\n /* ... */\n }\n return row;\n}\n\nstatic PyObject *\n_pysqlite_fetch_one_row(pysqlite_Cursor* self)\n{\n /* ... */\n converted = PyObject_CallFunction(self->connection->text_factory,\n \"y#\", text, nbytes); /* Reentrant call site */\n /* ... */\n}\n\n/* Clobbering Path */\nstatic int\nconnection_close(pysqlite_Connection *self)\n{\n sqlite3 *db = self->db;\n self->db = NULL; /* state mutate site */\n (void)sqlite3_close_v2(db);\n /* ... */\n return rc;\n}\n```\n\u003c/details>\n\n**Sanitizer Output:**\n\n\u003cdetails>\n\u003csummary>Click to expand\u003c/summary>\n\n```\nAddressSanitizer:DEADLYSIGNAL\n=================================================================\n==431550==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000078 (pc 0x7fbfd133c1b4 bp 0x7ffed0387610 sp 0x7ffed0387608 T0)\n==431550==The signal is caused by a READ memory access.\n==431550==Hint: address points to the zero page.\n #0 0x7fbfd133c1b4 in sqlite3_changes64 (/lib/x86_64-linux-gnu/libsqlite3.so.0+0xa41b4) (BuildId: ac2bec9c45eec6feab0928b3b1373b467aa51339)\n #1 0x7fbfd133c1cc in sqlite3_changes (/lib/x86_64-linux-gnu/libsqlite3.so.0+0xa41cc) (BuildId: ac2bec9c45eec6feab0928b3b1373b467aa51339)\n #2 0x7fbfd3bdbb1c in pysqlite_cursor_iternext Modules/_sqlite/cursor.c:1121\n #3 0x7fbfd3bdbb1c in pysqlite_cursor_fetchone_impl Modules/_sqlite/cursor.c:1154\n #4 0x7fbfd3bdbb1c in pysqlite_cursor_fetchone Modules/_sqlite/clinic/cursor.c.h:169\n #5 0x6390ee26e3e7 in _PyObject_VectorcallTstate Include/internal/pycore_call.h:169\n #6 0x6390ee26e3e7 in PyObject_Vectorcall Objects/call.c:327\n #7 0x6390ee1225a2 in _PyEval_EvalFrameDefault Python/generated_cases.c.h:1620\n #8 0x6390ee5ecad6 in _PyEval_EvalFrame Include/internal/pycore_ceval.h:121\n #9 0x6390ee5ecad6 in _PyEval_Vector Python/ceval.c:2001\n #10 0x6390ee5ecad6 in PyEval_EvalCode Python/ceval.c:884\n #11 0x6390ee73216e in run_eval_code_obj Python/pythonrun.c:1365\n #12 0x6390ee73216e in run_mod Python/pythonrun.c:1459\n #13 0x6390ee736e17 in pyrun_file Python/pythonrun.c:1293\n #14 0x6390ee736e17 in _PyRun_SimpleFileObject Python/pythonrun.c:521\n #15 0x6390ee73793c in _PyRun_AnyFileObject Python/pythonrun.c:81\n #16 0x6390ee7aae3c in pymain_run_file_obj Modules/main.c:410\n #17 0x6390ee7aae3c in pymain_run_file Modules/main.c:429\n #18 0x6390ee7aae3c in pymain_run_python Modules/main.c:691\n #19 0x6390ee7ac71e in Py_RunMain Modules/main.c:772\n #20 0x6390ee7ac71e in pymain_main Modules/main.c:802\n #21 0x6390ee7ac71e in Py_BytesMain Modules/main.c:826\n #22 0x7fbfd442a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58\n #23 0x7fbfd442a28a in __libc_start_main_impl ../csu/libc-start.c:360\n #24 0x6390ee146634 in _start (/home/jackfromeast/Desktop/entropy/targets/grammar-afl++-latest/targets/cpython/python+0x206634) (BuildId: 4d105290d0ad566a4d6f4f7b2f05fbc9e317b533)\n\nAddressSanitizer can not provide additional info.\nSUMMARY: AddressSanitizer: SEGV (/lib/x86_64-linux-gnu/libsqlite3.so.0+0xa41b4) (BuildId: ac2bec9c45eec6feab0928b3b1373b467aa51339) in sqlite3_changes64\n==431550==ABORTING\n```\n\u003c/details>\n\n### Affected Versions:\n\n\u003cdetails>\n\n| Python Version | Status | Exit Code |\n|---|---|---|\n| `Python 3.9.24+ (heads/3.9:111bbc15b26, Oct 28 2025, 16:51:20) ` | OK | 0 |\n| `Python 3.10.19+ (heads/3.10:014261980b1, Oct 28 2025, 16:52:08) [Clang 18.1.3 (1ubuntu1)]` | OK | 0 |\n| `Python 3.11.14+ (heads/3.11:88f3f5b5f11, Oct 28 2025, 16:53:08) [Clang 18.1.3 (1ubuntu1)]` | ASAN | 1 |\n| `Python 3.12.12+ (heads/3.12:8cb2092bd8c, Oct 28 2025, 16:54:14) [Clang 18.1.3 (1ubuntu1)]` | ASAN | 1 |\n| `Python 3.13.9+ (heads/3.13:9c8eade20c6, Oct 28 2025, 16:55:18) [Clang 18.1.3 (1ubuntu1)]` | ASAN | 1 |\n| `Python 3.14.0+ (heads/3.14:2e216728038, Oct 28 2025, 16:56:16) [Clang 18.1.3 (1ubuntu1)]` | ASAN | 1 |\n| `Python 3.15.0a1+ (heads/main:f5394c257ce, Oct 28 2025, 19:29:54) [GCC 13.3.0]` | ASAN | 1 |\n\n\u003c/details>\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\nLinux\n\n### Output from running 'python -VV' on the command line:\n\nPython 3.15.0a1+ (heads/main:f5394c257ce, Oct 28 2025, 19:29:54) [GCC 13.3.0]","author":{"url":"https://github.com/jackfromeast","@type":"Person","name":"jackfromeast"},"datePublished":"2026-01-10T14:59:08Z","url":"https://github.com/python/cpython/issues/143662"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:9c9642f8-5ca8-8c51-9744-55a69711d2d6 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D2F8:382318:D4ECFD:10B2642:6B022F66 |
| html-safe-nonce | abca4b66681c204b3a2de1f0422675acfca588eb6248b9fd1b4800bf230ebc30 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMkY4OjM4MjMxODpENEVDRkQ6MTBCMjY0Mjo2QjAyMkY2NiIsInZpc2l0b3JfaWQiOiI4NzIxNzY1MzUwNzcwODgwMzU4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 0f9f89e24cea20fa93d447b0b80dd73bd39936a3d1ecd5bb966a8dd16343f355 |
| hovercard-subject-tag | issue:3799847200 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/143662/issue_layout |
| twitter:image | https://opengraph.githubassets.com/60e0c318deee3d90edbe56aa6e33c51645106d3f9a220761cc4baf9b49c954e9/python/cpython/issues/143662 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/60e0c318deee3d90edbe56aa6e33c51645106d3f9a220761cc4baf9b49c954e9/python/cpython/issues/143662 |
| og:image:alt | A user-provided text_factory runs while _pysqlite_fetch_one_row converts TEXT columns. If it calls Connection.close, connection->db is set to NULL, but pysqlite_cursor_iternext still calls sqlite3_... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jackfromeast |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width