python / python/cpython

plistlib: a <key> outside a <dict> raises IndexError instead of a proper error

未關閉
#152,959 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

stdlib type-feature
主要語言
Python
星號
77.2k
分支
36k
PR 合併指標
PR 指標待擷取

描述

Bug report

plistlib.loads on an XML plist whose <key> element appears outside a
<dict> raises an uncaught IndexError instead of a ValueError.

_PlistParser.end_key checks self.stack[-1] without first testing whether
the stack is empty:

def end_key(self):
    if self.current_key or not isinstance(self.stack[-1], dict):
        raise ValueError("unexpected key at line %d" %
                         self.parser.CurrentLineNumber)
    self.current_key = self.get_data()

When a <key> is the document's top-level element there is no enclosing
container on the stack, so self.stack[-1] raises IndexError.

Reproducer:

>>> import plistlib
>>> plistlib.loads(b"<plist><key>x</key></plist>")
Traceback (most recent call last):
  ...
IndexError: list index out of range

The sibling handler add_object (used by <integer>, <string>, etc.)
already guards the empty-stack case with elif not self.stack:, so scalar
elements at the top level are handled cleanly. A <key> outside a <dict>
should likewise raise a ValueError, consistent with the other malformed
inputs the parser rejects.

The binary plist format does not use this code path and is unaffected.

Versions

3.15 (main); the same code is present on 3.13 and 3.14.

Linked PRs
  • gh-152960
  • gh-152970

貢獻指南

開啟貢獻指南

從這裡開始

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

研究方向

從 plistlib 中的 _PlistParser.end_key 開始,將其堆疊處理與 add_object 進行比較。使用 plistlib.loads 重現頂層 情況,然後驗證格式錯誤的 XML plist 輸入會引發 ValueError 而不是 IndexError;相關 PR gh-152960 和 gh-152970 表明這項工作已經在進行中。

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

評估

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

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

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