public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH/committed] sim: rl78: clean up various warnings
@ 2021-05-05  3:04 Mike Frysinger
  2021-05-05 17:30 ` Tom Tromey
  0 siblings, 1 reply; 8+ messages in thread
From: Mike Frysinger @ 2021-05-05  3:04 UTC (permalink / raw)
  To: gdb-patches

A random grab bag of minor fixes to enable -Werror for this port.

Fix local prototypes for a bunch of functions (e.g. adding static).
Add missing includes for missing prototypes.
Move local variable decls from the middle of functions to the top
of the scope.
Fix a logic error when processing commands where p was reassigned
to cmd and then has its leading whitespace scanned a 2nd time.
Handle short reads with fread().
---
 sim/rl78/ChangeLog    | 14 ++++++++++++++
 sim/rl78/configure    |  5 ++++-
 sim/rl78/configure.ac |  1 -
 sim/rl78/cpu.c        |  2 +-
 sim/rl78/gdb-if.c     |  9 ++-------
 sim/rl78/mem.c        |  6 +++---
 sim/rl78/mem.h        |  2 ++
 sim/rl78/rl78.c       |  3 ++-
 sim/rl78/trace.c      |  9 ++++++---
 9 files changed, 34 insertions(+), 17 deletions(-)

diff --git a/sim/rl78/ChangeLog b/sim/rl78/ChangeLog
index f0e1631d5e48..baaf6d8eae9e 100644
--- a/sim/rl78/ChangeLog
+++ b/sim/rl78/ChangeLog
@@ -1,3 +1,17 @@
+2021-05-04  Mike Frysinger  <vapier@gentoo.org>
+
+	* cpu.c (trace_register_init): Add missing (void).
+	* gdb-if.c (rl78_signal_to_target, handle_step): Mark static.
+	(sim_do_command): Delete redundant for loop.
+	* mem.c (mirror_rom_base, mirror_ram_base, mirror_length): Mark static.
+	* mem.h (mem_set_mirror): New prototype.
+	* rl78.c (op_flags): Move psw decl to top of scope.
+	* trace.c: Include trace.h.
+	(load_file_and_line): Move file decl to top of scope.  Declare ret.
+	assign fread to ret and use to index f->data.
+	* configure.ac: Delete SIM_AC_OPTION_WARNINGS call.
+	* configure: Regenerate.
+
 2021-05-04  Mike Frysinger  <vapier@gentoo.org>
 
 	* configure: Regenerate.
diff --git a/sim/rl78/configure b/sim/rl78/configure
index 59cb1ec2861a..6e2076857dde 100755
--- a/sim/rl78/configure
+++ b/sim/rl78/configure
@@ -11844,6 +11844,7 @@ _ACEOF
 
 
 
+
 # Check whether --enable-werror was given.
 if test "${enable_werror+set}" = set; then :
   enableval=$enable_werror; case "${enableval}" in
@@ -11860,6 +11861,9 @@ if test "${GCC}" = yes -a -z "${ERROR_ON_WARNING}" ; then
 fi
 
 WERROR_CFLAGS=""
+  if test "${ERROR_ON_WARNING}" = yes ; then
+    WERROR_CFLAGS="-Werror"
+  fi
 
 build_warnings="-Wall -Wdeclaration-after-statement -Wpointer-arith \
 -Wpointer-sign \
@@ -11941,7 +11945,6 @@ $as_echo "${WARN_CFLAGS} ${WERROR_CFLAGS}" >&6; }
 fi
 
 
-
 hardware="cfi core pal glue "
 sim_hw_cflags="-DWITH_HW=1"
 sim_hw="$hardware"
diff --git a/sim/rl78/configure.ac b/sim/rl78/configure.ac
index 0e5f69c7065c..4f2c0ace70bb 100644
--- a/sim/rl78/configure.ac
+++ b/sim/rl78/configure.ac
@@ -22,6 +22,5 @@ AC_INIT(Makefile.in)
 AC_CONFIG_MACRO_DIRS([../m4 ../.. ../../config])
 
 SIM_AC_COMMON
-SIM_AC_OPTION_WARNINGS(no)
 
 SIM_AC_OUTPUT
diff --git a/sim/rl78/cpu.c b/sim/rl78/cpu.c
index a38d6f688aee..fde8afee96b0 100644
--- a/sim/rl78/cpu.c
+++ b/sim/rl78/cpu.c
@@ -51,7 +51,7 @@ typedef struct {
   unsigned char h;
 } RegBank;
 
-static void trace_register_init ();
+static void trace_register_init (void);
 
 /* This maps PSW to a pointer into memory[] */
 static RegBank *regbase_table[256];
diff --git a/sim/rl78/gdb-if.c b/sim/rl78/gdb-if.c
index 56717917e552..7119214113be 100644
--- a/sim/rl78/gdb-if.c
+++ b/sim/rl78/gdb-if.c
@@ -403,7 +403,7 @@ int siggnal;
 /* Given a signal number used by the rl78 bsp (that is, newlib),
    return the corresponding signal numbers.  */
 
-int
+static int
 rl78_signal_to_target (int sig)
 {
   switch (sig)
@@ -442,7 +442,7 @@ rl78_signal_to_target (int sig)
 /* Take a step return code RC and set up the variables consulted by
    sim_stop_reason appropriately.  */
 
-void
+static void
 handle_step (int rc)
 {
   if (RL78_STEPPED (rc) || RL78_HIT_BREAK (rc))
@@ -549,11 +549,6 @@ sim_do_command (SIM_DESC sd, const char *cmd)
       while (isspace (*p))
 	p++;
 
-      /* Find the extent of the command word.  */
-      for (p = cmd; *p; p++)
-	if (isspace (*p))
-	  break;
-
       /* Null-terminate the command word, and record the start of any
 	 further arguments.  */
       if (*p)
diff --git a/sim/rl78/mem.c b/sim/rl78/mem.c
index 8527e02cba1e..77e4987b6ff1 100644
--- a/sim/rl78/mem.c
+++ b/sim/rl78/mem.c
@@ -63,9 +63,9 @@ mem_rom_size (int rom_bytes)
   rom_limit = rom_bytes;
 }
 
-int mirror_rom_base = 0x01000;
-int mirror_ram_base = 0xf1000;
-int mirror_length = 0x7000;
+static int mirror_rom_base = 0x01000;
+static int mirror_ram_base = 0xf1000;
+static int mirror_length = 0x7000;
 
 void
 mem_set_mirror (int rom_base, int ram_base, int length)
diff --git a/sim/rl78/mem.h b/sim/rl78/mem.h
index f04b36aa8d36..77d2f18a786c 100644
--- a/sim/rl78/mem.h
+++ b/sim/rl78/mem.h
@@ -29,6 +29,8 @@ extern unsigned char memory[];
 
 void init_mem (void);
 
+void mem_set_mirror (int rom_base, int ram_base, int length);
+
 /* Pass the amount of bytes, like 2560 for 2.5k  */
 void mem_ram_size (int ram_bytes);
 void mem_rom_size (int rom_bytes);
diff --git a/sim/rl78/rl78.c b/sim/rl78/rl78.c
index a969845c5f97..006691c4c0af 100644
--- a/sim/rl78/rl78.c
+++ b/sim/rl78/rl78.c
@@ -249,6 +249,7 @@ static void
 op_flags (int before, int after, int mask, RL78_Size size)
 {
   int vmask, cmask, amask, avmask;
+  int psw;
 
   if (size == RL78_Word)
     {
@@ -265,7 +266,7 @@ op_flags (int before, int after, int mask, RL78_Size size)
       avmask = 0x0f;
     }
 
-  int psw = get_reg (RL78_Reg_PSW);
+  psw = get_reg (RL78_Reg_PSW);
   psw &= ~mask;
 
   if (mask & RL78_PSW_CY)
diff --git a/sim/rl78/trace.c b/sim/rl78/trace.c
index b30ec2ae8415..6f897eb03393 100644
--- a/sim/rl78/trace.c
+++ b/sim/rl78/trace.c
@@ -36,6 +36,7 @@
 #include "cpu.h"
 #include "mem.h"
 #include "load.h"
+#include "trace.h"
 
 static disassembler_ftype rl78_disasm_fn = NULL;
 
@@ -138,6 +139,8 @@ load_file_and_line (const char *filename, int lineno)
       int i;
       struct stat s;
       const char *found_filename, *slash;
+      FILE *file;
+      size_t ret;
 
       found_filename = filename;
       while (1)
@@ -155,9 +158,9 @@ load_file_and_line (const char *filename, int lineno)
       files = f;
       f->filename = xstrdup (filename);
       f->data = (char *) xmalloc (s.st_size + 2);
-      FILE *file = fopen (found_filename, "rb");
-      fread (f->data, 1, s.st_size, file);
-      f->data[s.st_size] = 0;
+      file = fopen (found_filename, "rb");
+      ret = fread (f->data, 1, s.st_size, file);
+      f->data[ret] = 0;
       fclose (file);
 
       f->nlines = 1;
-- 
2.31.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH/committed] sim: rl78: clean up various warnings
  2021-05-05  3:04 [PATCH/committed] sim: rl78: clean up various warnings Mike Frysinger
@ 2021-05-05 17:30 ` Tom Tromey
  2021-05-05 18:52   ` Mike Frysinger
  2021-05-05 19:02   ` [PATCH] sim: m32c/rl78/rx: fix command parsing Mike Frysinger
  0 siblings, 2 replies; 8+ messages in thread
