Psych.dump_stream raises NoMethodError when the stream has no documents

オープン 初心者向け
#839 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
83/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
ruby
領域
backend

調査の方向性

まず tree_builder.rb の Psych::TreeBuilder#end_stream と #set_end_location から始め、次に YAMLTree#finish がドキュメントのないストリームをどのように処理するかを追ってください。ドキュメント数がゼロの Psych.dump_stream に対するリグレッションテストを追加し、空文字列が返され、それを safe_load_stream が [] として読み込むことを確認してください。

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Ruby
スター
596
フォーク
228
平均マージ
24分
マージ済み PR(30日)
5

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

ruby/psych のほかの issue

ruby/psych の issue をすべて見る

似ている issue

Ruby の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。