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

ruby-changes:34468

From: nobu <ko1@a...>
Date: Wed, 25 Jun 2014 21:19:03 +0900 (JST)
Subject: [ruby-changes:34468] nobu:r46549 (trunk): hash.c: env_name

nobu	2014-06-25 21:18:53 +0900 (Wed, 25 Jun 2014)

  New Revision: 46549

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

  Log:
    hash.c: env_name
    
    * hash.c (get_env_ptr, env_name): extract check for bad
      environment variable name.

  Modified files:
    trunk/hash.c
Index: hash.c
===================================================================
--- hash.c	(revision 46548)
+++ hash.c	(revision 46549)
@@ -2542,16 +2542,25 @@ env_str_new2(const char *ptr) https://github.com/ruby/ruby/blob/trunk/hash.c#L2542
     return env_str_new(ptr, strlen(ptr));
 }
 
+#define get_env_ptr(var, val) \
+    (memchr((var = RSTRING_PTR(val)), '\0', RSTRING_LEN(val)) ? \
+     rb_raise(rb_eArgError, "bad environment variable " #var) : (void)0)
+
+static inline const char *
+env_name(VALUE s)
+{
+    const char *name;
+    SafeStringValue(s);
+    get_env_ptr(name, s);
+    return name;
+}
+
 static VALUE
 env_delete(VALUE obj, VALUE name)
 {
-    char *nam, *val;
+    const char *nam, *val;
 
-    SafeStringValue(name);
-    nam = RSTRING_PTR(name);
-    if (memchr(nam, '\0', RSTRING_LEN(name))) {
-	rb_raise(rb_eArgError, "bad environment variable name");
-    }
+    nam = env_name(name);
     val = getenv(nam);
     if (val) {
 	VALUE value = env_str_new2(val);
@@ -2596,13 +2605,9 @@ static int env_path_tainted(const char * https://github.com/ruby/ruby/blob/trunk/hash.c#L2605
 static VALUE
 rb_f_getenv(VALUE obj, VALUE name)
 {
-    char *nam, *env;
+    const char *nam, *env;
 
-    SafeStringValue(name);
-    nam = RSTRING_PTR(name);
-    if (memchr(nam, '\0', RSTRING_LEN(name))) {
-	rb_raise(rb_eArgError, "bad environment variable name");
-    }
+    nam = env_name(name);
     env = getenv(nam);
     if (env) {
 	if (ENVMATCH(nam, PATH_ENV) && !env_path_tainted(env)) {
@@ -2632,18 +2637,14 @@ env_fetch(int argc, VALUE *argv) https://github.com/ruby/ruby/blob/trunk/hash.c#L2637
 {
     VALUE key, if_none;
     long block_given;
-    char *nam, *env;
+    const char *nam, *env;
 
     rb_scan_args(argc, argv, "11", &key, &if_none);
     block_given = rb_block_given_p();
     if (block_given && argc == 2) {
 	rb_warn("block supersedes default value argument");
     }
-    SafeStringValue(key);
-    nam = RSTRING_PTR(key);
-    if (memchr(nam, '\0', RSTRING_LEN(key))) {
-	rb_raise(rb_eArgError, "bad environment variable name");
-    }
+    nam = env_name(key);
     env = getenv(nam);
     if (!env) {
 	if (block_given) return rb_yield(key);
@@ -2873,12 +2874,10 @@ env_aset(VALUE obj, VALUE nm, VALUE val) https://github.com/ruby/ruby/blob/trunk/hash.c#L2874
     }
     SafeStringValue(nm);
     SafeStringValue(val);
-    name = RSTRING_PTR(nm);
-    value = RSTRING_PTR(val);
-    if (memchr(name, '\0', RSTRING_LEN(nm)))
-	rb_raise(rb_eArgError, "bad environment variable name");
-    if (memchr(value, '\0', RSTRING_LEN(val)))
-	rb_raise(rb_eArgError, "bad environment variable value");
+    /* nm can be modified in `val.to_str`, don't get `name` before
+     * check for `val` */
+    get_env_ptr(name, nm);
+    get_env_ptr(value, val);
 
     ruby_setenv(name, value);
     if (ENVMATCH(name, PATH_ENV)) {
@@ -3367,12 +3366,9 @@ env_empty_p(void) https://github.com/ruby/ruby/blob/trunk/hash.c#L3366
 static VALUE
 env_has_key(VALUE env, VALUE key)
 {
-    char *s;
+    const char *s;
 
-    SafeStringValue(key);
-    s = RSTRING_PTR(key);
-    if (memchr(s, '\0', RSTRING_LEN(key)))
-	rb_raise(rb_eArgError, "bad environment variable name");
+    s = env_name(key);
     if (getenv(s)) return Qtrue;
     return Qfalse;
 }
@@ -3387,12 +3383,9 @@ env_has_key(VALUE env, VALUE key) https://github.com/ruby/ruby/blob/trunk/hash.c#L3383
 static VALUE
 env_assoc(VALUE env, VALUE key)
 {
-    char *s, *e;
+    const char *s, *e;
 
-    SafeStringValue(key);
-    s = RSTRING_PTR(key);
-    if (memchr(s, '\0', RSTRING_LEN(key)))
-	rb_raise(rb_eArgError, "bad environment variable name");
+    s = env_name(key);
     e = getenv(s);
     if (e) return rb_assoc_new(key, rb_tainted_str_new2(e));
     return Qnil;

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

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