[前][次][番号順一覧][スレッド一覧]

ruby-changes:34457

From: nobu <ko1@a...>
Date: Tue, 24 Jun 2014 10:50:17 +0900 (JST)
Subject: [ruby-changes:34457] nobu:r46538 (trunk): hash.c: frozen PATH env

nobu	2014-06-24 10:50:09 +0900 (Tue, 24 Jun 2014)

  New Revision: 46538

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=46538

  Log:
    hash.c: frozen PATH env
    
    * hash.c (env_path_str_new): make PATH environment variable
      string, to be frozen.

  Modified files:
    trunk/ChangeLog
    trunk/hash.c
    trunk/test/ruby/test_env.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 46537)
+++ ChangeLog	(revision 46538)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Tue Jun 24 10:50:06 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* hash.c (env_path_str_new): make PATH environment variable
+	  string, to be frozen.
+
 Tue Jun 24 10:40:52 2014  SHIBATA Hiroshi  <shibata.hiroshi@g...>
 
 	* tool/make-snapshot: download bundle gems when package making.
Index: hash.c
===================================================================
--- hash.c	(revision 46537)
+++ hash.c	(revision 46538)
@@ -2514,6 +2514,20 @@ env_str_new(const char *ptr, long len) https://github.com/ruby/ruby/blob/trunk/hash.c#L2514
 }
 
 static VALUE
+env_path_str_new(const char *ptr)
+{
+#ifdef _WIN32
+    VALUE str = rb_enc_str_new_cstr(ptr, rb_utf8_encoding());
+    str = rb_str_conv_enc(str, NULL, rb_filesystem_encoding());
+#else
+    VALUE str = rb_filesystem_str_new_cstr(ptr);
+#endif
+
+    rb_obj_freeze(str);
+    return str;
+}
+
+static VALUE
 env_str_new2(const char *ptr)
 {
     if (!ptr) return Qnil;
@@ -2584,14 +2598,7 @@ rb_f_getenv(VALUE obj, VALUE name) https://github.com/ruby/ruby/blob/trunk/hash.c#L2598
     env = getenv(nam);
     if (env) {
 	if (ENVMATCH(nam, PATH_ENV) && !env_path_tainted(env)) {
-#ifdef _WIN32
-	    VALUE str = rb_str_conv_enc(rb_str_new(env, strlen(env)), rb_utf8_encoding(), rb_filesystem_encoding());
-#else
-	    VALUE str = rb_filesystem_str_new_cstr(env);
-#endif
-
-	    rb_obj_freeze(str);
-	    return str;
+	    return env_path_str_new(env);
 	}
 	return env_str_new2(env);
     }
@@ -2638,11 +2645,7 @@ env_fetch(int argc, VALUE *argv) https://github.com/ruby/ruby/blob/trunk/hash.c#L2645
 	return if_none;
     }
     if (ENVMATCH(nam, PATH_ENV) && !env_path_tainted(env))
-#ifdef _WIN32
-	return rb_str_conv_enc(rb_str_new(env, strlen(env)), rb_utf8_encoding(), rb_filesystem_encoding());
-#else
-	return rb_filesystem_str_new_cstr(env);
-#endif
+	return env_path_str_new(env);
     return env_str_new2(env);
 }
 
Index: test/ruby/test_env.rb
===================================================================
--- test/ruby/test_env.rb	(revision 46537)
+++ test/ruby/test_env.rb	(revision 46538)
@@ -408,4 +408,22 @@ class TestEnv < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_env.rb#L408
       keys.each {|k| ENV.delete(k)}
     end
   end
+
+  def test_frozen
+    ENV[PATH_ENV] = "/"
+    ENV.each do |k, v|
+      assert_predicate(k, :frozen?)
+      assert_predicate(v, :frozen?)
+    end
+    ENV.each_key do |k|
+      assert_predicate(k, :frozen?)
+    end
+    ENV.each_value do |v|
+      assert_predicate(v, :frozen?)
+    end
+    ENV.each_key do |k|
+      assert_predicate(ENV[k], :frozen?, "[#{k.dump}]")
+      assert_predicate(ENV.fetch(k), :frozen?, "fetch(#{k.dump})")
+    end
+  end
 end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]