Skip to content

Commit

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

If via IO.pipe, `IOError` exception is not raised, but `Errno::ESPIPE`
or `Errno::EPIPE` or `Errno::EINVAL` exception is raised.

- CRuby
```
@er_source.pos
#=> Errno::ESPIPE Exception: Illegal seek
```

- CRuby (Windows (Ruby 2.6 or earlier))
```
@er_source.pos
#=> Errno::EINVAL: Invalid argument
```

- JRuby
```
@er_source.pos
#=> Errno::EPIPE: Broken pipe - No message available
```

Co-authored-by: Sutou Kouhei <kou@clear-code.com>
  • Loading branch information
naitoh and kou authored Nov 19, 2024
1 parent 20562ec commit 963ccdf
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, SystemCallError
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 963ccdf

Please sign in to comment.