From: Mike Frysinger <vapier@gentoo.org>
To: gdb-patches@sourceware.org
Subject: [PATCH v3] sim: m32c/rl78/rx: fix command parsing
Date: Wed, 5 May 2021 19:06:44 -0400 [thread overview]
Message-ID: <20210505230644.17909-1-vapier@gentoo.org> (raw)
In-Reply-To: <877dkdq9hr.fsf@tromey.com>
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
prev parent reply other threads:[~2021-05-05 23:06 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
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 ` Mike Frysinger [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210505230644.17909-1-vapier@gentoo.org \
--to=vapier@gentoo.org \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).