From 210ecbccac1f99bec584927a95a272ec29e4e5f9 Mon Sep 17 00:00:00 2001 From: Ricardo Amendoeira Date: Fri, 19 Nov 2021 11:12:01 +0000 Subject: [PATCH 1/3] Add test for exception being raised from an XML::Builder block Co-authored-by: Joana Tavares --- test/xml/test_builder.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/xml/test_builder.rb b/test/xml/test_builder.rb index c27a848aa4..89a9ac522a 100644 --- a/test/xml/test_builder.rb +++ b/test/xml/test_builder.rb @@ -22,6 +22,29 @@ def test_builder_multiple_nodes end end + def test_builder_resilient_to_exceptions + builder = Nokogiri::XML::Builder.new do |xml| + xml.root do + begin + xml.a { raise "badjoras" } + rescue StandardError + # Ignored + end + + xml.b + end + end + + expected_output = <<~HEREDOC + + + + + + HEREDOC + assert_equal(expected_output, builder.to_xml) + end + def test_builder_with_utf8_text text = "test οΊ΅ " doc = Nokogiri::XML::Builder.new(encoding: "UTF-8") { |xml| xml.test(text) }.doc From f61dc0d618d7316a481efef9f3dc5192e9fbfed3 Mon Sep 17 00:00:00 2001 From: Ricardo Amendoeira Date: Fri, 19 Nov 2021 11:15:19 +0000 Subject: [PATCH 2/3] Update XML::Builder to safely handle exceptions raised in blocks Co-authored-by: Joana Tavares --- lib/nokogiri/xml/builder.rb | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/nokogiri/xml/builder.rb b/lib/nokogiri/xml/builder.rb index 98bb075b72..80428f6d4f 100644 --- a/lib/nokogiri/xml/builder.rb +++ b/lib/nokogiri/xml/builder.rb @@ -425,15 +425,18 @@ def method_missing(method, *args, &block) # :nodoc: def insert(node, &block) node = @parent.add_child(node) if block - old_parent = @parent - @parent = node - @arity ||= block.arity - if @arity <= 0 - instance_eval(&block) - else - yield(self) + begin + old_parent = @parent + @parent = node + @arity ||= block.arity + if @arity <= 0 + instance_eval(&block) + else + yield(self) + end + ensure + @parent = old_parent end - @parent = old_parent end NodeBuilder.new(node, self) end From be4a198a09e29514613114bb8285a90675e90517 Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Fri, 19 Nov 2021 10:06:44 -0500 Subject: [PATCH 3/3] update CHANGELOG for #2372 --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d678d12a6..dcb26061b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ A related discussion about Trust exists at [#2357](https://github.com/sparklemot ### Fixed +* XML::Builder blocks restore context properly when exceptions are raised. [[#2372](https://github.com/sparklemotion/nokogiri/issues/2372)] (Thanks, [@ric2b](https://github.com/ric2b) and [@rinthedev](https://github.com/rinthedev)!) * [CRuby] Fix memory leak in `Document#canonicalize` when inclusive namespaces are passed in. [[#2345](https://github.com/sparklemotion/nokogiri/issues/2345)] * [CRuby] Fix memory leak in `Document#canonicalize` when an argument type error is raised. [[#2345](https://github.com/sparklemotion/nokogiri/issues/2345)] * [CRuby] Fix memory leak in `EncodingHandler` where iconv handlers were not being cleaned up. [[#2345](https://github.com/sparklemotion/nokogiri/issues/2345)]