ruby-changes:47210
From: nobu <ko1@a...>
Date: Fri, 14 Jul 2017 13:43:25 +0900 (JST)
Subject: [ruby-changes:47210] nobu:r59325 (trunk): process.c: null bytes
nobu 2017-07-14 13:43:16 +0900 (Fri, 14 Jul 2017) New Revision: 59325 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=59325 Log: process.c: null bytes * process.c (rlimit_type_by_sym): prohibit null bytes in key names. [ruby-core:82033] [Bug #13744] Modified files: trunk/process.c trunk/test/ruby/test_process.rb Index: test/ruby/test_process.rb =================================================================== --- test/ruby/test_process.rb (revision 59324) +++ test/ruby/test_process.rb (revision 59325) @@ -254,6 +254,10 @@ class TestProcess < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_process.rb#L254 system("#{RUBY}", '-e', 'exit', :rlimit_bogus => 123) end end; + + assert_raise(ArgumentError, /rlimit_cpu/) { + system(RUBY, '-e', 'exit', "rlimit_cpu\0".to_sym => 3600) + } end MANDATORY_ENVS = %w[RUBYLIB] Index: process.c =================================================================== --- process.c (revision 59324) +++ process.c (revision 59325) @@ -1817,7 +1817,7 @@ check_exec_options_i(st_data_t st_key, s https://github.com/ruby/ruby/blob/trunk/process.c#L1817 VALUE execarg_obj = (VALUE)arg; if (rb_execarg_addopt(execarg_obj, key, val) != ST_CONTINUE) { if (SYMBOL_P(key)) - rb_raise(rb_eArgError, "wrong exec option symbol: %"PRIsVALUE, + rb_raise(rb_eArgError, "wrong exec option symbol: % "PRIsVALUE, key); rb_raise(rb_eArgError, "wrong exec option"); } @@ -4678,13 +4678,13 @@ proc_setpriority(VALUE obj, VALUE which, https://github.com/ruby/ruby/blob/trunk/process.c#L4678 #if defined(HAVE_SETRLIMIT) && defined(NUM2RLIM) static int -rlimit_resource_name2int(const char *name, int casetype) +rlimit_resource_name2int(const char *name, long len, int casetype) { int resource; const char *p; #define RESCHECK(r) \ do { \ - if (STRCASECMP(name, #r) == 0) { \ + if (len == rb_strlen_lit(#r) && STRCASECMP(name, #r) == 0) { \ resource = RLIMIT_##r; \ goto found; \ } \ @@ -4787,25 +4787,29 @@ rlimit_resource_name2int(const char *nam https://github.com/ruby/ruby/blob/trunk/process.c#L4787 } static int -rlimit_type_by_hname(const char *name) +rlimit_type_by_hname(const char *name, long len) { - return rlimit_resource_name2int(name, 0); + return rlimit_resource_name2int(name, len, 0); } static int -rlimit_type_by_lname(const char *name) +rlimit_type_by_lname(const char *name, long len) { - return rlimit_resource_name2int(name, 1); + return rlimit_resource_name2int(name, len, 1); } static int rlimit_type_by_sym(VALUE key) { - const char *rname = RSTRING_PTR(rb_sym2str(key)); + VALUE name = rb_sym2str(key); + const char *rname = RSTRING_PTR(name); + long len = RSTRING_LEN(name); int rtype = -1; + static const char prefix[] = "rlimit_"; + enum {prefix_len = sizeof(prefix)-1}; - if (strncmp("rlimit_", rname, 7) == 0) { - rtype = rlimit_type_by_lname(rname + 7); + if (len > prefix_len && strncmp(prefix, rname, prefix_len) == 0) { + rtype = rlimit_type_by_lname(rname + prefix_len, len - prefix_len); } RB_GC_GUARD(key); @@ -4816,6 +4820,7 @@ static int https://github.com/ruby/ruby/blob/trunk/process.c#L4820 rlimit_resource_type(VALUE rtype) { const char *name; + long len; VALUE v; int r; @@ -4823,6 +4828,7 @@ rlimit_resource_type(VALUE rtype) https://github.com/ruby/ruby/blob/trunk/process.c#L4828 case T_SYMBOL: v = rb_sym2str(rtype); name = RSTRING_PTR(v); + len = RSTRING_LEN(v); break; default: @@ -4831,6 +4837,7 @@ rlimit_resource_type(VALUE rtype) https://github.com/ruby/ruby/blob/trunk/process.c#L4837 rtype = v; case T_STRING: name = StringValueCStr(rtype); + len = RSTRING_LEN(rtype); break; } /* fall through */ @@ -4840,11 +4847,11 @@ rlimit_resource_type(VALUE rtype) https://github.com/ruby/ruby/blob/trunk/process.c#L4847 return NUM2INT(rtype); } - r = rlimit_type_by_hname(name); + r = rlimit_type_by_hname(name, len); if (r != -1) return r; - rb_raise(rb_eArgError, "invalid resource name: %"PRIsVALUE, rtype); + rb_raise(rb_eArgError, "invalid resource name: % "PRIsVALUE, rtype); UNREACHABLE; } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/