Skip to content

Commit cf0fb9c

Browse files
authored
Fix IOSource#readline for @pending_buffer (#215)
## Why? Fixed a problem that `@pending_buffer` is not processed when `IOError` occurs in `@source.readline` although `@pending_buffer` exists when reading XML file.
1 parent 1d0c362 commit cf0fb9c

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

lib/rexml/parsers/baseparser.rb

+1
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ def initialize( source )
167167
@entity_expansion_count = 0
168168
@entity_expansion_limit = Security.entity_expansion_limit
169169
@entity_expansion_text_limit = Security.entity_expansion_text_limit
170+
@source.ensure_buffer
170171
end
171172

172173
def add_listener( listener )

lib/rexml/source.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -295,14 +295,19 @@ def current_line
295295

296296
private
297297
def readline(term = nil)
298-
str = @source.readline(term || @line_break)
299298
if @pending_buffer
299+
begin
300+
str = @source.readline(term || @line_break)
301+
rescue IOError
302+
end
300303
if str.nil?
301304
str = @pending_buffer
302305
else
303306
str = @pending_buffer + str
304307
end
305308
@pending_buffer = nil
309+
else
310+
str = @source.readline(term || @line_break)
306311
end
307312
return nil if str.nil?
308313

test/parse/test_text.rb

+17
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,23 @@
44
module REXMLTests
55
class TestParseText < Test::Unit::TestCase
66
class TestInvalid < self
7+
def test_text_only
8+
exception = assert_raise(REXML::ParseException) do
9+
parser = REXML::Parsers::BaseParser.new('a')
10+
while parser.has_next?
11+
parser.pull
12+
end
13+
end
14+
15+
assert_equal(<<~DETAIL.chomp, exception.to_s)
16+
Malformed XML: Content at the start of the document (got 'a')
17+
Line: 1
18+
Position: 1
19+
Last 80 unconsumed characters:
20+
21+
DETAIL
22+
end
23+
724
def test_before_root
825
exception = assert_raise(REXML::ParseException) do
926
parser = REXML::Parsers::BaseParser.new('b<a></a>')

0 commit comments

Comments
 (0)