python / python/cpython

Crash in genericaliasobject.c when tuple_extend() fails under allocation failure

未关闭
#155,053 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

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

描述

Crash report

What happened?

While investigating allocation-failure paths in Objects/genericaliasobject.c, I found a NULL dereference in subs_tvars().

When tuple_extend() fails, it returns -1 after _PyTuple_Resize() fails. _PyTuple_Resize() clears its out-parameter (*pv = NULL) on failure, so subargs is guaranteed to be NULL.

However, subs_tvars() unconditionally calls:

if (j < 0) {
    Py_DECREF(subparams);
    Py_DECREF(subargs);
    return NULL;
}

As a result, Py_DECREF(subargs) dereferences a NULL pointer and crashes the interpreter instead of propagating the MemoryError.

Reproducer

A debug build with _testcapi is required.

import _testcapi
from typing import TypeVarTuple

Ts = TypeVarTuple("Ts")
alias = dict[str, tuple[*Ts]]
key = (int, str)

_testcapi.set_nomemory(24, 25)
try:
    alias[key]
finally:
    _testcapi.remove_mem_hooks()

The exact allocation index may vary between builds, so sweeping a range of indices is recommended.

Observed result

ASan reports:

AddressSanitizer: SEGV on unknown address 0x000000000000

#0 _Py_IsImmortal
#1 Py_DECREF
#2 subs_tvars (Objects/genericaliasobject.c)
#3 _Py_subs_parameters
#4 ga_getitem
Root cause

tuple_extend() immediately returns -1 when _PyTuple_Resize() fails. _PyTuple_Resize() sets its out-parameter to NULL on failure, so subargs is guaranteed to be NULL when control reaches the error path. Calling Py_DECREF(subargs) therefore dereferences a NULL pointer.

The issue appears related to gh-148222, which removed the same Py_DECREF() pattern from _Py_make_parameters() after _PyTuple_Resize() failure, but this analogous path in subs_tvars() remained unchanged.

Removing Py_DECREF(subargs); causes the reproducer to raise MemoryError instead of crashing.

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/investigate-genericalias-oom-null-decref-dirty:7ce7f0bd851, Aug 1 2026) [GCC 13.3.0]

Linked PRs
  • gh-155055

贡献指南

打开贡献指南

从这里开始

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

调研方向

从 Objects/genericaliasobject.c 中的 subs_tvars() 和 tuple_extend() 开始,然后在调试构建上运行提供的 _testcapi 分配失败复现程序。确认失败路径不再崩溃并传播 MemoryError;比较 _Py_make_parameters() 中的类似处理,并检查相关的 gh-155055 工作。

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

评估

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

把新 issue 发到你的邮箱

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