From aece1efd00365834dafb185974f7cb1631ac1e18 Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Sun, 13 Sep 2015 12:20:54 -0400 Subject: [PATCH] Ensure SyntaxError on empty XML in strict mode Previously raised RuntimeError in MRI for zero-length docs. Fixes #1349. Related to #1005 and 2c9b7f1 --- lib/nokogiri/xml/document.rb | 8 +++++++- test/xml/test_document.rb | 9 +++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/nokogiri/xml/document.rb b/lib/nokogiri/xml/document.rb index c83902cadfd..6c10df9ac0d 100644 --- a/lib/nokogiri/xml/document.rb +++ b/lib/nokogiri/xml/document.rb @@ -45,7 +45,13 @@ def self.parse string_or_io, url = nil, encoding = nil, options = ParseOptions:: # Give the options to the user yield options if block_given? - return new if !options.strict? && empty_doc?(string_or_io) + if empty_doc?(string_or_io) + if options.strict? + raise Nokogiri::XML::SyntaxError.new("Empty document") + else + return new + end + end doc = if string_or_io.respond_to?(:read) url ||= string_or_io.respond_to?(:path) ? string_or_io.path : nil diff --git a/test/xml/test_document.rb b/test/xml/test_document.rb index f7e90fa8e37..bd67a942da9 100644 --- a/test/xml/test_document.rb +++ b/test/xml/test_document.rb @@ -28,8 +28,13 @@ def test_dtd_with_empty_internal_subset # issue #1005 def test_strict_parsing_empty_doc_should_raise_exception - assert_raises(SyntaxError) do - Nokogiri::XML(StringIO.new('')) { |c| c.strict } + ["", " "].each do |empty_string| + assert_raises(SyntaxError, "empty string '#{empty_string}' should raise a SyntaxError") do + Nokogiri::XML(empty_string) { |c| c.strict } + end + assert_raises(SyntaxError, "StringIO of '#{empty_string}' should raise a SyntaxError") do + Nokogiri::XML(StringIO.new(empty_string)) { |c| c.strict } + end end end