python / python/cpython

3.13.2: cast in Py_XDECREF causes runtime failures with immortal objects

未关闭
#135,746 17 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

interpreter-core OS-windows topic-C-API type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Bug report

Bug description:

In Python 3.13.2 I use

Py_XDECREF(PyObject *op) 

It is called at the end of a function-call to load an image (cv2.imread) which fails, resulting in a None value (not NULL!). For Python 3.9 Py_XDECREF works just fine, without crash. For Python 3.13.2 the program, compiled with VS2022 (64 Bit), raises a "Run-Time Check Failure #1 - A cast to a smaller data type has caused a loss of data." and my application crashes ("/RTC1 = Basic Runtime Checks enabled").

I followed up the calls:

    Py_DECREF
        _Py_IsImmortal
            #elif SIZEOF_VOID_P > 4
                _Py_CAST(PY_INT32_T, op->ob_refcnt)     <-- crash

The value is

op->ob_refcnt == 0x0000000100000000

To fix this problem, I propose, that the type cast to 32 bit should be changed to

_Py_CAST(PY_INT32_T, op->ob_refcnt & 0xFFFFFFFF)
CPython versions tested on:

3.13

Operating systems tested on:

Windows

贡献指南

打开贡献指南

从这里开始

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

调研方向

从 Py_XDECREF/Py_DECREF 和 _Py_IsImmortal 的调用路径开始,使用报告中的 immortal reference count 重现 Windows VS2022 /RTC1 失败。将 cast 行为与提议的 masking 进行比较,然后使用相关的 CPython 测试验证该失败已得到解决;该 issue 未指定具体的源文件或测试。

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

评估

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

把新 issue 发到你的邮箱

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