Skip to content

Commit fb20023

Browse files
committed
(PUP-11841) Fix encoding of empty String
In 2077971 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: voxpupuli/puppet-bacula#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.
1 parent 5e1cea2 commit fb20023

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+69
-70
lines changed

lib/puppet/application/doc.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def rdoc
152152
end
153153

154154
def other
155-
text = String.new
155+
text = ''.dup
156156
with_contents = options[:references].length <= 1
157157
exit_code = 0
158158
require_relative '../../puppet/util/reference'

lib/puppet/face/config.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
end
8383

8484
when_rendering :console do |to_be_rendered|
85-
output = String.new
85+
output = ''.dup
8686
if to_be_rendered.keys.length > 1
8787
to_be_rendered.keys.sort.each do |setting|
8888
output << "#{setting} = #{to_be_rendered[setting]}\n"

lib/puppet/face/epp.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@
367367
end
368368

369369
def dump_parse(source, filename, options, show_filename = true)
370-
output = String.new
370+
output = ''.dup
371371
evaluating_parser = Puppet::Pops::Parser::EvaluatingParser::EvaluatingEppParser.new
372372
begin
373373
if options[:validate]
@@ -451,7 +451,7 @@ def render_inline(epp_source, compiler, options)
451451

452452
def render_file(epp_template_name, compiler, options, show_filename, file_nbr)
453453
template_args = get_values(compiler, options)
454-
output = String.new
454+
output = ''.dup
455455
begin
456456
if show_filename && options[:header]
457457
output << "\n" unless file_nbr == 1

lib/puppet/face/module/list.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
environment = result[:environment]
7575
modules_by_path = result[:modules_by_path]
7676

77-
output = String.new
77+
output = ''.dup
7878

7979
warn_unmet_dependencies(environment)
8080

@@ -248,7 +248,7 @@ def list_build_tree(list, ancestors=[], parent=nil, params={})
248248
# Returns a Hash
249249
#
250250
def list_build_node(mod, parent, params)
251-
str = String.new
251+
str = ''.dup
252252
str << (mod.forge_name ? mod.forge_name.tr('/', '-') : mod.name)
253253
str << ' (' + colorize(:cyan, mod.version ? "v#{mod.version}" : '???') + ')'
254254

lib/puppet/face/parser.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@
174174
end
175175

176176
def dump_parse(source, filename, options, show_filename = true)
177-
output = String.new
177+
output = ''.dup
178178
evaluating_parser = Puppet::Pops::Parser::EvaluatingParser.new
179179
begin
180180
if options[:validate]

lib/puppet/indirector/facts/facter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def self.setup_external_search_paths(request)
105105

106106
def find_with_options(request)
107107
options = request.options
108-
options_for_facter = String.new
108+
options_for_facter = ''.dup
109109
options_for_facter += options[:user_query].join(' ')
110110
options_for_facter += " --config #{options[:config_file]}" if options[:config_file]
111111
options_for_facter += " --show-legacy" if options[:show_legacy]

lib/puppet/indirector/file_bucket_file/file.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def list(request)
5858
end
5959
# Setting hash's default value to [], needed by the following loop
6060
bucket = Hash.new {[]}
61-
msg = String.new
61+
msg = ''.dup
6262
# Get all files with mtime between 'from' and 'to'
6363
Pathname.new(request.options[:bucket_path]).find { |item|
6464
if item.file? and item.basename.to_s == "paths"

lib/puppet/indirector/indirection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def expiration
8181

8282
# Generate the full doc string.
8383
def doc
84-
text = String.new
84+
text = ''.dup
8585

8686
text << scrub(@doc) << "\n\n" if @doc
8787

lib/puppet/module_tool.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def self.is_module_root?(path)
7070
# Builds a formatted tree from a list of node hashes containing +:text+
7171
# and +:dependencies+ keys.
7272
def self.format_tree(nodes, level = 0)
73-
str = String.new
73+
str = ''.dup
7474
nodes.each_with_index do |node, i|
7575
last_node = nodes.length - 1 == i
7676
deps = node[:dependencies] || []

lib/puppet/network/formats.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def render(datum)
156156

157157
# Simple hash to table
158158
if datum.is_a?(Hash) && datum.keys.all? { |x| x.is_a?(String) || x.is_a?(Numeric) }
159-
output = String.new
159+
output = ''.dup
160160
column_a = datum.empty? ? 2 : datum.map{ |k,v| k.to_s.length }.max + 2
161161
datum.sort_by { |k,v| k.to_s } .each do |key, value|
162162
output << key.to_s.ljust(column_a)
@@ -169,7 +169,7 @@ def render(datum)
169169

170170
# Print one item per line for arrays
171171
if datum.is_a? Array
172-
output = String.new
172+
output = ''.dup
173173
datum.each do |item|
174174
output << item.to_s
175175
output << "\n"
@@ -227,7 +227,7 @@ def flatten_array(array)
227227
end
228228

229229
def construct_output(data)
230-
output = String.new
230+
output = ''.dup
231231
data.each do |key, value|
232232
output << "#{key}=#{value}"
233233
output << "\n"

0 commit comments

Comments
 (0)