Commit 9f894f4
gh-152107: Fix crash creating a dict item iterator under MemoryError
dictiter_new() for item iterators allocates di_result via
_PyTuple_FromPairSteal() before calling _PyObject_GC_TRACK(di). If that
allocation fails (OOM), the error path runs Py_DECREF(di) on the
not-yet-tracked iterator and dictiter_dealloc()'s _PyObject_GC_UNTRACK(di)
asserts the object is GC-tracked -- aborting on debug builds and corrupting
the GC list (later segfault) on release builds.
An iterator with di_result == NULL is already in a valid state, so track it
right after initialization, before the only fallible allocation. The OOM
error path then deallocates a properly tracked object.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>1 parent a46db4f commit 9f894f4
3 files changed
Lines changed: 33 additions & 5 deletions
File tree
- Lib/test
- Misc/NEWS.d/next/Core_and_Builtins
- Objects
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
| 12 | + | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| |||
116 | 116 | | |
117 | 117 | | |
118 | 118 | | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
119 | 143 | | |
120 | 144 | | |
121 | 145 | | |
| |||
Lines changed: 3 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5520 | 5520 | | |
5521 | 5521 | | |
5522 | 5522 | | |
| 5523 | + | |
| 5524 | + | |
| 5525 | + | |
| 5526 | + | |
| 5527 | + | |
5523 | 5528 | | |
5524 | 5529 | | |
5525 | 5530 | | |
| |||
5528 | 5533 | | |
5529 | 5534 | | |
5530 | 5535 | | |
5531 | | - | |
5532 | | - | |
5533 | | - | |
5534 | | - | |
5535 | 5536 | | |
5536 | 5537 | | |
5537 | 5538 | | |
| |||
0 commit comments