Assignment of a signed value 'str' to a bigger unsigned variable 'ucs4' at /Objects/unicodeobject.c

未关闭 适合新手
#161,429 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@divya-git03 已经在做这个了。

开始于 2026年11月5日。

评估

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

调研方向

阅读 Objects/unicodeobject.c 中的 PyUnicode_4BYTE_KIND 分支,并检查 _PyUnicodeWriter_WriteLatin1String 如何处理大于 0x7F 的字节。使用四字节 Unicode 缓冲区和 0xE9 这样的 Latin-1 字节编写一个有针对性的测试,确认符号扩展行为。结果码点仍在 Unicode 范围内且相关测试通过,即为完成。

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

描述

interpreter-core type-bug
Bug report

Potential Problem

In /Objects/unicodeobject.c in the PyUnicode_4BYTE_KIND branch https://github.com/python/cpython/blob/3.12/Objects/unicodeobject.c#L1922. A static analyzer has identified a genuine issue: *str is of type char (which is signed on x86/x64 architectures). Consequently, when Latin-1 bytes in the range 0x80–0xFF are cast to Py_UCS4, they undergo sign extension—for example, 0xE9 becomes 0xFFFFFFE9. This value exceeds MAX_UNICODE and violates the invariants of the str object. This code path can be triggered via the exported function _PyUnicodeWriter_WriteLatin1String when using a PyUnicode_4BYTE_KIND buffer (while not invoked internally by CPython itself, the issue can manifest in extensions). The fix is ​​to introduce an intermediate cast to Py_UCS1

    case PyUnicode_4BYTE_KIND: {
        Py_UCS4 *start = (Py_UCS4 *)data + index;
        Py_UCS4 *ucs4 = start;

        for (; str < end; ++ucs4, ++str)
            *ucs4 = (Py_UCS4)*str;

        assert((ucs4 - start) <= PyUnicode_GET_LENGTH(unicode));
        break;
    }
    default:
        Py_UNREACHABLE();
    }
}

Possible Solution

Add an intermediate cast to Py_UCS1:
*ucs4 = (Py_UCS4)(Py_UCS1)*str;

Found by Linux Verification Center (portal.linuxtesting.ru (https://portal.linuxtesting.ru/)) with SVACE.

Author D. Babushkin.

CPython versions tested on:

3.12

Operating systems tested on:

No response

主要语言
Python
星标
77.9k
派生
37.1k
平均合并
17 小时 19 分钟
30 天内合并 PR
679

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

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

python/cpython 的其他 Issue

查看 python/cpython 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

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