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

ruby-changes:47433

From: nobu <ko1@a...>
Date: Wed, 9 Aug 2017 20:04:01 +0900 (JST)
Subject: [ruby-changes:47433] nobu:r59549 (trunk): ruby.c: origarg

nobu	2017-08-09 20:03:55 +0900 (Wed, 09 Aug 2017)

  New Revision: 59549

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

  Log:
    ruby.c: origarg
    
    * ruby.c (dladdr_path, ruby_set_argv): add guards for origarg.
      [ruby-core:82272] [Bug #13788]
    
    * ruby.c (proc_options, process_options, ruby_process_options):
      set origarg if not set yet.
    
    * ruby.c (process_options): prefer argv in the argument to origarg
      as program name.
    
    * ruby.c (ruby_sysinit): set origarg only if argc and argv seem
      valid.

  Modified files:
    trunk/ruby.c
Index: ruby.c
===================================================================
--- ruby.c	(revision 59548)
+++ ruby.c	(revision 59549)
@@ -450,7 +450,7 @@ dladdr_path(const void* addr) https://github.com/ruby/ruby/blob/trunk/ruby.c#L450
 	return rb_str_new(0, 0);
     }
 #ifdef __linux__
-    else if (dli.dli_fname == origarg.argv[0]) {
+    else if (origarg.argc > 0 && origarg.argv && dli.dli_fname == origarg.argv[0]) {
 	fname = rb_str_new_cstr("/proc/self/exe");
 	path = rb_readlink(fname, NULL);
     }
@@ -912,7 +912,7 @@ proc_options(long argc, char **argv, rub https://github.com/ruby/ruby/blob/trunk/ruby.c#L912
     const char *s;
     int warning = opt->warning;
 
-    if (argc == 0)
+    if (argc <= 0 || !argv)
 	return 0;
 
     for (argc--, argv++; argc > 0; argc--, argv++) {
@@ -1465,14 +1465,18 @@ process_options(int argc, char **argv, r https://github.com/ruby/ruby/blob/trunk/ruby.c#L1465
     const struct rb_block *base_block;
     unsigned int dump = opt->dump & dump_exit_bits;
 
-    argc -= i;
-    argv += i;
-
     if (opt->dump & (DUMP_BIT(usage)|DUMP_BIT(help))) {
-	usage(origarg.argv[0], (opt->dump & DUMP_BIT(help)));
+	const char *const progname =
+	    (argc > 0 && argv && argv[0] ? argv[0] :
+	     origarg.argc > 0 && origarg.argv && origarg.argv[0] ? origarg.argv[0] :
+	     ruby_engine);
+	usage(progname, (opt->dump & DUMP_BIT(help)));
 	return Qtrue;
     }
 
+    argc -= i;
+    argv += i;
+
     if ((opt->features & FEATURE_BIT(rubyopt)) &&
 	opt->safe_level == 0 && (s = getenv("RUBYOPT"))) {
 	VALUE src_enc_name = opt->src.enc.name;
@@ -1502,7 +1506,7 @@ process_options(int argc, char **argv, r https://github.com/ruby/ruby/blob/trunk/ruby.c#L1506
     }
 
     if (!opt->e_script) {
-	if (argc == 0) {	/* no more args */
+	if (argc <= 0) {	/* no more args */
 	    if (opt->verbose)
 		return Qtrue;
 	    opt->script = "-";
@@ -2192,9 +2196,9 @@ ruby_set_argv(int argc, char **argv) https://github.com/ruby/ruby/blob/trunk/ruby.c#L2196
     VALUE av = rb_argv;
 
 #if defined(USE_DLN_A_OUT)
-    if (origarg.argv)
+    if (origarg.argc > 0 && origarg.argv)
 	dln_argv0 = origarg.argv[0];
-    else
+    else if (argc > 0 && argv)
 	dln_argv0 = argv[0];
 #endif
     rb_ary_clear(av);
@@ -2213,6 +2217,10 @@ ruby_process_options(int argc, char **ar https://github.com/ruby/ruby/blob/trunk/ruby.c#L2217
     VALUE iseq;
     const char *script_name = (argc > 0 && argv[0]) ? argv[0] : ruby_engine;
 
+    if (!origarg.argv || origarg.argc <= 0) {
+	origarg.argc = argc;
+	origarg.argv = argv;
+    }
     ruby_script(script_name);  /* for the time being */
     rb_argv0 = rb_str_new4(rb_progname);
     rb_gc_register_mark_object(rb_argv0);
@@ -2255,11 +2263,12 @@ fill_standard_fds(void) https://github.com/ruby/ruby/blob/trunk/ruby.c#L2263
     }
 }
 
-/*! Initializes the process for ruby(1).
+/*! Initializes the process for libruby.
  *
  * This function assumes this process is ruby(1) and it has just started.
- * Usually programs that embeds CRuby interpreter should not call this function,
- * and should do their own initialization.
+ * Usually programs that embed CRuby interpreter may not call this function,
+ * and may do their own initialization.
+ * argc and argv cannot be NULL.
  */
 void
 ruby_sysinit(int *argc, char ***argv)
@@ -2267,10 +2276,12 @@ ruby_sysinit(int *argc, char ***argv) https://github.com/ruby/ruby/blob/trunk/ruby.c#L2276
 #if defined(_WIN32)
     rb_w32_sysinit(argc, argv);
 #endif
-    origarg.argc = *argc;
-    origarg.argv = *argv;
+    if (*argc >= 0 && *argv) {
+	origarg.argc = *argc;
+	origarg.argv = *argv;
 #if defined(USE_DLN_A_OUT)
-    dln_argv0 = origarg.argv[0];
+	dln_argv0 = origarg.argv[0];
 #endif
+    }
     fill_standard_fds();
 }

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

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