From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id HvgQOgslk2DqDwAAWB0awg (envelope-from ) for ; Wed, 05 May 2021 19:06:51 -0400 Received: by simark.ca (Postfix, from userid 112) id DFD481F11C; Wed, 5 May 2021 19:06:51 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-0.7 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,RDNS_DYNAMIC,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 332051E813 for ; Wed, 5 May 2021 19:06:51 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DCA57385802D; Wed, 5 May 2021 23:06:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DCA57385802D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1620256010; bh=P7STyLms5VZxwyKm/yHubX3ewXpD+S5ggzrcmsdLthg=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=mUHcEjtjB6IZuIWkaTi/aeGRMx6a2A+e9OsnZP0kkfpOpJQnVC+yq9zU+vhI5V2Q1 buInh+vUbuFCPlIKR3TKBipS3ubk74i1eaca/8lYWA5P1Ytw3JZGN9jIkrJZDgcEtn 3kuWsK+w/CuZTWx//lkUMQcVNPXZO2ubtWPqIVSk= Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by sourceware.org (Postfix) with ESMTP id 6D471385802D for ; Wed, 5 May 2021 23:06:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 6D471385802D Received: from vapier.lan (localhost [127.0.0.1]) by smtp.gentoo.org (Postfix) with ESMTP id 5B1DB33FE49 for ; Wed, 5 May 2021 23:06:46 +0000 (UTC) To: gdb-patches@sourceware.org Subject: [PATCH v3] sim: m32c/rl78/rx: fix command parsing Date: Wed, 5 May 2021 19:06:44 -0400 Message-Id: <20210505230644.17909-1-vapier@gentoo.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <877dkdq9hr.fsf@tromey.com> References: <877dkdq9hr.fsf@tromey.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Mike Frysinger via Gdb-patches Reply-To: Mike Frysinger Errors-To: gdb-patches-bounces@sourceware.org Sender: "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 . */ #include #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 . */ #include #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 . */ #include #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