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

+1-1
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

+1-1
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

+2-2
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

+2-2
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

+1-1
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

+1-1
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

+1-1
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

+1-1
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

+1-1
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

+3-3
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"

lib/puppet/network/http/memory_response.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class Puppet::Network::HTTP::MemoryResponse
33
attr_reader :code, :type, :body
44

55
def initialize
6-
@body = String.new
6+
@body = ''.dup
77
end
88

99
def respond_with(code, type, body)

lib/puppet/parameter/value_collection.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def aliasvalue(name, other)
3131
#
3232
def doc
3333
unless defined?(@doc)
34-
@doc = String.new
34+
@doc = ''.dup
3535
unless values.empty?
3636
@doc << "Valid values are "
3737
@doc << @strings.collect do |value|

lib/puppet/parser/functions.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ def self.function(name, environment = Puppet.lookup(:current_environment))
258258
def self.functiondocs(environment = Puppet.lookup(:current_environment))
259259
autoloader.delegatee.loadall(environment)
260260

261-
ret = String.new
261+
ret = ''.dup
262262

263263
merged_functions(environment).sort { |a,b| a[0].to_s <=> b[0].to_s }.each do |name, hash|
264264
ret << "#{name}\n#{"-" * name.to_s.length}\n"

lib/puppet/pops/loader/loader_paths.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def effective_path(typed_name, start_index_in_name)
8888

8989
def typed_name(type, name_authority, relative_path, module_name)
9090
# Module name is assumed to be included in the path and therefore not added here
91-
n = String.new
91+
n = ''.dup
9292
unless extension.empty?
9393
# Remove extension
9494
relative_path = relative_path[0..-(extension.length+1)]
@@ -153,7 +153,7 @@ def effective_path(typed_name, start_index_in_name)
153153
end
154154

155155
def typed_name(type, name_authority, relative_path, module_name)
156-
n = String.new
156+
n = ''.dup
157157
n << module_name unless module_name.nil?
158158
unless extension.empty?
159159
# Remove extension
@@ -249,7 +249,7 @@ def relative_path
249249
end
250250

251251
def typed_name(type, name_authority, relative_path, module_name)
252-
n = String.new
252+
n = ''.dup
253253
n << module_name unless module_name.nil?
254254

255255
# 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)
351351
if @init_filenames.include?(relative_path) && !(module_name.nil? || module_name.empty?)
352352
TypedName.new(type, module_name, name_authority)
353353
else
354-
n = String.new
354+
n = ''.dup
355355
n << module_name unless module_name.nil?
356356
ext = @extensions.find { |extension| relative_path.end_with?(extension) }
357357
relative_path = relative_path[0..-(ext.length+1)]

lib/puppet/pops/lookup/explainer.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def to_hash
2020
end
2121

2222
def explain
23-
io = String.new
23+
io = ''.dup
2424
dump_on(io, '', '')
2525
io
2626
end

lib/puppet/pops/lookup/hiera_config.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ def find_line_matching(regexp, start_line = 1)
228228
line_number += 1
229229
next if line_number < start_line
230230
quote = nil
231-
stripped = String.new
231+
stripped = ''.dup
232232
line.each_codepoint do |cp|
233233
if cp == 0x22 || cp == 0x27 # double or single quote
234234
if quote == cp

lib/puppet/pops/model/factory.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,7 @@ def is_interop_rewriteable?(o)
11121112
end
11131113

11141114
def self.concat(*args)
1115-
result = String.new
1115+
result = ''.dup
11161116
args.each do |e|
11171117
if e.instance_of?(Factory) && e.model_class <= LiteralString
11181118
result << e[KEY_VALUE]

lib/puppet/pops/model/tree_dumper.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def indent
2121
end
2222

2323
def format(x)
24-
result = String.new
24+
result = ''.dup
2525
parts = format_r(x)
2626
parts.each_index do |i|
2727
if i > 0

lib/puppet/pops/parser/epp_support.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def scan(skip_leading=false)
183183
@skip_leading = skip_leading
184184

185185
return nil if scanner.eos?
186-
s = String.new
186+
s = ''.dup
187187
until scanner.eos?
188188
part = @scanner.scan_until(/(<%)|\z/)
189189
if @skip_leading

lib/puppet/pops/parser/evaluating_parser.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def quote(x)
125125
# @return [String] The quoted string
126126
#
127127
def self.quote(x)
128-
escaped = String.new('"')
128+
escaped = '"'.dup
129129
p = nil
130130
x.each_char do |c|
131131
case p

lib/puppet/pops/parser/pn_parser.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ def consume_identifier(s)
218218

219219
def consume_string
220220
s = @pos
221-
b = String.new
221+
b = ''.dup
222222
loop do
223223
c = next_cp
224224
case c

lib/puppet/pops/pn.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def ==(o)
2020
end
2121

2222
def to_s
23-
s = String.new
23+
s = ''.dup
2424
format(nil, s)
2525
s
2626
end

lib/puppet/pops/serialization/json_path.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module JsonPath
1111
#
1212
# @api private
1313
def self.to_json_path(path)
14-
p = String.new('$')
14+
p = '$'.dup
1515
path.each do |seg|
1616
if seg.nil?
1717
p << '[null]'

lib/puppet/pops/time/timespan.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ def initialize(format, segments)
545545
end
546546

547547
def format(timespan)
548-
bld = String.new(timespan.negative? ? '-' : '')
548+
bld = timespan.negative? ? '-'.dup : ''.dup
549549
@segments.each { |segment| segment.append_to(bld, timespan) }
550550
bld
551551
end
@@ -575,7 +575,7 @@ def regexp
575575
end
576576

577577
def build_regexp
578-
bld = String.new('\A-?')
578+
bld = '\A-?'.dup
579579
@segments.each { |segment| segment.append_regexp(bld) }
580580
bld << '\z'
581581
Regexp.new(bld)
@@ -613,7 +613,7 @@ def bad_format_specifier(format, start, position)
613613

614614
def append_literal(bld, codepoint)
615615
if bld.empty? || !bld.last.is_a?(Format::LiteralSegment)
616-
bld << Format::LiteralSegment.new(String.new.concat(codepoint))
616+
bld << Format::LiteralSegment.new(''.dup.concat(codepoint))
617617
else
618618
bld.last.concat(codepoint)
619619
end

lib/puppet/pops/types/ruby_generator.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def create_class(obj)
6060
if cls.nil?
6161
rp = key.resolved_parent
6262
parent_class = rp.is_a?(PObjectType) ? rp.implementation_class : Object
63-
class_def = String.new
63+
class_def = ''.dup
6464
class_body(key, EMPTY_ARRAY, class_def)
6565
cls = Class.new(parent_class)
6666
cls.class_eval(class_def)
@@ -109,7 +109,7 @@ def module_definition(types, comment, *impl_subst)
109109
end
110110

111111
# Create class definition of all contained types
112-
bld = String.new
112+
bld = ''.dup
113113
start_module(common_prefix, comment, bld)
114114
class_names = []
115115
names_by_prefix.each_pair do |seg_array, index_and_name_array|

lib/puppet/pops/types/string_converter.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ def string_PBooleanType(val_type, val, format_map, indentation)
701701
# Performs post-processing of literals to apply width and precision flags
702702
def apply_string_flags(f, literal_str)
703703
if f.left || f.width || f.prec
704-
fmt = String.new('%')
704+
fmt = '%'.dup
705705
fmt << '-' if f.left
706706
fmt << f.width.to_s if f.width
707707
fmt << '.' << f.prec.to_s if f.prec
@@ -853,7 +853,7 @@ def puppet_quote(str, enforce_double_quotes = false)
853853
end
854854

855855
# Assume that the string can be single quoted
856-
bld = String.new('\'')
856+
bld = "'".dup
857857
bld.force_encoding(str.encoding)
858858
escaped = false
859859
str.each_codepoint do |codepoint|
@@ -879,12 +879,12 @@ def puppet_quote(str, enforce_double_quotes = false)
879879
# If string ended with a backslash, then that backslash must be escaped
880880
bld << 0x5c if escaped
881881

882-
bld << '\''
882+
bld << "'"
883883
bld
884884
end
885885

886886
def puppet_double_quote(str)
887-
bld = String.new('"')
887+
bld = '"'.dup
888888
str.each_codepoint do |codepoint|
889889
case codepoint
890890
when 0x09
@@ -940,7 +940,7 @@ def string_PArrayType(val_type, val, format_map, indentation)
940940

941941
case format.format
942942
when :a, :s, :p
943-
buf = String.new
943+
buf = ''.dup
944944
if indentation.breaks?
945945
buf << "\n"
946946
buf << indentation.padding
@@ -1055,7 +1055,7 @@ def string_PHashType(val_type, val, format_map, indentation)
10551055

10561056
when :h, :s, :p
10571057
indentation = indentation.indenting(format.alt? || indentation.is_indenting?)
1058-
buf = String.new
1058+
buf = ''.dup
10591059
if indentation.breaks?
10601060
buf << "\n"
10611061
buf << indentation.padding

lib/puppet/pops/types/type_formatter.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def ruby(ref_ctor)
5050
# @api public
5151
#
5252
def string(t)
53-
@bld = String.new
53+
@bld = ''.dup
5454
append_string(t)
5555
@bld
5656
end
@@ -64,7 +64,7 @@ def string(t)
6464
#
6565
# @api public
6666
def indented_string(t, indent = 0, indent_width = 2)
67-
@bld = String.new
67+
@bld = ''.dup
6868
append_indented_string(t, indent, indent_width)
6969
@bld
7070
end

lib/puppet/pops/types/types.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1713,7 +1713,7 @@ def self.append_flags_group(rx_string, options)
17131713
if options == 0
17141714
rx_string
17151715
else
1716-
bld = String.new('(?')
1716+
bld = '(?'.dup
17171717
bld << 'i' if (options & Regexp::IGNORECASE) != 0
17181718
bld << 'm' if (options & Regexp::MULTILINE) != 0
17191719
bld << 'x' if (options & Regexp::EXTENDED) != 0

0 commit comments

Comments
 (0)