Skip to content

Commit 15843f6

Browse files
committed
Avoid deduplicating tainted strings or ones with instance variables.
1 parent 64d7478 commit 15843f6

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

spec/ruby/core/string/uminus_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,20 @@
4242
(-dynamic).should_not equal("this string is frozen".freeze)
4343
(-dynamic).should_not equal(-"this string is frozen".freeze)
4444
end
45+
46+
it "does not deduplicate tainted strings" do
47+
dynamic = %w(this string is frozen).join(' ')
48+
dynamic.taint
49+
(-dynamic).should_not equal("this string is frozen".freeze)
50+
(-dynamic).should_not equal(-"this string is frozen".freeze)
51+
end
52+
53+
it "does not deduplicate strings with additional instance variables" do
54+
dynamic = %w(this string is frozen).join(' ')
55+
dynamic.instance_variable_set(:@foo, :bar)
56+
(-dynamic).should_not equal("this string is frozen".freeze)
57+
(-dynamic).should_not equal(-"this string is frozen".freeze)
58+
end
4559
end
4660

4761
ruby_version_is "2.6" do

src/main/ruby/core/string.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,8 +1591,10 @@ def +@
15911591

15921592
def -@
15931593
str = frozen? ? self : dup.freeze
1594-
Truffle::Ropes.flatten_rope(str)
1595-
Truffle.invoke_primitive(:string_intern, str)
1594+
unless str.tainted? || !(str.instance_variables).empty?
1595+
Truffle::Ropes.flatten_rope(str)
1596+
Truffle.invoke_primitive(:string_intern, str)
1597+
end
15961598
end
15971599

15981600
def encoding

0 commit comments

Comments
 (0)