From 62b018972c2be5e3bfc247e2ef01947533940293 Mon Sep 17 00:00:00 2001 From: knu Date: Sun, 25 May 2008 18:33:05 +0000 Subject: Merge from ruby_1_8. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8_7@16594 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 23 +++++++++++++++++ hash.c | 85 +++++++++++++++++++++++++++++++++++++++++++++------------------ 2 files changed, 84 insertions(+), 24 deletions(-) diff --git a/ChangeLog b/ChangeLog index de7f989eb5..8b30b4072d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,26 @@ +Mon May 26 03:16:20 2008 Akinori MUSHA + + * hash.c (rb_hash_default): Fix rdoc. + (rb_hash_each, env_each_value, env_each_pair): Return an + enumerator if no block is given. + (rb_hash_update): Update rdoc. + (envix): Conditionalize the definition itself. + (rb_f_getenv, env_fetch, env_keys, env_values, env_values_at) + (env_select, env_inspect, env_to_a, env_empty_p, env_has_key) + (env_has_value, env_index, env_indexes, env_to_hash, env_shift) + (env_update): Require secure level 4. + (env_each_value, env_each_i): Delay variable initialization. + (env_each_key, env_each_value, env_reject_bang) + (env_clear, env_replace): Omit duplicated secure level check. + (env_has_value): Do to_str conversion. + +Sun May 25 19:48:12 2008 Akinori MUSHA + + * hash.c (env_delete_if): Return an enumerator if no block is + given. + (env_each_key): Delay a variable initialization after + RETURN_ENUMERATOR(). + Sun May 25 05:07:19 2008 Akinori MUSHA * array.c (rb_ary_slice_bang): Be consistent with Array#slice() diff --git a/hash.c b/hash.c index 75db4d6b4f..55e2d0a819 100644 --- a/hash.c +++ b/hash.c @@ -538,7 +538,7 @@ rb_hash_fetch(argc, argv, hash) * h.default(2) #=> "cat" * * h = Hash.new {|h,k| h[k] = k.to_i*10} #=> {} - * h.default #=> 0 + * h.default #=> nil * h.default(2) #=> 20 */ @@ -891,7 +891,7 @@ select_i(key, value, result) * * h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" } * h.values_at("cow", "cat") #=> ["bovine", "feline"] -*/ + */ VALUE rb_hash_values_at(argc, argv, hash) @@ -1212,6 +1212,7 @@ static VALUE rb_hash_each(hash) VALUE hash; { + RETURN_ENUMERATOR(hash, 0, 0); rb_hash_foreach(hash, each_i, 0); return hash; } @@ -1716,12 +1717,20 @@ rb_hash_update_block_i(key, value, hash) * hsh.merge!(other_hash){|key, oldval, newval| block} => hsh * hsh.update(other_hash){|key, oldval, newval| block} => hsh * - * Adds the contents of other_hash to hsh, overwriting - * entries with duplicate keys with those from other_hash. + * Adds the contents of other_hash to hsh. If no + * block is specified entries with duplicate keys are overwritten + * with the values from other_hash, otherwise the value + * of each duplicate key is determined by calling the block with + * the key, its value in hsh and its value in other_hash. * * h1 = { "a" => 100, "b" => 200 } * h2 = { "b" => 254, "c" => 300 } * h1.merge!(h2) #=> {"a"=>100, "b"=>254, "c"=>300} + * + * h1 = { "a" => 100, "b" => 200 } + * h2 = { "b" => 254, "c" => 300 } + * h1.merge!(h2) { |key, v1, v2| v1 } + * #=> {"a"=>100, "b"=>200, "c"=>300} */ static VALUE @@ -1847,7 +1856,8 @@ rb_f_getenv(obj, name) { char *nam, *env; - StringValue(name); + rb_secure(4); + SafeStringValue(name); nam = RSTRING(name)->ptr; if (strlen(nam) != RSTRING(name)->len) { rb_raise(rb_eArgError, "bad environment variable name"); @@ -1879,12 +1889,13 @@ env_fetch(argc, argv) long block_given; char *nam, *env; + rb_secure(4); 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"); } - StringValue(key); + SafeStringValue(key); nam = RSTRING(key)->ptr; if (strlen(nam) != RSTRING(key)->len) { rb_raise(rb_eArgError, "bad environment variable name"); @@ -1922,6 +1933,7 @@ rb_env_path_tainted() return path_tainted; } +#if !defined(_WIN32) && !(defined(HAVE_SETENV) && defined(HAVE_UNSETENV)) static int envix(nam) const char *nam; @@ -1943,6 +1955,7 @@ envix(nam) FREE_ENVIRON(environ); return i; } +#endif void ruby_setenv(name, value) @@ -2079,8 +2092,10 @@ static VALUE env_keys() { char **env; - VALUE ary = rb_ary_new(); + VALUE ary; + rb_secure(4); + ary = rb_ary_new(); env = GET_ENVIRON(environ); while (*env) { char *s = strchr(*env, '='); @@ -2097,10 +2112,11 @@ static VALUE env_each_key(ehash) VALUE ehash; { - VALUE keys = env_keys(); + VALUE keys; long i; RETURN_ENUMERATOR(ehash, 0, 0); + keys = env_keys(); /* rb_secure(4); */ for (i=0; ilen; i++) { rb_yield(RARRAY(keys)->ptr[i]); } @@ -2110,9 +2126,11 @@ env_each_key(ehash) static VALUE env_values() { + VALUE ary; char **env; - VALUE ary = rb_ary_new(); + rb_secure(4); + ary = rb_ary_new(); env = GET_ENVIRON(environ); while (*env) { char *s = strchr(*env, '='); @@ -2129,10 +2147,11 @@ static VALUE env_each_value(ehash) VALUE ehash; { - VALUE values = env_values(); + VALUE values; long i; RETURN_ENUMERATOR(ehash, 0, 0); + values = env_values(); /* rb_secure(4); */ for (i=0; ilen; i++) { rb_yield(RARRAY(values)->ptr[i]); } @@ -2145,9 +2164,11 @@ env_each_i(ehash, values) int values; { char **env; - VALUE ary = rb_ary_new(); + VALUE ary; long i; + rb_secure(4); + ary = rb_ary_new(); env = GET_ENVIRON(environ); while (*env) { char *s = strchr(*env, '='); @@ -2182,6 +2203,7 @@ static VALUE env_each_pair(ehash) VALUE ehash; { + RETURN_ENUMERATOR(ehash, 0, 0); return env_each_i(ehash, Qtrue); } @@ -2194,9 +2216,7 @@ env_reject_bang(ehash) int del = 0; RETURN_ENUMERATOR(ehash, 0, 0); - rb_secure(4); - keys = env_keys(); - + keys = env_keys(); /* rb_secure(4); */ for (i=0; ilen; i++) { VALUE val = rb_f_getenv(Qnil, RARRAY(keys)->ptr[i]); if (!NIL_P(val)) { @@ -2215,6 +2235,7 @@ static VALUE env_delete_if(ehash) VALUE ehash; { + RETURN_ENUMERATOR(ehash, 0, 0); env_reject_bang(ehash); return envtbl; } @@ -2224,9 +2245,11 @@ env_values_at(argc, argv) int argc; VALUE *argv; { - VALUE result = rb_ary_new(); + VALUE result; long i; + rb_secure(4); + result = rb_ary_new(); for (i=0; ilen; i++) { VALUE val = rb_f_getenv(Qnil, RARRAY(keys)->ptr[i]); if (!NIL_P(val)) { @@ -2287,9 +2309,10 @@ static VALUE env_inspect() { char **env; - VALUE str = rb_str_buf_new2("{"); - VALUE i; + VALUE str, i; + rb_secure(4); + str = rb_str_buf_new2("{"); env = GET_ENVIRON(environ); while (*env) { char *s = strchr(*env, '='); @@ -2317,8 +2340,10 @@ static VALUE env_to_a() { char **env; - VALUE ary = rb_ary_new(); + VALUE ary; + rb_secure(4); + ary = rb_ary_new(); env = GET_ENVIRON(environ); while (*env) { char *s = strchr(*env, '='); @@ -2344,6 +2369,7 @@ env_size() int i; char **env; + rb_secure(4); env = GET_ENVIRON(environ); for(i=0; env[i]; i++) ; @@ -2356,6 +2382,7 @@ env_empty_p() { char **env; + rb_secure(4); env = GET_ENVIRON(environ); if (env[0] == 0) { FREE_ENVIRON(environ); @@ -2371,6 +2398,7 @@ env_has_key(env, key) { char *s; + rb_secure(4); s = StringValuePtr(key); if (strlen(s) != RSTRING(key)->len) rb_raise(rb_eArgError, "bad environment variable name"); @@ -2384,7 +2412,9 @@ env_has_value(dmy, value) { char **env; - if (TYPE(value) != T_STRING) return Qfalse; + rb_secure(4); + value = rb_check_string_type(value); + if (NIL_P(value)) return Qfalse; env = GET_ENVIRON(environ); while (*env) { char *s = strchr(*env, '='); @@ -2408,6 +2438,7 @@ env_index(dmy, value) char **env; VALUE str; + rb_secure(4); StringValue(value); env = GET_ENVIRON(environ); while (*env) { @@ -2436,6 +2467,7 @@ env_indexes(argc, argv) rb_warn("ENV.%s is deprecated; use ENV.values_at", rb_id2name(rb_frame_last_func())); + rb_secure(4); for (i=0;i