diff --git a/lib/erb.rb b/lib/erb.rb index 2adc5e0e58..df9a1e910d 100644 --- a/lib/erb.rb +++ b/lib/erb.rb @@ -833,6 +833,7 @@ class ERB @src, @encoding, @frozen_string = *compiler.compile(str) @filename = nil @lineno = 0 + @_init = self.class.singleton_class end NOT_GIVEN = Object.new private_constant :NOT_GIVEN @@ -891,6 +892,9 @@ class ERB # code evaluation. # def result(b=new_toplevel) + unless @_init.equal?(self.class.singleton_class) + raise ArgumentError, "not initialized" + end if @safe_level proc do prev_safe_level = $SAFE diff --git a/test/erb/test_erb.rb b/test/erb/test_erb.rb index 9af7e11ef6..16ecf84105 100644 --- a/test/erb/test_erb.rb +++ b/test/erb/test_erb.rb @@ -687,6 +687,19 @@ EOS end end end + + def test_prohibited_marshal_dump + erb = ERB.new("") + assert_raise(TypeError) {Marshal.dump(erb)} + end + + def test_prohibited_marshal_load + erb = ERB.allocate + erb.instance_variable_set(:@src, "") + erb.instance_variable_set(:@lineno, 1) + erb = Marshal.load(Marshal.dump(erb)) + assert_raise(ArgumentError) {erb.result} + end end class TestERBCoreWOStrScan < TestERBCore