Skip to content

Fixed an issue with IOSource#read_until when reaching the end of a file#288

Merged
kou merged 1 commit into
ruby:masterfrom
naitoh:fix_read_until_error
Sep 4, 2025
Merged

Fixed an issue with IOSource#read_until when reaching the end of a file#288
kou merged 1 commit into
ruby:masterfrom
naitoh:fix_read_until_error

Conversation

@naitoh

@naitoh naitoh commented Aug 28, 2025

Copy link
Copy Markdown
Contributor

Fix GH-287

Why?

If @source = nil is set at the end of the file, prevent IOSource#read_until from raising an error.

/.gems/gems/rexml-3.4.2/lib/rexml/parsers/baseparser.rb:524:in 'REXML::Parsers::BaseParser#pull_event': #<NoMethodError: undefined method 'eof?' for nil> (REXML::ParseException)
.gems/gems/rexml-3.4.2/lib/rexml/source.rb:275:in 'REXML::IOSource#read_until'

rexml/lib/rexml/source.rb

Lines 266 to 276 in 185bdc7

def read_until(term)
pattern = Private::PRE_DEFINED_TERM_PATTERNS[term] || /#{Regexp.escape(term)}/
term = @encoded_terms[term] ||= encode(term)
until str = @scanner.scan_until(pattern)
break if @source.nil?
break if @source.eof?
@scanner << readline(term)
end
if str
read if @scanner.eos? and !@source.eof?
str

  1. Reaching the end of a file in IOSource#read.
  2. If the string remains in @scanner.rest, use @scanner.scan_until(pattern) to return str.
  3. If @source is nil, an exception occurs.
@naitoh naitoh marked this pull request as draft August 28, 2025 13:24
@naitoh naitoh marked this pull request as ready for review September 3, 2025 08:35
@naitoh naitoh requested a review from kou September 3, 2025 08:50
Comment thread lib/rexml/source.rb
@naitoh naitoh force-pushed the fix_read_until_error branch from 3ed00ef to cd1c156 Compare September 3, 2025 14:14
@naitoh naitoh changed the title Fixed initialization processing when reaching the end of a file in IOSource#read Sep 3, 2025
@naitoh naitoh requested a review from kou September 3, 2025 14:18
Comment thread test/test_io_source.rb Outdated
Comment thread test/test_io_source.rb Outdated
Comment thread test/test_io_source.rb Outdated
…file

## Why?
If `@source = nil` is set at the end of the file, prevent `IOSource#read_until` from raising an error.

> /.gems/gems/rexml-3.4.2/lib/rexml/parsers/baseparser.rb:524:in 'REXML::Parsers::BaseParser#pull_event': #<NoMethodError: undefined method 'eof?' for nil> (REXML::ParseException)
> .gems/gems/rexml-3.4.2/lib/rexml/source.rb:275:in 'REXML::IOSource#read_until'

See: https://github.com/ruby/rexml/blob/185bdc737da406ba4f9564726849ad3477858eb2/lib/rexml/source.rb#L266-L276
@naitoh naitoh force-pushed the fix_read_until_error branch from cd1c156 to 9ddcf5c Compare September 4, 2025 10:46
@naitoh naitoh requested a review from kou September 4, 2025 10:51
@kou kou merged commit 1531862 into ruby:master Sep 4, 2025
67 checks passed
@kou

kou commented Sep 4, 2025

Copy link
Copy Markdown
Member

Thanks.

@naitoh naitoh deleted the fix_read_until_error branch September 7, 2025 08:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants