Psych.dump_stream raises NoMethodError when the stream has no documents

Offen Anfängerfreundlich
#839 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
83/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
ruby
Bereich
backend

Rechercherichtung

Beginne mit Psych::TreeBuilder#end_stream und #set_end_location in tree_builder.rb und verfolge anschließend, wie YAMLTree#finish einen Stream ohne Dokumente behandelt. Füge einen Regressionstest für Psych.dump_stream mit null Dokumenten hinzu und überprüfe, dass ein leerer String zurückgegeben wird, den safe_load_stream als [] einliest.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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.

Vorherrschende Sprache
Ruby
Sterne
596
Forks
228
Ø Merge
24 Min.
Gemergte PRs (30 T.)
5

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus ruby/psych

Alle Issues in ruby/psych

Ähnliche Issues

Weitere Issues zu Ruby

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.