From 98ef38ada43338c073f50a0093196f0356284625 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Wed, 27 Nov 2019 11:40:18 +0000 Subject: Freeze Regexp literals [Feature #8948] [Feature #16377] Since Regexp literals always reference the same instance, allowing to mutate them can lead to state leak. --- spec/ruby/core/marshal/dump_spec.rb | 4 ++-- spec/ruby/core/marshal/shared/load.rb | 2 +- spec/ruby/core/regexp/initialize_spec.rb | 12 ++++++++++-- spec/ruby/core/string/match_spec.rb | 2 +- spec/ruby/language/regexp_spec.rb | 6 ++++++ spec/ruby/optional/capi/encoding_spec.rb | 6 +++--- 6 files changed, 23 insertions(+), 9 deletions(-) (limited to 'spec') diff --git a/spec/ruby/core/marshal/dump_spec.rb b/spec/ruby/core/marshal/dump_spec.rb index fc78ca4ff9..ea021991a8 100644 --- a/spec/ruby/core/marshal/dump_spec.rb +++ b/spec/ruby/core/marshal/dump_spec.rb @@ -235,13 +235,13 @@ describe "Marshal.dump" do end it "dumps a Regexp with instance variables" do - o = // + o = Regexp.new("") o.instance_variable_set(:@ivar, :ivar) Marshal.dump(o).should == "\x04\bI/\x00\x00\a:\x06EF:\n@ivar:\tivar" end it "dumps an extended Regexp" do - Marshal.dump(//.extend(Meths)).should == "\x04\bIe:\nMeths/\x00\x00\x06:\x06EF" + Marshal.dump(Regexp.new("").extend(Meths)).should == "\x04\bIe:\nMeths/\x00\x00\x06:\x06EF" end it "dumps a Regexp subclass" do diff --git a/spec/ruby/core/marshal/shared/load.rb b/spec/ruby/core/marshal/shared/load.rb index 302d3d5bda..78fc9fb286 100644 --- a/spec/ruby/core/marshal/shared/load.rb +++ b/spec/ruby/core/marshal/shared/load.rb @@ -623,7 +623,7 @@ describe :marshal_load, shared: true do describe "for a Regexp" do it "loads an extended Regexp" do - obj = /[a-z]/.extend(Meths, MethsMore) + obj = /[a-z]/.dup.extend(Meths, MethsMore) new_obj = Marshal.send(@method, "\004\be:\nMethse:\016MethsMore/\n[a-z]\000") new_obj.should == obj diff --git a/spec/ruby/core/regexp/initialize_spec.rb b/spec/ruby/core/regexp/initialize_spec.rb index 36fd5c7bf2..ae188fb9c2 100644 --- a/spec/ruby/core/regexp/initialize_spec.rb +++ b/spec/ruby/core/regexp/initialize_spec.rb @@ -5,8 +5,16 @@ describe "Regexp#initialize" do Regexp.should have_private_method(:initialize) end - it "raises a SecurityError on a Regexp literal" do - -> { //.send(:initialize, "") }.should raise_error(SecurityError) + ruby_version_is ""..."2.7" do + it "raises a SecurityError on a Regexp literal" do + -> { //.send(:initialize, "") }.should raise_error(SecurityError) + end + end + + ruby_version_is "2.7" do + it "raises a FrozenError on a Regexp literal" do + -> { //.send(:initialize, "") }.should raise_error(FrozenError) + end end it "raises a TypeError on an initialized non-literal Regexp" do diff --git a/spec/ruby/core/string/match_spec.rb b/spec/ruby/core/string/match_spec.rb index 78b94baa44..5e988f34ca 100644 --- a/spec/ruby/core/string/match_spec.rb +++ b/spec/ruby/core/string/match_spec.rb @@ -137,7 +137,7 @@ describe "String#match" do end it "calls match on the regular expression" do - regexp = /./ + regexp = /./.dup regexp.should_receive(:match).and_return(:foo) 'hello'.match(regexp).should == :foo end diff --git a/spec/ruby/language/regexp_spec.rb b/spec/ruby/language/regexp_spec.rb index 67c7c034e9..5881afe6f1 100644 --- a/spec/ruby/language/regexp_spec.rb +++ b/spec/ruby/language/regexp_spec.rb @@ -18,6 +18,12 @@ describe "Literal Regexps" do /Hello/.should be_kind_of(Regexp) end + ruby_version_is "2.7" do + it "is frozen" do + /Hello/.frozen?.should == true + end + end + it "caches the Regexp object" do rs = [] 2.times do |i| diff --git a/spec/ruby/optional/capi/encoding_spec.rb b/spec/ruby/optional/capi/encoding_spec.rb index 857e421ddb..b74a360760 100644 --- a/spec/ruby/optional/capi/encoding_spec.rb +++ b/spec/ruby/optional/capi/encoding_spec.rb @@ -312,7 +312,7 @@ describe "C-API Encoding function" do end it "sets the encoding of a Regexp to that of the second argument" do - @s.rb_enc_copy(/regexp/, @obj).encoding.should == Encoding::US_ASCII + @s.rb_enc_copy(/regexp/.dup, @obj).encoding.should == Encoding::US_ASCII end end @@ -363,7 +363,7 @@ describe "C-API Encoding function" do end it "sets the encoding of a Regexp to the encoding" do - @s.rb_enc_associate(/regexp/, "BINARY").encoding.should == Encoding::BINARY + @s.rb_enc_associate(/regexp/.dup, "BINARY").encoding.should == Encoding::BINARY end it "sets the encoding of a String to a default when the encoding is NULL" do @@ -380,7 +380,7 @@ describe "C-API Encoding function" do it "sets the encoding of a Regexp to the encoding" do index = @s.rb_enc_find_index("UTF-8") - enc = @s.rb_enc_associate_index(/regexp/, index).encoding + enc = @s.rb_enc_associate_index(/regexp/.dup, index).encoding enc.should == Encoding::UTF_8 end -- cgit v1.2.3