Iterating over ctypes pointer causes hang or segmentation fault

オープン
#92,347 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
25/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
python
領域
backend

調査の方向性

2つの再現例で示されている ctypes PyCPointer の動作から始め、影響を受ける Python バージョンでハング、メモリ増加、セグメンテーションフォールトが発生することを確認してください。ポインターのインデックス指定と反復処理がどのように相互作用するかを追跡し、そのうえで、完成した動作がハングやクラッシュを起こさずに反復処理を安全に拒否することを確認してください。リンクされている PR gh-126318 と gh-157801 に調査対象となる既存の作業があります。

索引モデルが issue の本文から書いたものです。

説明

topic-ctypes type-crash

Trying to create iterable (list, tuple, set etc.) from PyCPointer instance leads to hang or segmentation fault.
This code causes hang and memory leak which goes until oom-killer kills python process:

import ctypes
x = ctypes.c_int32(12345)
buf4_p_t = ctypes.POINTER(ctypes.ARRAY(ctypes.c_byte, 4))
buf_p = ctypes.cast(ctypes.byref(x), buf4_p_t)
list(buf_p)

Similar code that causes segmentation fault:

import ctypes
x = ctypes.c_int32(54321)
xp = ctypes.pointer(x)
list(xp)

Iterating over pointer using for statement leads to an infinite loop:
for i in buf_p: print(i)
for i in xp: print(i)
list(for i in buf_p)

Tried on 3.10.3 and 3.11.0a6+, fails at both.

The possible reason is that PyCPointer_Type defines sq_item without defining tp_iter.
Despite I can't imagine any reason why anyone might need to use pointer as iterable/sequence, raising TypeError in such situation would be a better option.

Linked PRs
  • gh-126318
  • gh-157801
主要言語
Python
スター
77.2k
フォーク
36k
平均マージ
1日 9時間
マージ済み PR(30日)
558

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

python/cpython のほかの issue

python/cpython の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。