xml.sax expatreader: entity stack not cleaned up after error in external_entity_ref()

オープン 初心者向け
#148,448 コメント 4 件 リアクション 0 件 担当者 0 名 GitHub で見る

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

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
74/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
静か
技術スタック
python
領域
backend

調査の方向性

まず、Lib/xml/sax/expatreader.py の ExpatParser.external_entity_ref() と、関連する gh-148427 / PR #148435 のコンテキストを読んでください。外部エンティティの解析中に例外が発生しても、self._parser、self._source、または _entity_stack が説明されている破損状態のまま残らなくなれば完了です。

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

説明

stdlib topic-XML type-bug

Follow-up from gh-148427 / PR #148435.

In ExpatParser.external_entity_ref() (Lib/xml/sax/expatreader.py), when an exception occurs during external entity parsing, the _entity_stack cleanup (restoring self._parser and self._source) is skipped because except Exception: return 0 exits the method before reaching lines 430–431:

try:
    xmlreader.IncrementalParser.parse(self, source)
except Exception:
    return 0  # exits here, cleanup below never runs

(self._parser, self._source) = self._entity_stack[-1]  # skipped
del self._entity_stack[-1]                               # skipped
return 1

After an error, self._parser still points to the sub-parser for the external entity, self._source points to the entity's input source, and the original parser/source pair is stuck in _entity_stack.

In practice this probably doesn't matter much since the parser is usually abandoned after a SAXParseException, but it's still a state corruption if someone tries to reuse the parser or inspect its state after catching the exception.

A finally clause would fix this, but that was intentionally kept out of gh-148427 to keep the scope minimal.

主要言語
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 を短くまとめたダイジェスト。