Psych.dump_stream raises NoMethodError when the stream has no documents

Đang mở Phù hợp với người mới
#839 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
83/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
ruby
Lĩnh vực
backend

Hướng nghiên cứu

Bắt đầu với Psych::TreeBuilder#end_stream và #set_end_location trong tree_builder.rb, sau đó lần theo cách YAMLTree#finish xử lý một luồng không có tài liệu. Thêm kiểm thử hồi quy cho Psych.dump_stream với không tài liệu nào và xác minh rằng nó trả về chuỗi rỗng mà safe_load_stream đọc thành [].

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Ruby
Star
596
Fork
228
Merge trung bình
24 phút
Pull request đã merge (30 ngày)
5

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của ruby/psych

Tất cả issue của ruby/psych

Issue tương tự

Thêm issue về Ruby

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.