From: Tom Tromey @ 2021-05-05 17:30 UTC (permalink / raw)
  To: Mike Frysinger via Gdb-patches

>>>>> "Mike" == Mike Frysinger via Gdb-patches <gdb-patches@sourceware.org> writes:

Mike> Fix a logic error when processing commands where p was reassigned
Mike> to cmd and then has its leading whitespace scanned a 2nd time.
Mike> Handle short reads with fread().

Mike> -      /* Find the extent of the command word.  */
Mike> -      for (p = cmd; *p; p++)
Mike> -	if (isspace (*p))
Mike> -	  break;
Mike> -

I'm not sure it makes sense to completely delete this.
Perhaps instead just removing 'p = cmd' is correct?

It looks to me that this code is trying to parse a command name followed
by arguments.  Like, ideally, "monitor trace on" should set cmd="trace"
and args="on", but with the patch I don't think this will happen.

Tom

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH/committed] sim: rl78: clean up various warnings
  2021-05-05 17:30 ` Tom Tromey
@ 2021-05-05 18:52   ` Mike Frysinger
  2021-05-05 19:05     ` Tom Tromey
  2021-05-05 19:02   ` [PATCH] sim: m32c/rl78/rx: fix command parsing Mike Frysinger
  1 sibling, 1 reply; 8+ messages in thread
