Skip to content

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

Open

Description

@tonghuaroot

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

Activity

  1. picnixz commented on Jul 3, 2026

    @picnixz
    Member

    @Wojusensei Before asking to work on it, please check if there isn't already an open PR.

  2. added
    type-featureA feature request or enhancement
    stdlibStandard Library Python modules in the Lib/ directory
    on Jul 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions