Skip to content

Commit 244b38a

Browse files
committed
Fix double-quoted YAML escape handling.
This leads installation issue of Windows.
1 parent 9bb8f15 commit 244b38a

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

lib/rubygems/yaml_serializer.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,15 @@ def coerce(val)
255255
val = val.sub(/^! /, "") if val.start_with?("! ")
256256

257257
if val =~ /^"(.*)"$/
258-
$1.gsub(/\\"/, '"').gsub(/\\n/, "\n").gsub(/\\r/, "\r").gsub(/\\t/, "\t").gsub(/\\\\/, "\\")
258+
$1.gsub(/\\["nrt\\]/) do |m|
259+
case m
260+
when '\\"' then '"'
261+
when "\\n" then "\n"
262+
when "\\r" then "\r"
263+
when "\\t" then "\t"
264+
when "\\\\" then "\\"
265+
end
266+
end
259267
elsif val =~ /^'(.*)'$/
260268
$1.gsub(/''/, "'")
261269
elsif val == "true"

test/rubygems/test_gem_safe_yaml.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,26 @@ def test_load_double_quoted_escape_sequences
751751
assert_equal "say\"hi\"", result["quote"]
752752
end
753753

754+
def test_load_double_quoted_backslash_before_escape_chars
755+
pend "Psych mode" if Gem.use_psych?
756+
757+
# \\r in YAML should become literal backslash + r, not carriage return
758+
result = Gem::YAMLSerializer.load('path: "D:\\\\ruby-mswin\\\\lib"')
759+
assert_equal "D:\\ruby-mswin\\lib", result["path"]
760+
761+
# \\n should become literal backslash + n, not newline
762+
result = Gem::YAMLSerializer.load('path: "C:\\\\new_folder"')
763+
assert_equal "C:\\new_folder", result["path"]
764+
765+
# \\t should become literal backslash + t, not tab
766+
result = Gem::YAMLSerializer.load('path: "C:\\\\tmp\\\\test"')
767+
assert_equal "C:\\tmp\\test", result["path"]
768+
769+
# \\\\ should become two literal backslashes
770+
result = Gem::YAMLSerializer.load('val: "a\\\\\\\\b"')
771+
assert_equal "a\\\\b", result["val"]
772+
end
773+
754774
def test_load_single_quoted_escape
755775
pend "Psych mode" if Gem.use_psych?
756776

0 commit comments

Comments
 (0)