python / python/cpython

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

未关闭 适合新手
#148,448 4 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

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. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 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 摘要。