python / python/cpython

Pickle `load_build` function checks if `slotstate` is False, not a dict

未关闭
#144,411 4 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

extension-modules type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Bug report

Bug description:

This issue is similar to Issue https://github.com/python/cpython/issues/128965. Inside of the load_build() function, Python pickle assumes that slotstate is a dictionary while C _pickle explicitly checks to ensure it's a dictionary. The behavior of these two implementations diverge when slotstate is falsey but not a dictionary.

payload:      b'NN\x8f\x86b.'

pickle:       None
_pickle.c:    FAILURE slot state is not a dictionary
pickletools:
    0: N    NONE
    1: N    NONE
    2: \x8f EMPTY_SET
    3: \x86 TUPLE2
    4: b    BUILD
    5: .    STOP
highest protocol among opcodes = 4

In this case, opcode 2 (EMPTY_SET) is slotstate. When running in pickle.py, this is falsey and thus the code inside setting attributes will not be run, causing the pickle to continue deserializing without any errors.

https://github.com/python/cpython/blob/29acc08c8dad664cd5713cb392e5beba65724c10/Lib/pickle.py#L1868

When running in _pickle.c, this value is not a dictionary, causing an error to be thrown.

https://github.com/python/cpython/blob/29acc08c8dad664cd5713cb392e5beba65724c10/Modules/_pickle.c#L6903

I guess also in general I'm not understanding what the if slotstate in pickle.py is meant to do anyway. If it's supposed to check if slotstate == None, then the C _pickle module doesn't do that correctly. It checks if slotstate is NULL, which is not the same as PyNone.

https://github.com/python/cpython/blob/29acc08c8dad664cd5713cb392e5beba65724c10/Modules/_pickle.c#L6899

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs
  • gh-144582

贡献指南

打开贡献指南

从这里开始

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

调研方向

比较 Lib/pickle.py 中 load_build 的实现与 Modules/_pickle.c,使用 payload b'NN\x8f\x86b.' 重现存在差异的行为。审查链接的 PR gh-144582,并确认两个实现都一致地拒绝非 dictionary 的 falsy slotstate;如果该 PR 指出了相关测试,则确认相关的 pickle 行为已由测试覆盖。

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

评估

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

把新 issue 发到你的邮箱

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