diff options
author | Jeremy Evans <code@jeremyevans.net> | 2019-06-14 16:53:42 -0700 |
---|---|---|
committer | Jeremy Evans <code@jeremyevans.net> | 2019-07-01 12:39:06 -0700 |
commit | f53d7e4bfd604be6f8131c5460c29f4af16da117 (patch) | |
tree | c971256baaed293e95d0a381bc524e6ec2113199 | |
parent | 93328b5237c515878dacfa7350688b016333225d (diff) | |
download | ruby-f53d7e4bfd604be6f8131c5460c29f4af16da117.tar.gz |
Raise TypeError if calling ENV.freeze
Previously, you could call ENV.freeze, but it would not have
the desired effect, as you could still modify ENV.
Fixes [Bug #15920]
-rw-r--r-- | hash.c | 15 | ||||
-rw-r--r-- | test/ruby/test_env.rb | 4 |
2 files changed, 19 insertions, 0 deletions
@@ -5701,6 +5701,20 @@ env_reject(void) /* * call-seq: + * ENV.freeze -> raises TypeError + * + * Ruby does not allow ENV to be frozen, so calling ENV.freeze + * raises TypeError. + */ +static VALUE +env_freeze(VALUE self) +{ + rb_raise(rb_eTypeError, "cannot freeze ENV"); + return self; /* Not reached */ +} + +/* + * call-seq: * ENV.shift -> Array or nil * * Removes an environment variable name-value pair from ENV and returns it as @@ -6058,6 +6072,7 @@ Init_Hash(void) rb_define_singleton_method(envtbl, "filter", env_select, 0); rb_define_singleton_method(envtbl, "filter!", env_select_bang, 0); rb_define_singleton_method(envtbl, "shift", env_shift, 0); + rb_define_singleton_method(envtbl, "freeze", env_freeze, 0); rb_define_singleton_method(envtbl, "invert", env_invert, 0); rb_define_singleton_method(envtbl, "replace", env_replace, 1); rb_define_singleton_method(envtbl, "update", env_update, 1); diff --git a/test/ruby/test_env.rb b/test/ruby/test_env.rb index 6343642ac1..e8ae762d03 100644 --- a/test/ruby/test_env.rb +++ b/test/ruby/test_env.rb @@ -497,6 +497,10 @@ class TestEnv < Test::Unit::TestCase end def test_frozen + assert_raise(TypeError) { ENV.freeze } + end + + def test_frozen ENV[PATH_ENV] = "/" ENV.each do |k, v| assert_predicate(k, :frozen?) |