summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeremy Evans <code@jeremyevans.net>2019-06-14 16:53:42 -0700
committerJeremy Evans <code@jeremyevans.net>2019-07-01 12:39:06 -0700
commitf53d7e4bfd604be6f8131c5460c29f4af16da117 (patch)
treec971256baaed293e95d0a381bc524e6ec2113199
parent93328b5237c515878dacfa7350688b016333225d (diff)
downloadruby-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.c15
-rw-r--r--test/ruby/test_env.rb4
2 files changed, 19 insertions, 0 deletions
diff --git a/hash.c b/hash.c
index 8af5be688e..be4fc216d4 100644
--- a/hash.c
+++ b/hash.c
@@ -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?)