python / python/cpython

OrderedDict.pop() can segfault when key equality changes between lookups

Aperta
#154,817 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

interpreter-core 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?

Calling collections.OrderedDict.pop() without a default value can cause a segmentation fault if the lookup key has a stateful __eq__ implementation that returns different results across successive comparisons.

The crash occurs on the current main branch.

Minimal reproducer:

from collections import OrderedDict

class K:
    def __init__(self):
        self.calls = 0

    def __hash__(self):
        return 12345

    def __eq__(self, other):
        self.calls += 1
        return self.calls == 1

k1 = K()
k2 = K()

od = OrderedDict()
od[k1] = "value"

od.pop(k2)

Running this with a locally built CPython from main results in:

Segmentation fault (core dumped)

Expected behavior:

  • OrderedDict.pop() should not crash the interpreter.
  • It should either remove the matching item or raise KeyError if the key is not found.
CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

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

Python 3.16.0a0 (heads/fix-odict-pop-null-failobj:2ffab083782, Jul 28 2026, 12:31:11) [GCC 13.3.0]

Linked PRs
  • gh-154818

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 dal punto di ingresso OrderedDict.pop() ed esegui il riproduttore minimo su un branch main di CPython compilato localmente. Il lavoro è completato quando il riproduttore non causa più un segmentation fault e pop() rimuove l'elemento corrispondente oppure solleva KeyError quando non viene trovata alcuna chiave.

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

Valutazione

Stack tecnologico
python
Ambito
backend
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.