Psych.dump_stream raises NoMethodError when the stream has no documents

Aperta Adatta ai principianti
#839 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
83/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
ruby
Ambito
backend

Direzione di ricerca

Inizia con Psych::TreeBuilder#end_stream e #set_end_location in tree_builder.rb, poi segui il modo in cui YAMLTree#finish gestisce uno stream senza documenti. Aggiungi un test di regressione per Psych.dump_stream con zero documenti e verifica che restituisca una stringa vuota che safe_load_stream legge come [].

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Ruby
Stelle
596
Fork
228
Merge medio
24m
PR unite (30g)
5

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di ruby/psych

Tutte le issue di ruby/psych

Issue simili

Altre issue su Ruby

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.