Psych.dump_stream raises NoMethodError when the stream has no documents

Aberta Para iniciantes
#839 0 comentários 0 reações 0 responsáveis Ver no GitHub

Mantenedores costumam responder em até 1 dia

Ninguém assumiu esta issue ainda.

Avaliação

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

Direção de pesquisa

Comece por Psych::TreeBuilder#end_stream e #set_end_location em tree_builder.rb e, em seguida, acompanhe como YAMLTree#finish lida com um fluxo sem documentos. Adicione um teste de regressão para Psych.dump_stream com zero documentos e verifique se ele retorna uma string vazia que safe_load_stream lê como [].

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

Descrição

Version: Psych 5.3.1 (Ruby 4.0.10, libyaml 0.2.5). The dump-side crash
also reproduces on Psych 5.2.2 (Ruby 3.4.13).

Reproduction (complete; no input files needed):

require "psych"

Psych.dump_stream
# => NoMethodError: undefined method 'end_line=' for nil

The round-trip form of the same failure:

Psych.safe_load_stream("")   # => []
Psych.dump_stream(*Psych.safe_load_stream(""))
# => NoMethodError: undefined method 'end_line=' for nil

Any input that loads to zero documents triggers it, e.g. a comment-only
stream ("# Comment only.\n").

Expected: dumping an empty stream succeeds — for example, it returns
"", which safe_load_stream reads back as [].

Actual: NoMethodError raised from Psych::TreeBuilder#set_end_location
(tree_builder.rb:133), called from TreeBuilder#end_stream via
Psych::Visitors::YAMLTree#finish. With no document ever added, the node
whose end location is being set is nil.

Relation to #685: the same error message appears in #685, but that
report traces it to a syntax error in user code on the parse side. This
report is the dump side: nothing is parsed, no malformed input is involved,
and Psych.dump_stream with zero documents crashes deterministically.

Proposed fix: guard the nil node in TreeBuilder#end_stream /
#set_end_location so a stream with no documents finishes normally and the
emitter produces an empty string. If that direction looks right, may I send
a pull request with tests?


Disclosure: this was found with a load/dump round-trip (self-consistency)
check run with AI assistance. The reproduction above is minimal and was
verified by hand on the versions listed.

Linguagem predominante
Ruby
Estrelas
596
Forks
228
Merge médio
24min
PRs com merge (30d)
5

Preparar o ambiente

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.

Mais de ruby/psych

Todas as issues de ruby/psych

Issues semelhantes

Mais issues de Ruby

Receba novas issues na sua caixa de entrada

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