ruby-changes:46919
From: kou <ko1@a...>
Date: Wed, 7 Jun 2017 22:01:33 +0900 (JST)
Subject: [ruby-changes:46919] kou:r59033 (trunk): rexml: add close tag check on end of document to StreamParser
kou 2017-06-07 22:01:28 +0900 (Wed, 07 Jun 2017) New Revision: 59033 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=59033 Log: rexml: add close tag check on end of document to StreamParser [ruby-core:81593] [Bug #13636] Reported by Anton Sivakov. Thanks!!! Added files: trunk/test/rexml/parser/test_stream.rb Modified files: trunk/lib/rexml/parsers/streamparser.rb Index: lib/rexml/parsers/streamparser.rb =================================================================== --- lib/rexml/parsers/streamparser.rb (revision 59032) +++ lib/rexml/parsers/streamparser.rb (revision 59033) @@ -7,6 +7,7 @@ module REXML https://github.com/ruby/ruby/blob/trunk/lib/rexml/parsers/streamparser.rb#L7 def initialize source, listener @listener = listener @parser = BaseParser.new( source ) + @tag_stack = [] end def add_listener( listener ) @@ -19,14 +20,21 @@ module REXML https://github.com/ruby/ruby/blob/trunk/lib/rexml/parsers/streamparser.rb#L20 event = @parser.pull case event[0] when :end_document + unless @tag_stack.empty? + tag_path = "/" + @tag_stack.join("/") + raise ParseException.new("Missing end tag for '#{tag_path}'", + @parser.source) + end return when :start_element + @tag_stack << event[1] attrs = event[2].each do |n, v| event[2][n] = @parser.unnormalize( v ) end @listener.tag_start( event[1], attrs ) when :end_element @listener.tag_end( event[1] ) + @tag_stack.pop when :text normalized = @parser.unnormalize( event[1] ) @listener.text( normalized ) Index: test/rexml/parser/test_stream.rb =================================================================== --- test/rexml/parser/test_stream.rb (nonexistent) +++ test/rexml/parser/test_stream.rb (revision 59033) @@ -0,0 +1,32 @@ https://github.com/ruby/ruby/blob/trunk/test/rexml/parser/test_stream.rb#L1 +require "test/unit" +require "rexml/document" +require "rexml/streamlistener" + +module REXMLTests + class TestStreamParser < Test::Unit::TestCase + class NullListener + include REXML::StreamListener + end + + class TestInvalid < self + def test_no_end_tag + xml = "<root><sub>" + exception = assert_raise(REXML::ParseException) do + parse(xml) + end + assert_equal(<<-MESSAGE, exception.to_s) +Missing end tag for '/root/sub' +Line: 1 +Position: #{xml.bytesize} +Last 80 unconsumed characters: + MESSAGE + end + + private + def parse(xml, listener=nil) + listener ||= NullListener.new + REXML::Document.parse_stream(xml, listener) + end + end + end +end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/