Skip to content

Commit

Permalink
Fix IOSource#readline for @pending_buffer (#215)
Browse files Browse the repository at this point in the history
## Why?
Fixed a problem that `@pending_buffer` is not processed when `IOError`
occurs in `@source.readline` although `@pending_buffer` exists when
reading XML file.
  • Loading branch information
naitoh authored Oct 19, 2024
1 parent 1d0c362 commit cf0fb9c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/rexml/parsers/baseparser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ def initialize( source )
@entity_expansion_count = 0
@entity_expansion_limit = Security.entity_expansion_limit
@entity_expansion_text_limit = Security.entity_expansion_text_limit
@source.ensure_buffer
end

def add_listener( listener )
Expand Down
7 changes: 6 additions & 1 deletion lib/rexml/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -295,14 +295,19 @@ def current_line

private
def readline(term = nil)
str = @source.readline(term || @line_break)
if @pending_buffer
begin
str = @source.readline(term || @line_break)
rescue IOError
end
if str.nil?
str = @pending_buffer
else
str = @pending_buffer + str
end
@pending_buffer = nil
else
str = @source.readline(term || @line_break)
end
return nil if str.nil?

Expand Down
17 changes: 17 additions & 0 deletions test/parse/test_text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@
module REXMLTests
class TestParseText < Test::Unit::TestCase
class TestInvalid < self
def test_text_only
exception = assert_raise(REXML::ParseException) do
parser = REXML::Parsers::BaseParser.new('a')
while parser.has_next?
parser.pull
end
end

assert_equal(<<~DETAIL.chomp, exception.to_s)
Malformed XML: Content at the start of the document (got 'a')
Line: 1
Position: 1
Last 80 unconsumed characters:
DETAIL
end

def test_before_root
exception = assert_raise(REXML::ParseException) do
parser = REXML::Parsers::BaseParser.new('b<a></a>')
Expand Down

0 comments on commit cf0fb9c

Please # to comment.