python / python/cpython

readline.c: Missing NULL check on PyLong_FromLong

未關閉
#154,385 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

extension-modules type-crash
主要語言
Python
星號
77.2k
分支
35.9k
PR 合併指標
PR 指標待擷取

描述

Bug report

Bug description:

Original gist: https://gist.github.com/devdanzin/60aed55f44fba133ee2d34d46aa8d197

readline.c: Missing NULL check on PyLong_FromLong in setup_readline

Summary

PyLong_FromLong(0L) at lines 1418-1419 stored in begidx/endidx without NULL check. On OOM during module init, readline.get_begidx() does Py_NewRef(NULL) → segfault.

Reproducer

import _testcapi, sys
if "readline" in sys.modules: del sys.modules["readline"]
for n in range(1, 80):
    if "readline" in sys.modules: del sys.modules["readline"]
    _testcapi.set_nomemory(n, 0)
    try:
        import readline
        _testcapi.remove_mem_hooks()
        break
    except MemoryError:
        _testcapi.remove_mem_hooks()
    except ImportError:
        _testcapi.remove_mem_hooks()
    except:
        _testcapi.remove_mem_hooks()
# Assertion: obj != NULL in PyStackRef_FromPyObjectSteal

The original gist said we should check the result of PyLong_FromLong(0L), but I think this is a false positive, because 0 is small int and should not failed while creating a Python int object from it.

However there are other line of code in this file for calling PyLong_FromLong with begidx/endidx withoud check the result:

https://github.com/python/cpython/blob/d333e5aa59f6cc20198502f4ff50b14eabde9b8b/Modules/readline.c#L1330-L1331

I guess the LLM found a real bug but reported it at the wrong place. Either way, we should fix it.

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs
  • gh-154386

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

閱讀 Modules/readline.c 中 setup_readline 附近的程式碼,以及 1330-1331 行附近的 PyLong_FromLong 呼叫;先執行提供的 _testcapi memory-hook reproducer,並檢查關聯的 PR gh-154386。完成的標準是,受影響的 out-of-memory 路徑不再允許 NULL 物件到達 readline accessors,且相關的 CPython 測試通過。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
c, python
領域
cli
Issue 類型
缺陷
難度
3/5
預估耗時
1-2 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
25/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。