decimal: crash when a re-entrant __bool__ deallocates the Context during Context.flags assignment or comparison
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 77.2k
- Forks
- 35.9k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug report
Context.flags (and traps) is a SignalDict that borrows a pointer to
flags owned by its Context. signaldict_setitem and
signaldict_richcompare read that pointer after calling back into Python
(PyObject_IsTrue, i.e. the value's __bool__), which can deallocate the
Context. gh-146011 cleared the borrowed pointer on Context teardown and
guarded signaldict_repr, but these two methods were left unguarded, so the
same teardown leaves them dereferencing a NULL pointer and crashing.
Assignment:
import decimal, gc
ctx = decimal.Context()
flags = ctx.flags
class Evil:
def __bool__(self):
global ctx; del ctx; gc.collect()
return True
flags[decimal.InvalidOperation] = Evil() # segfault
Comparison:
import decimal, gc
ctx = decimal.Context()
other = ctx.flags.copy()
class Evil:
def __bool__(self):
global ctx; del ctx; gc.collect()
return True
other[decimal.InvalidOperation] = Evil()
ctx.flags == other # segfault
Both crash with SIGSEGV on current main; the affected code exists on 3.13+.
Linked PRs
- gh-155494
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit den Einstiegspunkten des decimal-Moduls signaldict_setitem und signaldict_richcompare und reproduziere anschließend die Zuweisungs- und Vergleichsbeispiele aus dem Issue unter Python 3.13+. Erledigt ist die Arbeit, wenn beide re-entrant-bool-Fälle nach dem Abbau von Context nicht mehr abstürzen; beachte, dass der verknüpfte PR gh-155494 diese Arbeit bereits abdeckt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- backend
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 25/100