From 10184ad7a69f444832d7a915c9f760798551ea7d Mon Sep 17 00:00:00 2001 From: fatkodima Date: Mon, 6 Mar 2023 03:16:39 +0200 Subject: [PATCH] Performance and memory optimizations Co-authored-by: Sutou Kouhei --- lib/rexml/attribute.rb | 11 ++++++---- lib/rexml/entity.rb | 40 +++++++++++++++++++++------------- lib/rexml/formatters/pretty.rb | 4 ++-- lib/rexml/namespace.rb | 12 ++++++---- lib/rexml/text.rb | 10 +++++---- test/test_core.rb | 2 +- test/test_document.rb | 8 +++---- 7 files changed, 52 insertions(+), 35 deletions(-) diff --git a/lib/rexml/attribute.rb b/lib/rexml/attribute.rb index c198e00a..11893a95 100644 --- a/lib/rexml/attribute.rb +++ b/lib/rexml/attribute.rb @@ -1,4 +1,4 @@ -# frozen_string_literal: false +# frozen_string_literal: true require_relative "namespace" require_relative 'text' @@ -119,10 +119,13 @@ def hash # b = Attribute.new( "ns:x", "y" ) # b.to_string # -> "ns:x='y'" def to_string + value = to_s if @element and @element.context and @element.context[:attribute_quote] == :quote - %Q^#@expanded_name="#{to_s().gsub(/"/, '"')}"^ + value = value.gsub('"', '"') if value.include?('"') + %Q^#@expanded_name="#{value}"^ else - "#@expanded_name='#{to_s().gsub(/'/, ''')}'" + value = value.gsub("'", ''') if value.include?("'") + "#@expanded_name='#{value}'" end end @@ -192,7 +195,7 @@ def node_type end def inspect - rv = "" + rv = +"" write( rv ) rv end diff --git a/lib/rexml/entity.rb b/lib/rexml/entity.rb index 89a9e84c..573db691 100644 --- a/lib/rexml/entity.rb +++ b/lib/rexml/entity.rb @@ -132,24 +132,34 @@ def to_s # then: # doctype.entity('yada').value #-> "nanoo bar nanoo" def value - if @value - matches = @value.scan(PEREFERENCE_RE) - rv = @value.clone - if @parent - sum = 0 - matches.each do |entity_reference| - entity_value = @parent.entity( entity_reference[0] ) - if sum + entity_value.bytesize > Security.entity_expansion_text_limit - raise "entity expansion has grown too large" - else - sum += entity_value.bytesize - end - rv.gsub!( /%#{entity_reference.join};/um, entity_value ) + @resolved_value ||= resolve_value + end + + def parent=(other) + @resolved_value = nil + super + end + + private + def resolve_value + return nil if @value.nil? + return @value unless @value.match?(PEREFERENCE_RE) + + matches = @value.scan(PEREFERENCE_RE) + rv = @value.clone + if @parent + sum = 0 + matches.each do |entity_reference| + entity_value = @parent.entity( entity_reference[0] ) + if sum + entity_value.bytesize > Security.entity_expansion_text_limit + raise "entity expansion has grown too large" + else + sum += entity_value.bytesize end + rv.gsub!( /%#{entity_reference.join};/um, entity_value ) end - return rv end - nil + rv end end diff --git a/lib/rexml/formatters/pretty.rb b/lib/rexml/formatters/pretty.rb index 562ef946..a1198b7a 100644 --- a/lib/rexml/formatters/pretty.rb +++ b/lib/rexml/formatters/pretty.rb @@ -1,4 +1,4 @@ -# frozen_string_literal: false +# frozen_string_literal: true require_relative 'default' module REXML @@ -58,7 +58,7 @@ def write_element(node, output) skip = false if compact if node.children.inject(true) {|s,c| s & c.kind_of?(Text)} - string = "" + string = +"" old_level = @level @level = 0 node.children.each { |child| write( child, string ) } diff --git a/lib/rexml/namespace.rb b/lib/rexml/namespace.rb index 924edf95..2e67252a 100644 --- a/lib/rexml/namespace.rb +++ b/lib/rexml/namespace.rb @@ -1,4 +1,4 @@ -# frozen_string_literal: false +# frozen_string_literal: true require_relative 'xmltokens' @@ -10,13 +10,17 @@ module Namespace # The expanded name of the object, valid if name is set attr_accessor :prefix include XMLTokens + NAME_WITHOUT_NAMESPACE = /\A#{NCNAME_STR}\z/ NAMESPLIT = /^(?:(#{NCNAME_STR}):)?(#{NCNAME_STR})/u # Sets the name and the expanded name def name=( name ) @expanded_name = name - case name - when NAMESPLIT + if name.match?(NAME_WITHOUT_NAMESPACE) + @prefix = "" + @namespace = "" + @name = name + elsif name =~ NAMESPLIT if $1 @prefix = $1 else @@ -24,7 +28,7 @@ def name=( name ) @namespace = "" end @name = $2 - when "" + elsif name == "" @prefix = nil @namespace = nil @name = nil diff --git a/lib/rexml/text.rb b/lib/rexml/text.rb index 050b09c9..b47bad3b 100644 --- a/lib/rexml/text.rb +++ b/lib/rexml/text.rb @@ -1,4 +1,4 @@ -# frozen_string_literal: false +# frozen_string_literal: true require_relative 'security' require_relative 'entity' require_relative 'doctype' @@ -131,7 +131,7 @@ def parent= parent def Text.check string, pattern, doctype # illegal anywhere - if string !~ VALID_XML_CHARS + if !string.match?(VALID_XML_CHARS) if String.method_defined? :encode string.chars.each do |c| case c.ord @@ -371,7 +371,7 @@ def Text::normalize( input, doctype=nil, entity_filter=nil ) copy = input.to_s # Doing it like this rather than in a loop improves the speed #copy = copy.gsub( EREFERENCE, '&' ) - copy = copy.gsub( "&", "&" ) + copy = copy.gsub( "&", "&" ) if copy.include?("&") if doctype # Replace all ampersands that aren't part of an entity doctype.entities.each_value do |entity| @@ -382,7 +382,9 @@ def Text::normalize( input, doctype=nil, entity_filter=nil ) else # Replace all ampersands that aren't part of an entity DocType::DEFAULT_ENTITIES.each_value do |entity| - copy = copy.gsub(entity.value, "&#{entity.name};" ) + if copy.include?(entity.value) + copy = copy.gsub(entity.value, "&#{entity.name};" ) + end end end copy diff --git a/test/test_core.rb b/test/test_core.rb index fd3af8c2..7c18c03f 100644 --- a/test/test_core.rb +++ b/test/test_core.rb @@ -1423,7 +1423,7 @@ def test_ticket_91 d.root.add_element( "bah" ) p=REXML::Formatters::Pretty.new(2) p.compact = true # Don't add whitespace to text nodes unless necessary - p.write(d,out="") + p.write(d,out=+"") assert_equal( expected, out ) end diff --git a/test/test_document.rb b/test/test_document.rb index 5a8e7ec5..cca67df2 100644 --- a/test/test_document.rb +++ b/test/test_document.rb @@ -166,11 +166,9 @@ def test_empty_value EOF - assert_raise(REXML::ParseException) do - REXML::Document.new(xml) - end - REXML::Security.entity_expansion_limit = 100 - assert_equal(100, REXML::Security.entity_expansion_limit) + REXML::Document.new(xml) + REXML::Security.entity_expansion_limit = 90 + assert_equal(90, REXML::Security.entity_expansion_limit) assert_raise(REXML::ParseException) do REXML::Document.new(xml) end