From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:45725) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UzdXs-00062S-Q4 for qemu-devel@nongnu.org; Wed, 17 Jul 2013 22:02:18 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UzdXr-0005oy-Kf for qemu-devel@nongnu.org; Wed, 17 Jul 2013 22:02:16 -0400 Received: from e28smtp01.in.ibm.com ([122.248.162.1]:50991) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UzdXq-0005oi-IN for qemu-devel@nongnu.org; Wed, 17 Jul 2013 22:02:15 -0400 Received: from /spool/local by e28smtp01.in.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 18 Jul 2013 07:24:28 +0530 Received: from d28relay02.in.ibm.com (d28relay02.in.ibm.com [9.184.220.59]) by d28dlp01.in.ibm.com (Postfix) with ESMTP id A123AE004F for ; Thu, 18 Jul 2013 07:32:02 +0530 (IST) Received: from d28av03.in.ibm.com (d28av03.in.ibm.com [9.184.220.65]) by d28relay02.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id r6I22sg324576186 for ; Thu, 18 Jul 2013 07:32:55 +0530 Received: from d28av03.in.ibm.com (loopback [127.0.0.1]) by d28av03.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id r6I225jQ030922 for ; Thu, 18 Jul 2013 12:02:05 +1000 Message-ID: <51E74C86.50507@linux.vnet.ibm.com> Date: Thu, 18 Jul 2013 10:01:42 +0800 From: Wenchao Xia MIME-Version: 1.0 References: <1373512429-17865-1-git-send-email-xiawenc@linux.vnet.ibm.com> <1373512429-17865-9-git-send-email-xiawenc@linux.vnet.ibm.com> <20130717153953.414cca1e@redhat.com> In-Reply-To: <20130717153953.414cca1e@redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: [Qemu-devel] [PATCH V6 08/13] monitor: refine parse_cmdline() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Luiz Capitulino Cc: pbonzini@redhat.com, qemu-devel@nongnu.org, armbru@redhat.com 于 2013-7-18 3:39, Luiz Capitulino 写道: > On Thu, 11 Jul 2013 11:13:44 +0800 > Wenchao Xia wrote: > >> Since this function will be used by help_cmd() later, so improve >> it to make it more generic and easier to use. free_cmdline_args() >> is added to as paired function to free the result. >> >> Signed-off-by: Wenchao Xia >> --- >> monitor.c | 52 ++++++++++++++++++++++++++++++++++++++-------------- >> 1 files changed, 38 insertions(+), 14 deletions(-) >> >> diff --git a/monitor.c b/monitor.c >> index db63223..2d4f699 100644 >> --- a/monitor.c >> +++ b/monitor.c >> @@ -801,9 +801,31 @@ static int get_str(char *buf, int buf_size, const char **pp) >> >> #define MAX_ARGS 16 >> >> -/* NOTE: this parser is an approximate form of the real command parser */ >> -static void parse_cmdline(const char *cmdline, >> - int *pnb_args, char **args) >> +static void free_cmdline_args(char **args, int nb_args) >> +{ >> + int i; >> + >> + nb_args = nb_args < MAX_ARGS ? nb_args : MAX_ARGS; > > Why is this needed? nb_args is guaranteed to be at most MAX_ARGS, > isn't it? If you really want to ensure it, then you can assert() it. > I'll use assert(). >> + for (i = 0; i < nb_args; i++) { >> + g_free(args[i]); >> + } >> + >> +} >> + >> +/* >> + * Parse the command line to get valid args. >> + * @cmdline: command line to be parsed. >> + * @pnb_args: location to store the number of args, must NOT be NULL. >> + * @args: location to store the args, which should be freed by caller, must >> + * NOT be NULL. >> + * >> + * Returns 0 on success, negative on failure. >> + * >> + * NOTE: this parser is an approximate form of the real command parser. Number >> + * of args have a limit of MAX_ARGS. >> + */ >> +static int parse_cmdline(const char *cmdline, >> + int *pnb_args, char **args) >> { >> const char *p; >> int nb_args, ret; >> @@ -811,24 +833,26 @@ static void parse_cmdline(const char *cmdline, >> >> p = cmdline; >> nb_args = 0; >> - for (;;) { >> + while (nb_args < MAX_ARGS) { > > I think it would be better to fail if nb_args > MAX_ARGS. Well, ideally will fail the function if nb_args > MAX_ARGS in next version. > we shouldn't have any artificial limit, but I'd guess that dropping > MAX_ARGS goes a bit to far for this series' scope. > >> while (qemu_isspace(*p)) { >> p++; >> } >> if (*p == '\0') { >> break; >> } >> - if (nb_args >= MAX_ARGS) { >> - break; >> - } >> ret = get_str(buf, sizeof(buf), &p); >> - args[nb_args] = g_strdup(buf); >> - nb_args++; >> if (ret < 0) { >> - break; >> + goto fail; >> } >> + args[nb_args] = g_strdup(buf); >> + nb_args++; >> } >> *pnb_args = nb_args; >> + return 0; >> + >> + fail: >> + free_cmdline_args(args, nb_args); >> + return -1; >> } >> >> static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds, >> @@ -4144,7 +4168,9 @@ static void monitor_find_completion(Monitor *mon, >> const mon_cmd_t *cmd; >> MonitorBlockComplete mbs; >> >> - parse_cmdline(cmdline, &nb_args, args); >> + if (parse_cmdline(cmdline, &nb_args, args) < 0) { >> + return; >> + } >> #ifdef DEBUG_COMPLETION >> for (i = 0; i < nb_args; i++) { >> monitor_printf(mon, "arg%d = '%s'\n", i, args[i]); >> @@ -4234,9 +4260,7 @@ static void monitor_find_completion(Monitor *mon, >> } >> >> cleanup: >> - for (i = 0; i < nb_args; i++) { >> - g_free(args[i]); >> - } >> + free_cmdline_args(args, nb_args); >> } >> >> static int monitor_can_read(void *opaque) > -- Best Regards Wenchao Xia