From: nobu@... Date: 2017-08-19T03:53:59+00:00 Subject: [ruby-core:82425] [Ruby trunk Bug#13829] NUL char in $0 Issue #13829 has been reported by nobu (Nobuyoshi Nakada). ---------------------------------------- Bug #13829: NUL char in $0 https://bugs.ruby-lang.org/issues/13829 * Author: nobu (Nobuyoshi Nakada) * Status: Open * Priority: Normal * Assignee: * Target version: * ruby -v: * Backport: 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: UNKNOWN ---------------------------------------- `$0` and `Process.setproctitle` ignore `\0` and the after. Is it intentional? ``` $ ruby -e '$0 = "foo\0bar"; system("ps", "#$$"); Process.setproctitle("bar\0foo"); system("ps", "#$$")' PID TT STAT TIME COMMAND 34931 s001 S+ 0:00.06 foo PID TT STAT TIME COMMAND 34931 s001 S+ 0:00.06 bar ``` I'd expect an exception instead. ```diff commit c136175ff118c7837948cf6fbdcdd0d96bc8a837 Author: Nobuyoshi Nakada Date: Fri Aug 18 22:30:06 2017 ruby.c: reject NUL in $0 * ruby.c (ruby_setproctitle): raise if the argument contains NUL char. process title is a NUL-terminated string. diff --git a/ruby.c b/ruby.c index f2f3c86e09..09ab1c24c4 100644 --- a/ruby.c +++ b/ruby.c @@ -2041,6 +2041,8 @@ proc_argv0(VALUE process) return rb_orig_progname; } +static VALUE ruby_setproctitle(VALUE title); + /* * call-seq: * Process.setproctitle(string) -> string @@ -2061,10 +2063,14 @@ proc_argv0(VALUE process) static VALUE proc_setproctitle(VALUE process, VALUE title) { - StringValue(title); - - setproctitle("%.*s", RSTRING_LENINT(title), RSTRING_PTR(title)); + return ruby_setproctitle(title); +} +static VALUE +ruby_setproctitle(VALUE title) +{ + const char *ptr = StringValueCStr(title); + setproctitle("%.*s", RSTRING_LENINT(title), ptr); return title; } @@ -2074,7 +2080,7 @@ set_arg0(VALUE val, ID id) if (origarg.argv == 0) rb_raise(rb_eRuntimeError, "$0 not initialized"); - rb_progname = rb_str_new_frozen(proc_setproctitle(rb_mProcess, val)); + rb_progname = rb_str_new_frozen(ruby_setproctitle(val)); } static inline VALUE diff --git a/test/ruby/test_rubyoptions.rb b/test/ruby/test_rubyoptions.rb index d6297c8979..5d2967e65e 100644 --- a/test/ruby/test_rubyoptions.rb +++ b/test/ruby/test_rubyoptions.rb @@ -534,6 +534,13 @@ def test_setproctitle skip "platform dependent feature" unless defined?(PSCMD) and PSCMD + assert_separately([], "#{<<-"begin;"}\n#{<<-'end;'}") + begin; + assert_raise(ArgumentError) do + Process.setproctitle("hello\0") + end + end; + with_tmpchdir do write_file("test-script", "$_0 = $0.dup; Process.setproctitle('hello world'); $0 == $_0 or Process.setproctitle('$0 changed!'); sleep 60") ``` -- https://bugs.ruby-lang.org/ Unsubscribe: