readline.c: Missing NULL check on PyLong_FromLong
還沒有人認領這個 Issue。
- 主要語言
- 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 inbegidx/endidxwithout NULL check. On OOM during module init,readline.get_begidx()doesPy_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:
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
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 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