From: Mike Frysinger @ 2021-05-05 18:52 UTC (permalink / raw)
  To: Tom Tromey; +Cc: Mike Frysinger via Gdb-patches

On 05 May 2021 11:30, Tom Tromey wrote:
> >>>>> "Mike" == Mike Frysinger via Gdb-patches <gdb-patches@sourceware.org> writes:
> 
> Mike> Fix a logic error when processing commands where p was reassigned
> Mike> to cmd and then has its leading whitespace scanned a 2nd time.
> Mike> Handle short reads with fread().
> 
> Mike> -      /* Find the extent of the command word.  */
> Mike> -      for (p = cmd; *p; p++)
> Mike> -	if (isspace (*p))
> Mike> -	  break;
> Mike> -
> 
> I'm not sure it makes sense to completely delete this.
> Perhaps instead just removing 'p = cmd' is correct?
> 
> It looks to me that this code is trying to parse a command name followed
> by arguments.  Like, ideally, "monitor trace on" should set cmd="trace"
> and args="on", but with the patch I don't think this will happen.

you're right that the code is more subtle than i thought.  but it's still
broken with that tweak.  it's broken today too.  pretty sure this code has
always been wrong and crashes, either from writing to read-only memory, or
trying to free an invalid pointer.  it basically needs to be gutted.

glancing at some other ports (m32c & rx), they have the same bugs, so looks
like it started with one bad form, and then copied around.

i think replacing it with buildargv is easiest.
-mike

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH] sim: m32c/rl78/rx: fix command parsing
  2021-05-05 17:30 ` Tom Tromey
  2021-05-05 18:52   ` Mike Frysinger
@ 2021-05-05 19:02   ` Mike Frysinger
  2021-05-05 19:13     ` Tom Tromey
  1 sibling, 1 reply; 8+ messages in thread
From: Mike Frysinger @ 2021-05-05 19:02 UTC (permalink / raw)
  To: gdb-patches

Use buildargv to avoid writing to const memory and freeing invalid
pointers, and to avoid doing any string parsing ourselves.
---
 sim/m32c/gdb-if.c | 36 ++++++++++++------------------------
 sim/rl78/gdb-if.c | 41 +++++++++++++----------------------------
 sim/rx/gdb-if.c   | 38 +++++++++++++-------------------------
 3 files changed, 38 insertions(+), 77 deletions(-)

diff --git a/sim/m32c/gdb-if.c b/sim/m32c/gdb-if.c
index 92e447f17faa..9ced5b9cad38 100644
--- a/sim/m32c/gdb-if.c
+++ b/sim/m32c/gdb-if.c
@@ -27,6 +27,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 #include <ctype.h>
 
 #include "ansidecl.h"
+#include "libiberty.h"
 #include "gdb/callback.h"
 #include "gdb/remote-sim.h"
 #include "gdb/signals.h"
