Skip to content

Commit 1531862

Browse files
authored
Fixed an issue with IOSource#read_until when reaching the end of a file (#288)
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' https://github.com/ruby/rexml/blob/185bdc737da406ba4f9564726849ad3477858eb2/lib/rexml/source.rb#L266-L276 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.
1 parent 185bdc7 commit 1531862

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

lib/rexml/source.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ def read_until(term)
272272
@scanner << readline(term)
273273
end
274274
if str
275-
read if @scanner.eos? and !@source.eof?
275+
read if @scanner.eos? and @source and !@source.eof?
276276
str
277277
else
278278
rest = @scanner.rest

test/test_io_source.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: false
2+
3+
require "rexml/source"
4+
5+
module REXMLTests
6+
class TestIOSource < Test::Unit::TestCase
7+
def setup
8+
@source = REXML::SourceFactory.create_from('<?xml version="1.0"?>')
9+
end
10+
11+
sub_test_case("#read_until") do
12+
test("eof") do
13+
assert_true(@source.read("nonexistent")) # Consume all data
14+
assert_false(@source.read("nonexistent")) # Set EOF
15+
assert_equal('<?xml version="1.0"?>', @source.read_until(">"))
16+
end
17+
end
18+
end
19+
end

0 commit comments

Comments
 (0)