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

ruby-changes:43747

From: nobu <ko1@a...>
Date: Sat, 6 Aug 2016 00:39:53 +0900 (JST)
Subject: [ruby-changes:43747] nobu:r55819 (trunk): hash.c: env_name_new

nobu	2016-08-06 00:39:46 +0900 (Sat, 06 Aug 2016)

  New Revision: 55819

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

  Log:
    hash.c: env_name_new
    
    * hash.c (env_enc_str_new): make string for an environment
      variable name or value.
    * hash.c (env_name_new): make environment value string with the
      encoding for its name.

  Modified files:
    trunk/ChangeLog
    trunk/hash.c
    trunk/test/ruby/test_env.rb
Index: test/ruby/test_env.rb
===================================================================
--- test/ruby/test_env.rb	(revision 55818)
+++ test/ruby/test_env.rb	(revision 55819)
@@ -46,6 +46,7 @@ class TestEnv < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_env.rb#L46
     end
     ENV['TEST'] = 'bar'
     assert_equal('bar', ENV['TEST'])
+    assert_predicate(ENV['TEST'], :tainted?)
     if IGNORE_CASE
       assert_equal('bar', ENV['test'])
     else
@@ -112,6 +113,7 @@ class TestEnv < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_env.rb#L113
     assert_invalid_env {|v| ENV[v]}
     ENV[PATH_ENV] = ""
     assert_equal("", ENV[PATH_ENV])
+    assert_predicate(ENV[PATH_ENV], :tainted?)
     assert_nil(ENV[""])
   end
 
@@ -130,6 +132,7 @@ class TestEnv < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_env.rb#L132
     assert_nothing_raised { ENV.fetch(PATH_ENV, "foo") }
     ENV[PATH_ENV] = ""
     assert_equal("", ENV.fetch(PATH_ENV))
+    assert_predicate(ENV.fetch(PATH_ENV), :tainted?)
   end
 
   def test_aset
Index: hash.c
===================================================================
--- hash.c	(revision 55818)
+++ hash.c	(revision 55819)
@@ -2940,30 +2940,29 @@ env_str_transcode(VALUE str, rb_encoding https://github.com/ruby/ruby/blob/trunk/hash.c#L2940
 #endif
 
 static VALUE
-env_str_new(const char *ptr, long len)
+env_enc_str_new(const char *ptr, long len, rb_encoding *enc)
 {
 #ifdef _WIN32
-    VALUE str = env_str_transcode(rb_utf8_str_new(ptr, len), rb_locale_encoding());
-    OBJ_TAINT(str); /* rb_locale_str_new makes tainted string, but rb_utf8_str_new doesn't */
+    VALUE str = env_str_transcode(rb_utf8_str_new(ptr, len), enc);
 #else
-    VALUE str = rb_locale_str_new(ptr, len);
+    VALUE str = rb_external_str_new_with_enc(ptr, len, enc);
 #endif
 
+    OBJ_TAINT(str);
     rb_obj_freeze(str);
     return str;
 }
 
 static VALUE
-env_path_str_new(const char *ptr)
+env_enc_str_new_cstr(const char *ptr, rb_encoding *enc)
 {
-#ifdef _WIN32
-    VALUE str = env_str_transcode(rb_utf8_str_new_cstr(ptr), rb_filesystem_encoding());
-#else
-    VALUE str = rb_filesystem_str_new_cstr(ptr);
-#endif
+    return env_enc_str_new(ptr, strlen(ptr), enc);
+}
 
-    rb_obj_freeze(str);
-    return str;
+static VALUE
+env_str_new(const char *ptr, long len)
+{
+    return env_enc_str_new(ptr, len, rb_locale_encoding());
 }
 
 static VALUE
@@ -2973,6 +2972,25 @@ env_str_new2(const char *ptr) https://github.com/ruby/ruby/blob/trunk/hash.c#L2972
     return env_str_new(ptr, strlen(ptr));
 }
 
+static int env_path_tainted(const char *);
+
+static rb_encoding *
+env_encoding_for(const char *name, const char *ptr)
+{
+    if (ENVMATCH(name, PATH_ENV) && !env_path_tainted(ptr)) {
+	return rb_filesystem_encoding();
+    }
+    else {
+	return rb_locale_encoding();
+    }
+}
+
+static VALUE
+env_name_new(const char *name, const char *ptr)
+{
+    return env_enc_str_new_cstr(ptr, env_encoding_for(name, ptr));
+}
+
 static void *
 get_env_cstr(
 #ifdef _WIN32
@@ -3061,8 +3079,6 @@ env_delete_m(VALUE obj, VALUE name) https://github.com/ruby/ruby/blob/trunk/hash.c#L3079
     return val;
 }
 
-static int env_path_tainted(const char *);
-
 /*
  * call-seq:
  *   ENV[name] -> value
@@ -3078,10 +3094,7 @@ rb_f_getenv(VALUE obj, VALUE name) https://github.com/ruby/ruby/blob/trunk/hash.c#L3094
     nam = env_name(name);
     env = getenv(nam);
     if (env) {
-	if (ENVMATCH(nam, PATH_ENV) && !env_path_tainted(env)) {
-	    return env_path_str_new(env);
-	}
-	return env_str_new2(env);
+	return env_name_new(nam, env);
     }
     return Qnil;
 }
@@ -3122,9 +3135,7 @@ env_fetch(int argc, VALUE *argv) https://github.com/ruby/ruby/blob/trunk/hash.c#L3135
 	}
 	return argv[1];
     }
-    if (ENVMATCH(nam, PATH_ENV) && !env_path_tainted(env))
-	return env_path_str_new(env);
-    return env_str_new2(env);
+    return env_name_new(nam, env);
 }
 
 static void
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 55818)
+++ ChangeLog	(revision 55819)
@@ -1,3 +1,11 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sat Aug  6 00:39:44 2016  Nobuyoshi Nakada  <nobu@r...>
+
+	* hash.c (env_enc_str_new): make string for an environment
+	  variable name or value.
+
+	* hash.c (env_name_new): make environment value string with the
+	  encoding for its name.
+
 Fri Aug  5 23:18:35 2016  NAKAMURA Usaku  <usa@r...>
 
 	* hasn.c (env_str_new): taint the string.  get rid of a test failure

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

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