@@ -648,37 +649,24 @@ sim_stop_reason (SIM_DESC sd, enum sim_stop *reason_p, int *sigrc_p)
 void
 sim_do_command (SIM_DESC sd, const char *cmd)
 {
-  const char *args;
-  char *p = strdup (cmd);
+  const char *arg;
+  char **argv = buildargv (cmd);
 
   check_desc (sd);
 
-  /* Skip leading whitespace.  */
-  while (isspace (*p))
-    p++;
-
-  /* Find the extent of the command word.  */
-  for (p = cmd; *p; p++)
-    if (isspace (*p))
-      break;
-
-  /* Null-terminate the command word, and record the start of any
-     further arguments.  */
-  if (*p)
+  if (argv != NULL)
     {
-      *p = '\0';
-      args = p + 1;
-      while (isspace (*args))
-	args++;
+      cmd = argv[0];
+      arg = argv[1];
     }
   else
-    args = p;
+    cmd = arg = "";
 
   if (strcmp (cmd, "trace") == 0)
     {
-      if (strcmp (args, "on") == 0)
+      if (strcmp (arg, "on") == 0)
 	trace = 1;
-      else if (strcmp (args, "off") == 0)
+      else if (strcmp (arg, "off") == 0)
 	trace = 0;
       else
 	printf ("The 'sim trace' command expects 'on' or 'off' "
@@ -686,9 +674,9 @@ sim_do_command (SIM_DESC sd, const char *cmd)
     }
   else if (strcmp (cmd, "verbose") == 0)
     {
-      if (strcmp (args, "on") == 0)
+      if (strcmp (arg, "on") == 0)
 	verbose = 1;
-      else if (strcmp (args, "off") == 0)
+      else if (strcmp (arg, "off") == 0)
 	verbose = 0;
       else
 	printf ("The 'sim verbose' command expects 'on' or 'off'"
@@ -698,7 +686,7 @@ sim_do_command (SIM_DESC sd, const char *cmd)
     printf ("The 'sim' command expects either 'trace' or 'verbose'"
 	    " as a subcommand.\n");
 
-  free (p);
+  freeargv (argv);
 }
 
 char **
diff --git a/sim/rl78/gdb-if.c b/sim/rl78/gdb-if.c
index 7119214113be..280c290a74ce 100644
--- a/sim/rl78/gdb-if.c
+++ b/sim/rl78/gdb-if.c
@@ -27,6 +27,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 #include <stdlib.h>
 
 #include "ansidecl.h"
+#include "libiberty.h"
 #include "gdb/callback.h"
 #include "gdb/remote-sim.h"
 #include "gdb/signals.h"
@@ -533,40 +534,24 @@ sim_stop_reason (SIM_DESC sd, enum sim_stop *reason_p, int *sigrc_p)
 void
 sim_do_command (SIM_DESC sd, const char *cmd)
 {
-  const char *args;
-  char *p = strdup (cmd);
+  const char *arg;
+  char **argv = buildargv (cmd);
 
   check_desc (sd);
 
-  if (cmd == NULL)
+  if (argv != NULL)
     {
-      cmd = "";
-      args = "";
+      cmd = argv[0];
+      arg = argv[1];
     }
   else
-    {
-      /* Skip leading whitespace.  */
-      while (isspace (*p))
-	p++;
-
-      /* Null-terminate the command word, and record the start of any
-	 further arguments.  */
-      if (*p)
-	{
-	  *p = '\0';
-	  args = p + 1;
-	  while (isspace (*args))
-	    args++;
-	}
-      else
-	args = p;
-    }
+    cmd = arg = "";
 
   if (strcmp (cmd, "trace") == 0)
     {
-      if (strcmp (args, "on") == 0)
+      if (strcmp (arg, "on") == 0)
 	trace = 1;
-      else if (strcmp (args, "off") == 0)
+      else if (strcmp (arg, "off") == 0)
 	trace = 0;
       else
 	printf ("The 'sim trace' command expects 'on' or 'off' "
@@ -574,11 +559,11 @@ sim_do_command (SIM_DESC sd, const char *cmd)
     }
   else if (strcmp (cmd, "verbose") == 0)
     {
-      if (strcmp (args, "on") == 0)
+      if (strcmp (arg, "on") == 0)
 	verbose = 1;
-      else if (strcmp (args, "noisy") == 0)
+      else if (strcmp (arg, "noisy") == 0)
 	verbose = 2;
-      else if (strcmp (args, "off") == 0)
+      else if (strcmp (arg, "off") == 0)
 	verbose = 0;
       else
 	printf ("The 'sim verbose' command expects 'on', 'noisy', or 'off'"
@@ -588,7 +573,7 @@ sim_do_command (SIM_DESC sd, const char *cmd)
     printf ("The 'sim' command expects either 'trace' or 'verbose'"
 	    " as a subcommand.\n");
 
-  free (p);
+  freeargv (argv);
 }
 
 /* Stub for command completion.  */
diff --git a/sim/rx/gdb-if.c b/sim/rx/gdb-if.c
index 3d052e62baa2..ce2b9a0a4918 100644
--- a/sim/rx/gdb-if.c
+++ b/sim/rx/gdb-if.c
@@ -27,6 +27,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 #include <stdlib.h>
 
 #include "ansidecl.h"
+#include "libiberty.h"
 #include "gdb/callback.h"
 #include "gdb/remote-sim.h"
 #include "gdb/signals.h"
@@ -794,37 +795,24 @@ sim_stop_reason (SIM_DESC sd, enum sim_stop *reason_p, int *sigrc_p)
 void
 sim_do_command (SIM_DESC sd, const char *cmd)
 {
-  const char *args;
-  char *p = strdup (cmd);
+  const char *arg;
+  char **argv = buildargv (cmd);
 
   check_desc (sd);
 
-  /* Skip leading whitespace.  */
-  while (isspace (*p))
-    p++;
-
-  /* Find the extent of the command word.  */
-  for (; *p != '\0'; p++)
-    if (isspace (*p))
-      break;
-
-  /* Null-terminate the command word, and record the start of any
-     further arguments.  */
-  if (*p != '\0')
+  if (argv != NULL)
     {
-      *p = '\0';
-      args = p + 1;
-      while (isspace (*args))
-	args++;
+      cmd = argv[0];
+      arg = argv[1];
     }
   else
-    args = p;
+    cmd = arg = "";
 
   if (strcmp (cmd, "trace") == 0)
     {
-      if (strcmp (args, "on") == 0)
+      if (strcmp (arg, "on") == 0)
 	trace = 1;
-      else if (strcmp (args, "off") == 0)
+      else if (strcmp (arg, "off") == 0)
 	trace = 0;
       else
 	printf ("The 'sim trace' command expects 'on' or 'off' "
@@ -832,11 +820,11 @@ sim_do_command (SIM_DESC sd, const char *cmd)
     }
   else if (strcmp (cmd, "verbose") == 0)
     {
-      if (strcmp (args, "on") == 0)
+      if (strcmp (arg, "on") == 0)
 	verbose = 1;
-      else if (strcmp (args, "noisy") == 0)
+      else if (strcmp (arg, "noisy") == 0)
 	verbose = 2;
-      else if (strcmp (args, "off") == 0)
+      else if (strcmp (arg, "off") == 0)
 	verbose = 0;
       else
 	printf ("The 'sim verbose' command expects 'on', 'noisy', or 'off'"
@@ -846,7 +834,7 @@ sim_do_command (SIM_DESC sd, const char *cmd)
     printf ("The 'sim' command expects either 'trace' or 'verbose'"
 	    " as a subcommand.\n");
 
-  free (p);
+  freeargv (argv);
 }
 
 char **
-- 
2.31.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH/committed] sim: rl78: clean up various warnings
  2021-05-05 18:52   ` Mike Frysinger
@ 2021-05-05 19:05     ` Tom Tromey
  0 siblings, 0 replies; 8+ messages in thread
From: Tom Tromey @ 2021-05-05 19:05 UTC (permalink / raw)
  To: Tom Tromey; +Cc: Mike Frysinger via Gdb-patches

>>>>> "Mike" == Mike Frysinger <vapier@gentoo.org> writes:

Mike> i think replacing it with buildargv is easiest.

Makes sense to me.  Could also get rid of the strdup then as well.

Tom

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] sim: m32c/rl78/rx: fix command parsing
  2021-05-05 19:02   ` [PATCH] sim: m32c/rl78/rx: fix command parsing Mike Frysinger
@ 2021-05-05 19:13     ` Tom Tromey
  2021-05-05 23:03       ` [PATCH v2] " Mike Frysinger
  2021-05-05 23:06       ` [PATCH v3] " Mike Frysinger
  0 siblings, 2 replies; 8+ messages in thread
From: Tom Tromey @ 2021-05-05 19:13 UTC (permalink / raw)
  To: Mike Frysinger via Gdb-patches

>>>>> "Mike" == Mike Frysinger via Gdb-patches <gdb-patches@sourceware.org> writes:

Mike> Use buildargv to avoid writing to const memory and freeing invalid
Mike> pointers, and to avoid doing any string parsing ourselves.

Thank you.

Mike> +  char **argv = buildargv (cmd);
 
Mike> +      cmd = argv[0];
Mike> +      arg = argv[1];

I think this should also check that argv[0] and argv[1] != NULL somewhere.
Otherwise malformed commands like "monitor" or "monitor trace" may crash.

Tom

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v2] sim: m32c/rl78/rx: fix command parsing
  2021-05-05 19:13     ` Tom Tromey
@ 2021-05-05 23:03       ` Mike Frysinger
  2021-05-05 23:06       ` [PATCH v3] " Mike Frysinger
  1 sibling, 0 replies; 8+ messages in thread
From: Mike Frysinger @ 2021-05-05 23:03 UTC (permalink / raw)
  To: gdb-patches

Use buildargv to avoid writing to const memory and freeing invalid
pointers, and to avoid doing any string parsing ourselves.
---
 sim/m32c/gdb-if.c | 36 ++++++++++++------------------------
 sim/rl78/gdb-if.c | 41 +++++++++++++----------------------------
 sim/rx/gdb-if.c   | 38 +++++++++++++-------------------------
 3 files changed, 38 insertions(+), 77 deletions(-)

diff --git a/sim/m32c/gdb-if.c b/sim/m32c/gdb-if.c
index 92e447f17faa..9ced5b9cad38 100644
--- a/sim/m32c/gdb-if.c
+++ b/sim/m32c/gdb-if.c
@@ -27,6 +27,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 #include <ctype.h>
 
 #include "ansidecl.h"
+#include "libiberty.h"
 #include "gdb/callback.h"
 #include "gdb/remote-sim.h"
 #include "gdb/signals.h"
@@ -648,37 +649,24 @@ sim_stop_reason (SIM_DESC sd, enum sim_stop *reason_p, int *sigrc_p)
 void
 sim_do_command (SIM_DESC sd, const char *cmd)
 {
-  const char *args;
-  char *p = strdup (cmd);
+  const char *arg;
+  char **argv = buildargv (cmd);
 
   check_desc (sd);
 
-  /* Skip leading whitespace.  */
-  while (isspace (*p))
-    p++;
-
-  /* Find the extent of the command word.  */
-  for (p = cmd; *p; p++)
-    if (isspace (*p))
-      break;
-
-  /* Null-terminate the command word, and record the start of any
-     further arguments.  */
-  if (*p)
+  if (argv != NULL)
     {
-      *p = '\0';
-      args = p + 1;
-      while (isspace (*args))
-	args++;
+      cmd = argv[0];
+      arg = argv[1];
     }
   else
-    args = p;
+    cmd = arg = "";
 
   if (strcmp (cmd, "trace") == 0)
     {
-      if (strcmp (args, "on") == 0)
+      if (strcmp (arg, "on") == 0)
 	trace = 1;
-      else if (strcmp (args, "off") == 0)
+      else if (strcmp (arg, "off") == 0)
 	trace = 0;
       else
 	printf ("The 'sim trace' command expects 'on' or 'off' "
@@ -686,9 +674,9 @@ sim_do_command (SIM_DESC sd, const char *cmd)
     }
   else if (strcmp (cmd, "verbose") == 0)
     {
-      if (strcmp (args, "on") == 0)
+      if (strcmp (arg, "on") == 0)
 	verbose = 1;
-      else if (strcmp (args, "off") == 0)
+      else if (strcmp (arg, "off") == 0)
 	verbose = 0;
       else
 	printf ("The 'sim verbose' command expects 'on' or 'off'"
@@ -698,7 +686,7 @@ sim_do_command (SIM_DESC sd, const char *cmd)
     printf ("The 'sim' command expects either 'trace' or 'verbose'"
 	    " as a subcommand.\n");
 
-  free (p);
+  freeargv (argv);
 }
 
 char **
diff --git a/sim/rl78/gdb-if.c b/sim/rl78/gdb-if.c
index 7119214113be..280c290a74ce 100644
--- a/sim/rl78/gdb-if.c
+++ b/sim/rl78/gdb-if.c
@@ -27,6 +27,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 #include <stdlib.h>
 
 #include "ansidecl.h"
+#include "libiberty.h"
 #include "gdb/callback.h"
 #include "gdb/remote-sim.h"
 #include "gdb/signals.h"
@@ -533,40 +534,24 @@ sim_stop_reason (SIM_DESC sd, enum sim_stop *reason_p, int *sigrc_p)
 void
 sim_do_command (SIM_DESC sd, const char *cmd)
 {
-  const char *args;
-  char *p = strdup (cmd);
+  const char *arg;
+  char **argv = buildargv (cmd);
 
   check_desc (sd);
 
-  if (cmd == NULL)
+  if (argv != NULL)
     {
-      cmd = "";
-      args = "";
+      cmd = argv[0];
+      arg = argv[1];
     }
   else
-    {
-      /* Skip leading whitespace.  */
-      while (isspace (*p))
-	p++;
-
-      /* Null-terminate the command word, and record the start of any
-	 further arguments.  */
-      if (*p)
-	{
-	  *p = '\0';
-	  args = p + 1;
-	  while (isspace (*args))
-	    args++;
-	}
-      else
-	args = p;
-    }
+    cmd = arg = "";
 
   if (strcmp (cmd, "trace") == 0)
     {
-      if (strcmp (args, "on") == 0)
+      if (strcmp (arg, "on") == 0)
 	trace = 1;
-      else if (strcmp (args, "off") == 0)
+      else if (strcmp (arg, "off") == 0)
 	trace = 0;
       else
 	printf ("The 'sim trace' command expects 'on' or 'off' "
@@ -574,11 +559,11 @@ sim_do_command (SIM_DESC sd, const char *cmd)
     }
   else if (strcmp (cmd, "verbose") == 0)
     {
-      if (strcmp (args, "on") == 0)
+      if (strcmp (arg, "on") == 0)
 	verbose = 1;
-      else if (strcmp (args, "noisy") == 0)
+      else if (strcmp (arg, "noisy") == 0)
 	verbose = 2;
-      else if (strcmp (args, "off") == 0)
+      else if (strcmp (arg, "off") == 0)
 	verbose = 0;
       else
 	printf ("The 'sim verbose' command expects 'on', 'noisy', or 'off'"
@@ -588,7 +573,7 @@ sim_do_command (SIM_DESC sd, const char *cmd)
     printf ("The 'sim' command expects either 'trace' or 'verbose'"
 	    " as a subcommand.\n");
 
-  free (p);
+  freeargv (argv);
 }
 
 /* Stub for command completion.  */
diff --git a/sim/rx/gdb-if.c b/sim/rx/gdb-if.c
index 3d052e62baa2..ce2b9a0a4918 100644
--- a/sim/rx/gdb-if.c
+++ b/sim/rx/gdb-if.c
@@ -27,6 +27,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 #include <stdlib.h>
 
 #include "ansidecl.h"
+#include "libiberty.h"
 #include "gdb/callback.h"
 #include "gdb/remote-sim.h"
 #include "gdb/signals.h"
@@ -794,37 +795,24 @@ sim_stop_reason (SIM_DESC sd, enum sim_stop *reason_p, int *sigrc_p)
 void
 sim_do_command (SIM_DESC sd, const char *cmd)
 {
-  const char *args;
-  char *p = strdup (cmd);
+  const char *arg;
+  char **argv = buildargv (cmd);
 
   check_desc (sd);
 
-  /* Skip leading whitespace.  */
-  while (isspace (*p))
-    p++;
-
-  /* Find the extent of the command word.  */
-  for (; *p != '\0'; p++)
-    if (isspace (*p))
-      break;
-
-  /* Null-terminate the command word, and record the start of any
-     further arguments.  */
-  if (*p != '\0')
+  if (argv != NULL)
     {
-      *p = '\0';
-      args = p + 1;
-      while (isspace (*args))
-	args++;
+      cmd = argv[0];
+      arg = argv[1];
     }
   else
-    args = p;
+    cmd = arg = "";
 
   if (strcmp (cmd, "trace") == 0)
     {
-      if (strcmp (args, "on") == 0)
+      if (strcmp (arg, "on") == 0)
 	trace = 1;
-      else if (strcmp (args, "off") == 0)
+      else if (strcmp (arg, "off") == 0)
 	trace = 0;
       else
 	printf ("The 'sim trace' command expects 'on' or 'off' "
@@ -832,11 +820,11 @@ sim_do_command (SIM_DESC sd, const char *cmd)
     }
   else if (strcmp (cmd, "verbose") == 0)
     {
-      if (strcmp (args, "on") == 0)
+      if (strcmp (arg, "on") == 0)
 	verbose = 1;
-      else if (strcmp (args, "noisy") == 0)
+      else if (strcmp (arg, "noisy") == 0)
 	verbose = 2;
-      else if (strcmp (args, "off") == 0)
+      else if (strcmp (arg, "off") == 0)
 	verbose = 0;
       else
 	printf ("The 'sim verbose' command expects 'on', 'noisy', or 'off'"
@@ -846,7 +834,7 @@ sim_do_command (SIM_DESC sd, const char *cmd)
     printf ("The 'sim' command expects either 'trace' or 'verbose'"
 	    " as a subcommand.\n");
 
-  free (p);
+  freeargv (argv);
 }
 
 char **
-- 
2.31.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v3] sim: m32c/rl78/rx: fix command parsing
  2021-05-05 19:13     ` Tom Tromey
  2021-05-05 23:03       ` [PATCH v2] " Mike Frysinger
@ 2021-05-05 23:06       ` Mike Frysinger
  1 sibling, 0 replies; 8+ messages in thread
From: Mike Frysinger @ 2021-05-05 23:06 UTC (permalink / raw)
  To: gdb-patches

Use buildargv to avoid writing to const memory and freeing invalid
pointers, and to avoid doing any string parsing ourselves.
---
 sim/m32c/gdb-if.c | 39 ++++++++++++++-------------------------
 sim/rl78/gdb-if.c | 44 +++++++++++++++-----------------------------
 sim/rx/gdb-if.c   | 41 +++++++++++++++--------------------------
 3 files changed, 44 insertions(+), 80 deletions(-)

diff --git a/sim/m32c/gdb-if.c b/sim/m32c/gdb-if.c
index 92e447f17faa..c2aff064ff0f 100644
--- a/sim/m32c/gdb-if.c
+++ b/sim/m32c/gdb-if.c
@@ -27,6 +27,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 #include <ctype.h>
 
 #include "ansidecl.h"
+#include "libiberty.h"
 #include "gdb/callback.h"
 #include "gdb/remote-sim.h"
 #include "gdb/signals.h"
@@ -648,37 +649,25 @@ sim_stop_reason (SIM_DESC sd, enum sim_stop *reason_p, int *sigrc_p)
 void
 sim_do_command (SIM_DESC sd, const char *cmd)
 {
-  const char *args;
-  char *p = strdup (cmd);
+  const char *arg;
+  char **argv = buildargv (cmd);
 
   check_desc (sd);
 
-  /* Skip leading whitespace.  */
-  while (isspace (*p))
-    p++;
-
-  /* Find the extent of the command word.  */
-  for (p = cmd; *p; p++)
-    if (isspace (*p))
-      break;
-
-  /* Null-terminate the command word, and record the start of any
-     further arguments.  */
-  if (*p)
+  cmd = arg = "";
+  if (argv != NULL)
     {
-      *p = '\0';
-      args = p + 1;
-      while (isspace (*args))
-	args++;
+      if (argv[0] != NULL)
+	cmd = argv[0];
+      if (argv[1] != NULL)
+	arg = argv[1];
     }
-  else
-    args = p;
 
   if (strcmp (cmd, "trace") == 0)
     {
-      if (strcmp (args, "on") == 0)
+      if (strcmp (arg, "on") == 0)
 	trace = 1;
-      else if (strcmp (args, "off") == 0)
+      else if (strcmp (arg, "off") == 0)
 	trace = 0;
       else
 	printf ("The 'sim trace' command expects 'on' or 'off' "
@@ -686,9 +675,9 @@ sim_do_command (SIM_DESC sd, const char *cmd)
     }
   else if (strcmp (cmd, "verbose") == 0)
     {
-      if (strcmp (args, "on") == 0)
+      if (strcmp (arg, "on") == 0)
 	verbose = 1;
-      else if (strcmp (args, "off") == 0)
+      else if (strcmp (arg, "off") == 0)
 	verbose = 0;
       else
 	printf ("The 'sim verbose' command expects 'on' or 'off'"
@@ -698,7 +687,7 @@ sim_do_command (SIM_DESC sd, const char *cmd)
     printf ("The 'sim' command expects either 'trace' or 'verbose'"
 	    " as a subcommand.\n");
 
-  free (p);
+  freeargv (argv);
 }
 
 char **
diff --git a/sim/rl78/gdb-if.c b/sim/rl78/gdb-if.c
index 7119214113be..f4b6754f5873 100644
--- a/sim/rl78/gdb-if.c
+++ b/sim/rl78/gdb-if.c
@@ -27,6 +27,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 #include <stdlib.h>
 
 #include "ansidecl.h"
+#include "libiberty.h"
 #include "gdb/callback.h"
 #include "gdb/remote-sim.h"
 #include "gdb/signals.h"
@@ -533,40 +534,25 @@ sim_stop_reason (SIM_DESC sd, enum sim_stop *reason_p, int *sigrc_p)
 void
 sim_do_command (SIM_DESC sd, const char *cmd)
 {
-  const char *args;
-  char *p = strdup (cmd);
+  const char *arg;
+  char **argv = buildargv (cmd);
 
   check_desc (sd);
 
-  if (cmd == NULL)
+  cmd = arg = "";
+  if (argv != NULL)
     {
-      cmd = "";
-      args = "";
-    }
-  else
-    {
-      /* Skip leading whitespace.  */
-      while (isspace (*p))
-	p++;
-
-      /* Null-terminate the command word, and record the start of any
-	 further arguments.  */
-      if (*p)
-	{
-	  *p = '\0';
-	  args = p + 1;
-	  while (isspace (*args))
-	    args++;
-	}
-      else
-	args = p;
+      if (argv[0] != NULL)
+	cmd = argv[0];
+      if (argv[1] != NULL)
+	arg = argv[1];
     }
 
   if (strcmp (cmd, "trace") == 0)
     {
-      if (strcmp (args, "on") == 0)
+      if (strcmp (arg, "on") == 0)
 	trace = 1;
-      else if (strcmp (args, "off") == 0)
+      else if (strcmp (arg, "off") == 0)
 	trace = 0;
       else
 	printf ("The 'sim trace' command expects 'on' or 'off' "
@@ -574,11 +560,11 @@ sim_do_command (SIM_DESC sd, const char *cmd)
     }
   else if (strcmp (cmd, "verbose") == 0)
     {
-      if (strcmp (args, "on") == 0)
+      if (strcmp (arg, "on") == 0)
 	verbose = 1;
-      else if (strcmp (args, "noisy") == 0)
+      else if (strcmp (arg, "noisy") == 0)
 	verbose = 2;
-      else if (strcmp (args, "off") == 0)
+      else if (strcmp (arg, "off") == 0)
 	verbose = 0;
       else
 	printf ("The 'sim verbose' command expects 'on', 'noisy', or 'off'"
@@ -588,7 +574,7 @@ sim_do_command (SIM_DESC sd, const char *cmd)
     printf ("The 'sim' command expects either 'trace' or 'verbose'"
 	    " as a subcommand.\n");
 
-  free (p);
+  freeargv (argv);
 }
 
 /* Stub for command completion.  */
diff --git a/sim/rx/gdb-if.c b/sim/rx/gdb-if.c
index 3d052e62baa2..ec4191095888 100644
--- a/sim/rx/gdb-if.c
+++ b/sim/rx/gdb-if.c
@@ -27,6 +27,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 #include <stdlib.h>
 
 #include "ansidecl.h"
+#include "libiberty.h"
 #include "gdb/callback.h"
 #include "gdb/remote-sim.h"
 #include "gdb/signals.h"
@@ -794,37 +795,25 @@ sim_stop_reason (SIM_DESC sd, enum sim_stop *reason_p, int *sigrc_p)
 void
 sim_do_command (SIM_DESC sd, const char *cmd)
 {
-  const char *args;
-  char *p = strdup (cmd);
+  const char *arg;
+  char **argv = buildargv (cmd);
 
   check_desc (sd);
 
-  /* Skip leading whitespace.  */
-  while (isspace (*p))
-    p++;
-
-  /* Find the extent of the command word.  */
-  for (; *p != '\0'; p++)
-    if (isspace (*p))
-      break;
-
-  /* Null-terminate the command word, and record the start of any
-     further arguments.  */
-  if (*p != '\0')
+  cmd = arg = "";
+  if (argv != NULL)
     {
-      *p = '\0';
-      args = p + 1;
-      while (isspace (*args))
-	args++;
+      if (argv[0] != NULL)
+	cmd = argv[0];
+      if (argv[1] != NULL)
+	arg = argv[1];
     }
-  else
-    args = p;
 
   if (strcmp (cmd, "trace") == 0)
     {
-      if (strcmp (args, "on") == 0)
+      if (strcmp (arg, "on") == 0)
 	trace = 1;
-      else if (strcmp (args, "off") == 0)
+      else if (strcmp (arg, "off") == 0)
 	trace = 0;
       else
 	printf ("The 'sim trace' command expects 'on' or 'off' "
@@ -832,11 +821,11 @@ sim_do_command (SIM_DESC sd, const char *cmd)
     }
   else if (strcmp (cmd, "verbose") == 0)
     {
-      if (strcmp (args, "on") == 0)
+      if (strcmp (arg, "on") == 0)
 	verbose = 1;
-      else if (strcmp (args, "noisy") == 0)
+      else if (strcmp (arg, "noisy") == 0)
 	verbose = 2;
-      else if (strcmp (args, "off") == 0)
+      else if (strcmp (arg, "off") == 0)
 	verbose = 0;
       else
 	printf ("The 'sim verbose' command expects 'on', 'noisy', or 'off'"
@@ -846,7 +835,7 @@ sim_do_command (SIM_DESC sd, const char *cmd)
     printf ("The 'sim' command expects either 'trace' or 'verbose'"
 	    " as a subcommand.\n");
 
-  free (p);
+  freeargv (argv);
 }
 
 char **
-- 
2.31.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2021-05-05 23:06 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-05  3:04 [PATCH/committed] sim: rl78: clean up various warnings Mike Frysinger
2021-05-05 17:30 ` Tom Tromey
2021-05-05 18:52   ` Mike Frysinger
2021-05-05 19:05     ` Tom Tromey
2021-05-05 19:02   ` [PATCH] sim: m32c/rl78/rx: fix command parsing Mike Frysinger
2021-05-05 19:13     ` Tom Tromey
2021-05-05 23:03       ` [PATCH v2] " Mike Frysinger
2021-05-05 23:06       ` [PATCH v3] " Mike Frysinger

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).