python / python/cpython

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

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

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

stdlib topic-XML type-bug
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

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.

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

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

はじめの一歩

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

調査の方向性

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

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

評価

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

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

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