python / python/cpython

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

Aberta
#152,959 1 comentário 0 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

stdlib type-feature
Linguagem predominante
Python
Estrelas
77.2k
Forks
36k
Métricas de merge de PRs
Métricas de PR pendentes

Descrição

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

Guia de contribuição

Abrir o guia de contribuição

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Direção de pesquisa

Comece em plistlib, em _PlistParser.end_key, e compare o tratamento da pilha com add_object. Reproduza o caso de no nível superior com plistlib.loads e, em seguida, verifique se uma entrada plist XML malformada gera ValueError em vez de IndexError; os PRs vinculados gh-152960 e gh-152970 indicam que o trabalho já está em andamento.

Escrita pelo modelo de indexação a partir do texto da issue.

Avaliação

Stack de tecnologia
python
Domínio
backend
Tipo de issue
Bug
Dificuldade
2/5
Tempo estimado
1-3 horas
Status de atividade
Estagnada
Clareza
Claramente especificada
Facilidade para iniciantes
35/100

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.