python / python/cpython

bytearray.resize() must not clear the object on error (memory allocation failure)

未關閉
#157,242 2 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

interpreter-core type-bug
主要語言
Python
星號
77.2k
分支
35.9k
PR 合併指標
PR 指標待擷取

描述

Python 3.15 reimplemented bytearray using an internal bytes object (to implement the bytearray.take_bytes() method). _PyBytes_Resize() is used internally to resize the bytearray object. Problem: if _PyBytes_Resize() fails, the bytearray object is cleared.

On Python 3.14, the bytearray is not cleared on resize failure.

The following example works on Python 3.14 but fails on Python 3.15.

import _testcapi

data = b'some data'
ba = bytearray(data)
try:
    try:
        _testcapi.set_nomemory(0)
        ba.resize(1024)
    except MemoryError:
        pass
    else:
        raise AssertionError("expect MemoryError")
finally:
    _testcapi.remove_mem_hooks()
if ba != data:
    raise AssertionError(f"{ba} != {data}")
Linked PRs
  • gh-157243
  • gh-157340
  • gh-157344
  • gh-157350
  • gh-157351
  • gh-157352
  • gh-157371
  • gh-157375
  • gh-157431
  • gh-157619
  • gh-157723
  • gh-157811

貢獻指南

開啟貢獻指南

從這裡開始

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

研究方向

從 bytearray.resize() 路徑及其對 _PyBytes_Resize() 的使用開始,然後重現所提供的 _testcapi.set_nomemory() 範例。完成標準是:resize 失敗時引發 MemoryError,同時保留 bytearray 的內容,並有一個涵蓋該行為的回歸測試。

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

評估

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

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

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