Skip to content

Commit

Permalink
Fix error handling when parsing XML files via IO.pipe
Browse files Browse the repository at this point in the history
## Why?

IOError exception is not raised, but Errno::ESPIPE exception is raised.
```
@er_source.pos
#=> Errno::ESPIPE Exception: Illegal seek
```
  • Loading branch information
naitoh committed Nov 16, 2024
1 parent 20562ec commit d43ac92
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/rexml/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ def current_line
rescue
end
@er_source.seek(pos)
rescue IOError
rescue IOError, Errno::ESPIPE
pos = -1
line = -1
end
Expand Down
24 changes: 20 additions & 4 deletions test/parse/test_text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ 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
REXML::Parsers::BaseParser.new('a').pull
end

assert_equal(<<~DETAIL.chomp, exception.to_s)
Expand All @@ -21,6 +18,25 @@ def test_text_only
DETAIL
end

def test_text_only_with_io_pipe
IO.pipe do |reader, writer|
writer.write('a')
writer.close

exception = assert_raise(REXML::ParseException) do
REXML::Parsers::BaseParser.new(reader).pull
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
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 d43ac92

Please sign in to comment.