python / python/cpython

sqlite3: NULL pointer dereference in window aggregate xValue/xInverse callbacks when the window frame is empty

Aperta
#153,800 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

extension-modules topic-sqlite3 type-crash
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Crash report

What happened?

This issue is a result review security advisories GHSA-g74h-5v93-4q3p.

Summary

Modules/_sqlite/connection.c in the sqlite3 standard library module
dereferences a NULL pointer when a user-defined aggregate window
function's value() (or inverse()) method is invoked by SQLite before
step() has ever been called for that window instance. This happens
whenever the window frame is empty for a given output row (e.g. the
first row of a partition with a frame such as
ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING). On a standard (non-debug)
build, this crashes the whole Python process with SIGSEGV.

The problem in the code was found by the Svace static analyzer (ISP RAS) and
was later confirmed by a real REPL crash.

Details

Tested on: CPython 3.12.10 (Fedora 40 system build, standard release
build) and CPython main @ https://github.com/python/cpython/commit/4c79929705eb31959a6f4071cc34583de0737a28
(2026-07-06). No special configuration is needed - a default build of
either version reproduces this.

import sqlite3

class A:
    def value(self):
        return 1

con = sqlite3.connect(":memory:")
con.create_window_function("f", 1, A)
con.execute("CREATE TABLE t(x)")
con.execute("INSERT INTO t VALUES (1)")
con.execute(
    "SELECT f(x) OVER (ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) FROM t"
).fetchall()

Result:

segmentation fault (core dumped)  python

No step, inverse, or finalize method needs to be defined on A -
the single row's window frame is unconditionally empty (there is no
row "1 preceding" the only row in the table), so value() is the first
and only callback SQLite invokes.

For a debug build (Py_DEBUG=1, configure --with-pydebug), the
assert(*cls != NULL) fires instead of segfaulting, printing:
Assertion '*cls != NULL' failed in value_callback.

Package: sqlite3 C API.

Affected versions: >= 3.11

CPython versions tested on:

CPython main branch, 3.11, 3.12, 3.13, 3.14, 3.15, 3.16

Operating systems tested on:

Linux

Output from running 'python -VV' on the command line:

No response

Linked PRs
  • gh-154576

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia da Modules/_sqlite/connection.c, in particolare dai percorsi value_callback e inverse_callback, ed esegui il riproduttore fornito per le funzioni finestra con una build standard e una di debug. Il lavoro è completato quando il caso di frame vuoto termina senza un SIGSEGV o un errore di asserzione; il PR collegato gh-154576 indica che il lavoro potrebbe essere già in corso.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python, sqlite
Ambito
databases
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.