sim: run: fix crash in argc==0 error situation
authorMike Frysinger <vapier@gentoo.org>
Mon, 15 Nov 2021 08:32:26 +0000 (03:32 -0500)
committerMike Frysinger <vapier@gentoo.org>
Mon, 15 Nov 2021 08:32:26 +0000 (03:32 -0500)
The new argv processing code assumed that we were always passed a
command line.  If we weren't, make sure we don't crash before we
get a chance to output an error message about incorrect usage.

sim/common/sim-options.c

index ee7d11fb692534cd0763fe73b295e1da18a240f0..17e550e55577ee47b87a95a81c54e84e16afb1b9 100644 (file)
@@ -615,16 +615,26 @@ sim_parse_args (SIM_DESC sd, char * const *argv)
        {
          if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
            {
-             char **new_argv = dupargv (argv + optind);
+             char **new_argv;
 
-             STATE_PROG_FILE (sd) = xstrdup (argv[optind]);
-             if (STATE_PROG_ARGV0 (sd) != NULL)
-               {
-                 free (new_argv[0]);
-                 new_argv[0] = xstrdup (STATE_PROG_ARGV0 (sd));
-               }
+             free (STATE_PROG_FILE (sd));
+             STATE_PROG_FILE (sd) = NULL;
+
+             new_argv = dupargv (argv + optind);
              freeargv (STATE_PROG_ARGV (sd));
              STATE_PROG_ARGV (sd) = new_argv;
+
+             /* Skip steps when argc == 0.  */
+             if (argv[optind] != NULL)
+               {
+                 STATE_PROG_FILE (sd) = xstrdup (argv[optind]);
+
+                 if (STATE_PROG_ARGV0 (sd) != NULL)
+                   {
+                     free (new_argv[0]);
+                     new_argv[0] = xstrdup (STATE_PROG_ARGV0 (sd));
+                   }
+               }
            }
          break;
        }