From fb20023e1e2570508027e24c00f54eda24e07b73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Tarti=C3=A8re?= Date: Mon, 21 Aug 2023 05:33:37 -1000 Subject: [PATCH] (PUP-11841) Fix encoding of empty String In 2077971499d3cfc3e33b6eba4c91f43f82f1214c empty string literals where replaced by a call to `String.new` in preparation for moving to frozen/immutable strings. However, as stated in the String#new documentation, when no value is passed to `#new` the returned string has the 'ASCII-8BIT' encoding instead of the default one (which is assumed to be 'UTF-8' by Puppet). This cause regressions in some areas of the code, for example when using the concat module with exported resource built using epp templates, the incorrect encoding cause the fragment to be misinterpreted and is base64 encoded. When collected, the base64 representation of the string is used instead of the actual value of the string, as reported here: https://github.com/voxpupuli/puppet-bacula/issues/189 Replace calls to `String.new` with `''.dup` which use the current encoding. Do not change the few explicit but redundant occurrences of `String.new.force_encoding('ASCII-8BIT')` (so that the intent is clearly visible). Where appropriate, slightly adjust the code for better readability. --- lib/puppet/application/doc.rb | 2 +- lib/puppet/face/config.rb | 2 +- lib/puppet/face/epp.rb | 4 ++-- lib/puppet/face/module/list.rb | 4 ++-- lib/puppet/face/parser.rb | 2 +- lib/puppet/indirector/facts/facter.rb | 2 +- lib/puppet/indirector/file_bucket_file/file.rb | 2 +- lib/puppet/indirector/indirection.rb | 2 +- lib/puppet/module_tool.rb | 2 +- lib/puppet/network/formats.rb | 6 +++--- lib/puppet/network/http/memory_response.rb | 2 +- lib/puppet/parameter/value_collection.rb | 2 +- lib/puppet/parser/functions.rb | 2 +- lib/puppet/pops/loader/loader_paths.rb | 8 ++++---- lib/puppet/pops/lookup/explainer.rb | 2 +- lib/puppet/pops/lookup/hiera_config.rb | 2 +- lib/puppet/pops/model/factory.rb | 2 +- lib/puppet/pops/model/tree_dumper.rb | 2 +- lib/puppet/pops/parser/epp_support.rb | 2 +- lib/puppet/pops/parser/evaluating_parser.rb | 2 +- lib/puppet/pops/parser/pn_parser.rb | 2 +- lib/puppet/pops/pn.rb | 2 +- lib/puppet/pops/serialization/json_path.rb | 2 +- lib/puppet/pops/time/timespan.rb | 6 +++--- lib/puppet/pops/types/ruby_generator.rb | 4 ++-- lib/puppet/pops/types/string_converter.rb | 12 ++++++------ lib/puppet/pops/types/type_formatter.rb | 4 ++-- lib/puppet/pops/types/types.rb | 2 +- lib/puppet/provider/nameservice/directoryservice.rb | 4 ++-- lib/puppet/provider/user/directoryservice.rb | 2 +- lib/puppet/reference/configuration.rb | 2 +- lib/puppet/reference/indirection.rb | 2 +- lib/puppet/reports.rb | 2 +- lib/puppet/transaction/report.rb | 2 +- lib/puppet/util.rb | 5 ++--- lib/puppet/util/diff.rb | 2 +- lib/puppet/util/execution.rb | 4 ++-- lib/puppet/util/inifile.rb | 4 ++-- lib/puppet/util/package/version/rpm.rb | 2 +- lib/puppet/util/provider_features.rb | 2 +- lib/puppet/util/selinux.rb | 2 +- lib/puppet/util/windows/access_control_entry.rb | 2 +- lib/puppet/util/windows/access_control_list.rb | 2 +- lib/puppet/util/windows/adsi.rb | 4 ++-- lib/puppet/util/windows/error.rb | 2 +- lib/puppet/util/windows/file.rb | 4 ++-- lib/puppet/util/windows/process.rb | 2 +- 47 files changed, 69 insertions(+), 70 deletions(-) diff --git a/lib/puppet/application/doc.rb b/lib/puppet/application/doc.rb index b0c6abe5f27..dab806de454 100644 --- a/lib/puppet/application/doc.rb +++ b/lib/puppet/application/doc.rb @@ -152,7 +152,7 @@ def rdoc end def other - text = String.new + text = ''.dup with_contents = options[:references].length <= 1 exit_code = 0 require_relative '../../puppet/util/reference' diff --git a/lib/puppet/face/config.rb b/lib/puppet/face/config.rb index 76765b8d693..f500f224a8d 100644 --- a/lib/puppet/face/config.rb +++ b/lib/puppet/face/config.rb @@ -82,7 +82,7 @@ end when_rendering :console do |to_be_rendered| - output = String.new + output = ''.dup if to_be_rendered.keys.length > 1 to_be_rendered.keys.sort.each do |setting| output << "#{setting} = #{to_be_rendered[setting]}\n" diff --git a/lib/puppet/face/epp.rb b/lib/puppet/face/epp.rb index 149e5844d46..47ee58d89c5 100644 --- a/lib/puppet/face/epp.rb +++ b/lib/puppet/face/epp.rb @@ -367,7 +367,7 @@ end def dump_parse(source, filename, options, show_filename = true) - output = String.new + output = ''.dup evaluating_parser = Puppet::Pops::Parser::EvaluatingParser::EvaluatingEppParser.new begin if options[:validate] @@ -451,7 +451,7 @@ def render_inline(epp_source, compiler, options) def render_file(epp_template_name, compiler, options, show_filename, file_nbr) template_args = get_values(compiler, options) - output = String.new + output = ''.dup begin if show_filename && options[:header] output << "\n" unless file_nbr == 1 diff --git a/lib/puppet/face/module/list.rb b/lib/puppet/face/module/list.rb index 5ffa023bb14..ed47eed4fac 100644 --- a/lib/puppet/face/module/list.rb +++ b/lib/puppet/face/module/list.rb @@ -74,7 +74,7 @@ environment = result[:environment] modules_by_path = result[:modules_by_path] - output = String.new + output = ''.dup warn_unmet_dependencies(environment) @@ -248,7 +248,7 @@ def list_build_tree(list, ancestors=[], parent=nil, params={}) # Returns a Hash # def list_build_node(mod, parent, params) - str = String.new + str = ''.dup str << (mod.forge_name ? mod.forge_name.tr('/', '-') : mod.name) str << ' (' + colorize(:cyan, mod.version ? "v#{mod.version}" : '???') + ')' diff --git a/lib/puppet/face/parser.rb b/lib/puppet/face/parser.rb index 7170e4a6702..c773eb9dbf9 100644 --- a/lib/puppet/face/parser.rb +++ b/lib/puppet/face/parser.rb @@ -174,7 +174,7 @@ end def dump_parse(source, filename, options, show_filename = true) - output = String.new + output = ''.dup evaluating_parser = Puppet::Pops::Parser::EvaluatingParser.new begin if options[:validate] diff --git a/lib/puppet/indirector/facts/facter.rb b/lib/puppet/indirector/facts/facter.rb index c4e420fde27..15620659505 100644 --- a/lib/puppet/indirector/facts/facter.rb +++ b/lib/puppet/indirector/facts/facter.rb @@ -105,7 +105,7 @@ def self.setup_external_search_paths(request) def find_with_options(request) options = request.options - options_for_facter = String.new + options_for_facter = ''.dup options_for_facter += options[:user_query].join(' ') options_for_facter += " --config #{options[:config_file]}" if options[:config_file] options_for_facter += " --show-legacy" if options[:show_legacy] diff --git a/lib/puppet/indirector/file_bucket_file/file.rb b/lib/puppet/indirector/file_bucket_file/file.rb index 15e5c640d29..427b09a1823 100644 --- a/lib/puppet/indirector/file_bucket_file/file.rb +++ b/lib/puppet/indirector/file_bucket_file/file.rb @@ -58,7 +58,7 @@ def list(request) end # Setting hash's default value to [], needed by the following loop bucket = Hash.new {[]} - msg = String.new + msg = ''.dup # Get all files with mtime between 'from' and 'to' Pathname.new(request.options[:bucket_path]).find { |item| if item.file? and item.basename.to_s == "paths" diff --git a/lib/puppet/indirector/indirection.rb b/lib/puppet/indirector/indirection.rb index 7ac26f923fd..2c3f5f5b781 100644 --- a/lib/puppet/indirector/indirection.rb +++ b/lib/puppet/indirector/indirection.rb @@ -81,7 +81,7 @@ def expiration # Generate the full doc string. def doc - text = String.new + text = ''.dup text << scrub(@doc) << "\n\n" if @doc diff --git a/lib/puppet/module_tool.rb b/lib/puppet/module_tool.rb index b017818c959..2153760904a 100644 --- a/lib/puppet/module_tool.rb +++ b/lib/puppet/module_tool.rb @@ -70,7 +70,7 @@ def self.is_module_root?(path) # Builds a formatted tree from a list of node hashes containing +:text+ # and +:dependencies+ keys. def self.format_tree(nodes, level = 0) - str = String.new + str = ''.dup nodes.each_with_index do |node, i| last_node = nodes.length - 1 == i deps = node[:dependencies] || [] diff --git a/lib/puppet/network/formats.rb b/lib/puppet/network/formats.rb index 67dc8a32a35..112a0161773 100644 --- a/lib/puppet/network/formats.rb +++ b/lib/puppet/network/formats.rb @@ -156,7 +156,7 @@ def render(datum) # Simple hash to table if datum.is_a?(Hash) && datum.keys.all? { |x| x.is_a?(String) || x.is_a?(Numeric) } - output = String.new + output = ''.dup column_a = datum.empty? ? 2 : datum.map{ |k,v| k.to_s.length }.max + 2 datum.sort_by { |k,v| k.to_s } .each do |key, value| output << key.to_s.ljust(column_a) @@ -169,7 +169,7 @@ def render(datum) # Print one item per line for arrays if datum.is_a? Array - output = String.new + output = ''.dup datum.each do |item| output << item.to_s output << "\n" @@ -227,7 +227,7 @@ def flatten_array(array) end def construct_output(data) - output = String.new + output = ''.dup data.each do |key, value| output << "#{key}=#{value}" output << "\n" diff --git a/lib/puppet/network/http/memory_response.rb b/lib/puppet/network/http/memory_response.rb index caed3de75c7..c109b3942d4 100644 --- a/lib/puppet/network/http/memory_response.rb +++ b/lib/puppet/network/http/memory_response.rb @@ -3,7 +3,7 @@ class Puppet::Network::HTTP::MemoryResponse attr_reader :code, :type, :body def initialize - @body = String.new + @body = ''.dup end def respond_with(code, type, body) diff --git a/lib/puppet/parameter/value_collection.rb b/lib/puppet/parameter/value_collection.rb index 341a831fac6..537df0a7e68 100644 --- a/lib/puppet/parameter/value_collection.rb +++ b/lib/puppet/parameter/value_collection.rb @@ -31,7 +31,7 @@ def aliasvalue(name, other) # def doc unless defined?(@doc) - @doc = String.new + @doc = ''.dup unless values.empty? @doc << "Valid values are " @doc << @strings.collect do |value| diff --git a/lib/puppet/parser/functions.rb b/lib/puppet/parser/functions.rb index 42503656242..9648f8d4dd1 100644 --- a/lib/puppet/parser/functions.rb +++ b/lib/puppet/parser/functions.rb @@ -258,7 +258,7 @@ def self.function(name, environment = Puppet.lookup(:current_environment)) def self.functiondocs(environment = Puppet.lookup(:current_environment)) autoloader.delegatee.loadall(environment) - ret = String.new + ret = ''.dup merged_functions(environment).sort { |a,b| a[0].to_s <=> b[0].to_s }.each do |name, hash| ret << "#{name}\n#{"-" * name.to_s.length}\n" diff --git a/lib/puppet/pops/loader/loader_paths.rb b/lib/puppet/pops/loader/loader_paths.rb index a4339b5b7b8..cdf4a28e47e 100644 --- a/lib/puppet/pops/loader/loader_paths.rb +++ b/lib/puppet/pops/loader/loader_paths.rb @@ -88,7 +88,7 @@ def effective_path(typed_name, start_index_in_name) def typed_name(type, name_authority, relative_path, module_name) # Module name is assumed to be included in the path and therefore not added here - n = String.new + n = ''.dup unless extension.empty? # Remove extension relative_path = relative_path[0..-(extension.length+1)] @@ -153,7 +153,7 @@ def effective_path(typed_name, start_index_in_name) end def typed_name(type, name_authority, relative_path, module_name) - n = String.new + n = ''.dup n << module_name unless module_name.nil? unless extension.empty? # Remove extension @@ -249,7 +249,7 @@ def relative_path end def typed_name(type, name_authority, relative_path, module_name) - n = String.new + n = ''.dup n << module_name unless module_name.nil? # Remove the file extension, defined as everything after the *last* dot. @@ -351,7 +351,7 @@ def typed_name(type, name_authority, relative_path, module_name) if @init_filenames.include?(relative_path) && !(module_name.nil? || module_name.empty?) TypedName.new(type, module_name, name_authority) else - n = String.new + n = ''.dup n << module_name unless module_name.nil? ext = @extensions.find { |extension| relative_path.end_with?(extension) } relative_path = relative_path[0..-(ext.length+1)] diff --git a/lib/puppet/pops/lookup/explainer.rb b/lib/puppet/pops/lookup/explainer.rb index e3244427280..62bd46fbf57 100644 --- a/lib/puppet/pops/lookup/explainer.rb +++ b/lib/puppet/pops/lookup/explainer.rb @@ -20,7 +20,7 @@ def to_hash end def explain - io = String.new + io = ''.dup dump_on(io, '', '') io end diff --git a/lib/puppet/pops/lookup/hiera_config.rb b/lib/puppet/pops/lookup/hiera_config.rb index d679b738c6a..4f7283905a4 100644 --- a/lib/puppet/pops/lookup/hiera_config.rb +++ b/lib/puppet/pops/lookup/hiera_config.rb @@ -228,7 +228,7 @@ def find_line_matching(regexp, start_line = 1) line_number += 1 next if line_number < start_line quote = nil - stripped = String.new + stripped = ''.dup line.each_codepoint do |cp| if cp == 0x22 || cp == 0x27 # double or single quote if quote == cp diff --git a/lib/puppet/pops/model/factory.rb b/lib/puppet/pops/model/factory.rb index 68a0fe63928..6ce9f37fa49 100644 --- a/lib/puppet/pops/model/factory.rb +++ b/lib/puppet/pops/model/factory.rb @@ -1112,7 +1112,7 @@ def is_interop_rewriteable?(o) end def self.concat(*args) - result = String.new + result = ''.dup args.each do |e| if e.instance_of?(Factory) && e.model_class <= LiteralString result << e[KEY_VALUE] diff --git a/lib/puppet/pops/model/tree_dumper.rb b/lib/puppet/pops/model/tree_dumper.rb index e6b2a8bf9f5..0103886fa60 100644 --- a/lib/puppet/pops/model/tree_dumper.rb +++ b/lib/puppet/pops/model/tree_dumper.rb @@ -21,7 +21,7 @@ def indent end def format(x) - result = String.new + result = ''.dup parts = format_r(x) parts.each_index do |i| if i > 0 diff --git a/lib/puppet/pops/parser/epp_support.rb b/lib/puppet/pops/parser/epp_support.rb index cae046c2a62..55e502a05e3 100644 --- a/lib/puppet/pops/parser/epp_support.rb +++ b/lib/puppet/pops/parser/epp_support.rb @@ -183,7 +183,7 @@ def scan(skip_leading=false) @skip_leading = skip_leading return nil if scanner.eos? - s = String.new + s = ''.dup until scanner.eos? part = @scanner.scan_until(/(<%)|\z/) if @skip_leading diff --git a/lib/puppet/pops/parser/evaluating_parser.rb b/lib/puppet/pops/parser/evaluating_parser.rb index 983f865122e..1b7320bee8d 100644 --- a/lib/puppet/pops/parser/evaluating_parser.rb +++ b/lib/puppet/pops/parser/evaluating_parser.rb @@ -125,7 +125,7 @@ def quote(x) # @return [String] The quoted string # def self.quote(x) - escaped = String.new('"') + escaped = '"'.dup p = nil x.each_char do |c| case p diff --git a/lib/puppet/pops/parser/pn_parser.rb b/lib/puppet/pops/parser/pn_parser.rb index ea8f5086489..9922ee6be20 100644 --- a/lib/puppet/pops/parser/pn_parser.rb +++ b/lib/puppet/pops/parser/pn_parser.rb @@ -218,7 +218,7 @@ def consume_identifier(s) def consume_string s = @pos - b = String.new + b = ''.dup loop do c = next_cp case c diff --git a/lib/puppet/pops/pn.rb b/lib/puppet/pops/pn.rb index 6dbc3ddb95c..be46c522e50 100644 --- a/lib/puppet/pops/pn.rb +++ b/lib/puppet/pops/pn.rb @@ -20,7 +20,7 @@ def ==(o) end def to_s - s = String.new + s = ''.dup format(nil, s) s end diff --git a/lib/puppet/pops/serialization/json_path.rb b/lib/puppet/pops/serialization/json_path.rb index da7ad441170..eb81e92182f 100644 --- a/lib/puppet/pops/serialization/json_path.rb +++ b/lib/puppet/pops/serialization/json_path.rb @@ -11,7 +11,7 @@ module JsonPath # # @api private def self.to_json_path(path) - p = String.new('$') + p = '$'.dup path.each do |seg| if seg.nil? p << '[null]' diff --git a/lib/puppet/pops/time/timespan.rb b/lib/puppet/pops/time/timespan.rb index b7fcd1aa092..8a37d042091 100644 --- a/lib/puppet/pops/time/timespan.rb +++ b/lib/puppet/pops/time/timespan.rb @@ -545,7 +545,7 @@ def initialize(format, segments) end def format(timespan) - bld = String.new(timespan.negative? ? '-' : '') + bld = timespan.negative? ? '-'.dup : ''.dup @segments.each { |segment| segment.append_to(bld, timespan) } bld end @@ -575,7 +575,7 @@ def regexp end def build_regexp - bld = String.new('\A-?') + bld = '\A-?'.dup @segments.each { |segment| segment.append_regexp(bld) } bld << '\z' Regexp.new(bld) @@ -613,7 +613,7 @@ def bad_format_specifier(format, start, position) def append_literal(bld, codepoint) if bld.empty? || !bld.last.is_a?(Format::LiteralSegment) - bld << Format::LiteralSegment.new(String.new.concat(codepoint)) + bld << Format::LiteralSegment.new(''.dup.concat(codepoint)) else bld.last.concat(codepoint) end diff --git a/lib/puppet/pops/types/ruby_generator.rb b/lib/puppet/pops/types/ruby_generator.rb index 49db81f757a..9523ad0f552 100644 --- a/lib/puppet/pops/types/ruby_generator.rb +++ b/lib/puppet/pops/types/ruby_generator.rb @@ -60,7 +60,7 @@ def create_class(obj) if cls.nil? rp = key.resolved_parent parent_class = rp.is_a?(PObjectType) ? rp.implementation_class : Object - class_def = String.new + class_def = ''.dup class_body(key, EMPTY_ARRAY, class_def) cls = Class.new(parent_class) cls.class_eval(class_def) @@ -109,7 +109,7 @@ def module_definition(types, comment, *impl_subst) end # Create class definition of all contained types - bld = String.new + bld = ''.dup start_module(common_prefix, comment, bld) class_names = [] names_by_prefix.each_pair do |seg_array, index_and_name_array| diff --git a/lib/puppet/pops/types/string_converter.rb b/lib/puppet/pops/types/string_converter.rb index 5d0c3976595..c2293929d5e 100644 --- a/lib/puppet/pops/types/string_converter.rb +++ b/lib/puppet/pops/types/string_converter.rb @@ -701,7 +701,7 @@ def string_PBooleanType(val_type, val, format_map, indentation) # Performs post-processing of literals to apply width and precision flags def apply_string_flags(f, literal_str) if f.left || f.width || f.prec - fmt = String.new('%') + fmt = '%'.dup fmt << '-' if f.left fmt << f.width.to_s if f.width fmt << '.' << f.prec.to_s if f.prec @@ -853,7 +853,7 @@ def puppet_quote(str, enforce_double_quotes = false) end # Assume that the string can be single quoted - bld = String.new('\'') + bld = "'".dup bld.force_encoding(str.encoding) escaped = false str.each_codepoint do |codepoint| @@ -879,12 +879,12 @@ def puppet_quote(str, enforce_double_quotes = false) # If string ended with a backslash, then that backslash must be escaped bld << 0x5c if escaped - bld << '\'' + bld << "'" bld end def puppet_double_quote(str) - bld = String.new('"') + bld = '"'.dup str.each_codepoint do |codepoint| case codepoint when 0x09 @@ -940,7 +940,7 @@ def string_PArrayType(val_type, val, format_map, indentation) case format.format when :a, :s, :p - buf = String.new + buf = ''.dup if indentation.breaks? buf << "\n" buf << indentation.padding @@ -1055,7 +1055,7 @@ def string_PHashType(val_type, val, format_map, indentation) when :h, :s, :p indentation = indentation.indenting(format.alt? || indentation.is_indenting?) - buf = String.new + buf = ''.dup if indentation.breaks? buf << "\n" buf << indentation.padding diff --git a/lib/puppet/pops/types/type_formatter.rb b/lib/puppet/pops/types/type_formatter.rb index 4af375c18c9..9ca066f1149 100644 --- a/lib/puppet/pops/types/type_formatter.rb +++ b/lib/puppet/pops/types/type_formatter.rb @@ -50,7 +50,7 @@ def ruby(ref_ctor) # @api public # def string(t) - @bld = String.new + @bld = ''.dup append_string(t) @bld end @@ -64,7 +64,7 @@ def string(t) # # @api public def indented_string(t, indent = 0, indent_width = 2) - @bld = String.new + @bld = ''.dup append_indented_string(t, indent, indent_width) @bld end diff --git a/lib/puppet/pops/types/types.rb b/lib/puppet/pops/types/types.rb index 5c6f1a0c55a..5067475f611 100644 --- a/lib/puppet/pops/types/types.rb +++ b/lib/puppet/pops/types/types.rb @@ -1713,7 +1713,7 @@ def self.append_flags_group(rx_string, options) if options == 0 rx_string else - bld = String.new('(?') + bld = '(?'.dup bld << 'i' if (options & Regexp::IGNORECASE) != 0 bld << 'm' if (options & Regexp::MULTILINE) != 0 bld << 'x' if (options & Regexp::EXTENDED) != 0 diff --git a/lib/puppet/provider/nameservice/directoryservice.rb b/lib/puppet/provider/nameservice/directoryservice.rb index ef64404a966..122f807b8e6 100644 --- a/lib/puppet/provider/nameservice/directoryservice.rb +++ b/lib/puppet/provider/nameservice/directoryservice.rb @@ -218,8 +218,8 @@ def self.set_password(resource_name, guid, password_hash) password_hash_plist = users_plist['ShadowHashData'][0] converted_hash_plist = convert_binary_to_hash(password_hash_plist) else - users_plist['ShadowHashData'] = String.new - converted_hash_plist = {'SALTED-SHA512' => String.new} + users_plist['ShadowHashData'] = ''.dup + converted_hash_plist = {'SALTED-SHA512' => ''.dup} end # converted_hash_plist['SALTED-SHA512'] expects a Base64 encoded diff --git a/lib/puppet/provider/user/directoryservice.rb b/lib/puppet/provider/user/directoryservice.rb index edcc59f6109..2cc6e21439d 100644 --- a/lib/puppet/provider/user/directoryservice.rb +++ b/lib/puppet/provider/user/directoryservice.rb @@ -637,7 +637,7 @@ def base64_decode_string(value) def set_salted_sha512(users_plist, shadow_hash_data, value) unless shadow_hash_data shadow_hash_data = Hash.new - shadow_hash_data['SALTED-SHA512'] = String.new + shadow_hash_data['SALTED-SHA512'] = ''.dup end shadow_hash_data['SALTED-SHA512'] = base64_decode_string(value) binary_plist = self.class.convert_hash_to_binary(shadow_hash_data) diff --git a/lib/puppet/reference/configuration.rb b/lib/puppet/reference/configuration.rb index e521ba8e9ef..2724a7980c9 100644 --- a/lib/puppet/reference/configuration.rb +++ b/lib/puppet/reference/configuration.rb @@ -5,7 +5,7 @@ docs[name] = object end - str = String.new + str = ''.dup docs.sort { |a, b| a[0].to_s <=> b[0].to_s }.each do |name, object| diff --git a/lib/puppet/reference/indirection.rb b/lib/puppet/reference/indirection.rb index ef3194b1457..972b9985291 100644 --- a/lib/puppet/reference/indirection.rb +++ b/lib/puppet/reference/indirection.rb @@ -5,7 +5,7 @@ require_relative '../../puppet/file_serving/metadata' reference = Puppet::Util::Reference.newreference :indirection, :doc => "Indirection types and their terminus classes" do - text = String.new + text = ''.dup Puppet::Indirector::Indirection.instances.sort_by(&:to_s).each do |indirection| ind = Puppet::Indirector::Indirection.instance(indirection) name = indirection.to_s.capitalize diff --git a/lib/puppet/reports.rb b/lib/puppet/reports.rb index a117ff802aa..0c6914126be 100644 --- a/lib/puppet/reports.rb +++ b/lib/puppet/reports.rb @@ -71,7 +71,7 @@ def self.register_report(name, options = {}, &block) # Collects the docs for all reports. # @api private def self.reportdocs - docs = String.new + docs = ''.dup # Use this method so they all get loaded instance_loader(:report).loadall(Puppet.lookup(:current_environment)) diff --git a/lib/puppet/transaction/report.rb b/lib/puppet/transaction/report.rb index c91f2b9d84d..03967827ce6 100644 --- a/lib/puppet/transaction/report.rb +++ b/lib/puppet/transaction/report.rb @@ -369,7 +369,7 @@ def name def summary report = raw_summary - ret = String.new + ret = ''.dup report.keys.sort_by(&:to_s).each do |key| ret += "#{Puppet::Util::Metric.labelize(key)}:\n" diff --git a/lib/puppet/util.rb b/lib/puppet/util.rb index c3a46ec90b3..2772b4e92a6 100644 --- a/lib/puppet/util.rb +++ b/lib/puppet/util.rb @@ -409,8 +409,7 @@ def uri_query_encode(query_string) def uri_encode(path, opts = { :allow_fragment => false }) raise ArgumentError.new(_('path may not be nil')) if path.nil? - # ensure string starts as UTF-8 for the sake of Ruby 1.9.3 - encoded = String.new.encode!(Encoding::UTF_8) + encoded = ''.dup # parse uri into named matches, then reassemble properly encoded parts = path.match(RFC_3986_URI_REGEX) @@ -454,7 +453,7 @@ def uri_encode(path, opts = { :allow_fragment => false }) def rfc2396_escape(str) str.gsub(UNSAFE) do |match| - tmp = String.new + tmp = ''.dup match.each_byte do |uc| tmp << sprintf('%%%02X', uc) end diff --git a/lib/puppet/util/diff.rb b/lib/puppet/util/diff.rb index fadfd14af41..c6fe5d767e9 100644 --- a/lib/puppet/util/diff.rb +++ b/lib/puppet/util/diff.rb @@ -34,7 +34,7 @@ def lcs_diff(data_old, data_new, format=:unified, context_lines=3) data_old = data_old.split(/\n/).map! { |e| e.chomp } data_new = data_new.split(/\n/).map! { |e| e.chomp } - output = String.new + output = ''.dup diffs = ::Diff::LCS.diff(data_old, data_new) return output if diffs.empty? diff --git a/lib/puppet/util/execution.rb b/lib/puppet/util/execution.rb index 42d1b5f51b9..14485c49aed 100644 --- a/lib/puppet/util/execution.rb +++ b/lib/puppet/util/execution.rb @@ -162,7 +162,7 @@ def self.execute(command, options = NoOptionsSpecified) # do this after processing 'command' array or string command_str = '[redacted]' if options[:sensitive] - user_log_s = String.new + user_log_s = ''.dup if options[:uid] user_log_s << " uid=#{options[:uid]}" end @@ -202,7 +202,7 @@ def self.execute(command, options = NoOptionsSpecified) stderr = options[:combine] ? stdout : Puppet::FileSystem.open(null_file, nil, 'w') exec_args = [command, options, stdin, stdout, stderr] - output = String.new + output = ''.dup # We close stdin/stdout/stderr immediately after fork/exec as they're no longer needed by # this process. In most cases they could be closed later, but when `stdout` is the "writer" diff --git a/lib/puppet/util/inifile.rb b/lib/puppet/util/inifile.rb index 1c2b9363835..ba9e2bab3ac 100644 --- a/lib/puppet/util/inifile.rb +++ b/lib/puppet/util/inifile.rb @@ -79,7 +79,7 @@ def [](key) # written to file def format if @destroy - text = String.new + text = ''.dup else text = "[#{name}]\n" @entries.each do |entry| @@ -208,7 +208,7 @@ def get_section(name) end def format - text = String.new + text = ''.dup @contents.each do |content| if content.is_a? Section diff --git a/lib/puppet/util/package/version/rpm.rb b/lib/puppet/util/package/version/rpm.rb index ae5ebcfc0b8..ad5c15681b8 100644 --- a/lib/puppet/util/package/version/rpm.rb +++ b/lib/puppet/util/package/version/rpm.rb @@ -19,7 +19,7 @@ def self.parse(ver) end def to_s - version_found = String.new + version_found = ''.dup version_found += "#{@epoch}:" if @epoch version_found += @version version_found += "-#{@release}" if @release diff --git a/lib/puppet/util/provider_features.rb b/lib/puppet/util/provider_features.rb index 3d406ef5afd..dafae92427e 100644 --- a/lib/puppet/util/provider_features.rb +++ b/lib/puppet/util/provider_features.rb @@ -76,7 +76,7 @@ def feature(name, docs, hash = {}) # @return [String] Returns a string with documentation covering all features. def featuredocs - str = String.new + str = ''.dup @features ||= {} return nil if @features.empty? names = @features.keys.sort_by(&:to_s) diff --git a/lib/puppet/util/selinux.rb b/lib/puppet/util/selinux.rb index 58f10ca22e2..2b9ade616fa 100644 --- a/lib/puppet/util/selinux.rb +++ b/lib/puppet/util/selinux.rb @@ -227,7 +227,7 @@ def get_create_mode(resource_ensure) # Internal helper function to read and parse /proc/mounts def read_mounts - mounts = String.new + mounts = ''.dup begin if File.method_defined? "read_nonblock" # If possible we use read_nonblock in a loop rather than read to work- diff --git a/lib/puppet/util/windows/access_control_entry.rb b/lib/puppet/util/windows/access_control_entry.rb index cba294eb408..acde2d4ec94 100644 --- a/lib/puppet/util/windows/access_control_entry.rb +++ b/lib/puppet/util/windows/access_control_entry.rb @@ -61,7 +61,7 @@ def object_inherit? end def inspect - inheritance = String.new + inheritance = ''.dup inheritance << '(I)' if inherited? inheritance << '(OI)' if object_inherit? inheritance << '(CI)' if container_inherit? diff --git a/lib/puppet/util/windows/access_control_list.rb b/lib/puppet/util/windows/access_control_list.rb index 06bbb8533ce..09685034214 100644 --- a/lib/puppet/util/windows/access_control_list.rb +++ b/lib/puppet/util/windows/access_control_list.rb @@ -98,7 +98,7 @@ def reassign!(old_sid, new_sid) end def inspect - str = String.new + str = ''.dup @aces.each do |ace| str << " #{ace.inspect}\n" end diff --git a/lib/puppet/util/windows/adsi.rb b/lib/puppet/util/windows/adsi.rb index e48832131a7..ac31546fba2 100644 --- a/lib/puppet/util/windows/adsi.rb +++ b/lib/puppet/util/windows/adsi.rb @@ -488,7 +488,7 @@ def expired? # UNLEN from lmcons.h - https://stackoverflow.com/a/2155176 MAX_USERNAME_LENGTH = 256 def self.current_user_name - user_name = String.new + user_name = ''.dup max_length = MAX_USERNAME_LENGTH + 1 # NULL terminated FFI::MemoryPointer.new(max_length * 2) do |buffer| # wide string FFI::MemoryPointer.new(:dword, 1) do |buffer_size| @@ -520,7 +520,7 @@ def self.current_user_name NameSurname = 14 def self.current_user_name_with_format(format) - user_name = String.new + user_name = ''.dup max_length = 1024 FFI::MemoryPointer.new(:lpwstr, max_length * 2 + 1) do |buffer| diff --git a/lib/puppet/util/windows/error.rb b/lib/puppet/util/windows/error.rb index cbe908a28ef..67b26b9465c 100644 --- a/lib/puppet/util/windows/error.rb +++ b/lib/puppet/util/windows/error.rb @@ -32,7 +32,7 @@ def self.format_error_code(code) FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK - error_string = String.new + error_string = ''.dup # this pointer actually points to a :lpwstr (pointer) since we're letting Windows allocate for us FFI::MemoryPointer.new(:pointer, 1) do |buffer_ptr| diff --git a/lib/puppet/util/windows/file.rb b/lib/puppet/util/windows/file.rb index e26f844982c..c1fcab182d2 100644 --- a/lib/puppet/util/windows/file.rb +++ b/lib/puppet/util/windows/file.rb @@ -245,7 +245,7 @@ def readlink(link_name) module_function :readlink def get_long_pathname(path) - converted = String.new + converted = ''.dup FFI::Pointer.from_string_to_wide_string(path) do |path_ptr| # includes terminating NULL buffer_size = GetLongPathNameW(path_ptr, FFI::Pointer::NULL, 0) @@ -263,7 +263,7 @@ def get_long_pathname(path) module_function :get_long_pathname def get_short_pathname(path) - converted = String.new + converted = ''.dup FFI::Pointer.from_string_to_wide_string(path) do |path_ptr| # includes terminating NULL buffer_size = GetShortPathNameW(path_ptr, FFI::Pointer::NULL, 0) diff --git a/lib/puppet/util/windows/process.rb b/lib/puppet/util/windows/process.rb index fee52fb3916..efbdae2d029 100644 --- a/lib/puppet/util/windows/process.rb +++ b/lib/puppet/util/windows/process.rb @@ -121,7 +121,7 @@ def with_process_token(access, &block) module_function :with_process_token def get_process_image_name_by_pid(pid) - image_name = String.new + image_name = ''.dup Puppet::Util::Windows::Security.with_privilege(Puppet::Util::Windows::Security::SE_DEBUG_NAME) do open_process(PROCESS_QUERY_INFORMATION, false, pid) do |phandle|