python / python/cpython

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

未关闭
#154,817 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

interpreter-core type-crash
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 OrderedDict.pop() 入口点开始,在本地构建的 CPython main 分支上运行最小复现程序。当复现程序不再发生 segmentation fault,并且 pop() 要么删除匹配的项目,要么在找不到键时引发 KeyError 时,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
backend
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
描述清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。