python / python/cpython

sqlite3 Cursor NULL-deref via reentrant execute() inside a text_factory/converter during fetch

Open
#157,564 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

extension-modules topic-sqlite3 type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:
import sqlite3
con = sqlite3.connect(":memory:")
con.execute("CREATE TABLE t(a TEXT, b TEXT)")
con.execute("INSERT INTO t VALUES ('first', 'second')")
cur = con.cursor()
def btf(data):
    try:
        cur.execute("SELECT 1")            
    except sqlite3.ProgrammingError:
        pass                              
    return bytes(data).decode()
con.text_factory = btf
cur.execute("SELECT a, b FROM t")
print(cur.fetchone())                  
Modules/_sqlite/cursor.c:402:23: runtime error: member access within null pointer of type 'struct pysqlite_Statement'
    #0 _pysqlite_fetch_one_row Modules/_sqlite/cursor.c:402
    #1 pysqlite_cursor_iternext Modules/_sqlite/cursor.c:1155
    #2 pysqlite_cursor_fetchone_impl Modules/_sqlite/cursor.c:1206
    #3 pysqlite_cursor_fetchone Modules/_sqlite/clinic/cursor.c.h:169
CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs
  • gh-157578

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with Modules/_sqlite/cursor.c, especially _pysqlite_fetch_one_row around line 402, and reproduce the report under a sanitizer using the text_factory callback shown. Trace how the reentrant cur.execute() affects the statement during fetch; done means the reproducer no longer reaches the null dereference and appropriate coverage is added alongside the existing linked work.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, sqlite
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.