From patchwork Sat Jul 20 01:44:10 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wayne Xia X-Patchwork-Id: 260394 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 114052C0095 for ; Sat, 20 Jul 2013 11:53:50 +1000 (EST) Received: from localhost ([::1]:54269 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V0MMl-00043F-U9 for incoming@patchwork.ozlabs.org; Fri, 19 Jul 2013 21:53:47 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55780) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V0MFq-0003JI-5H for qemu-devel@nongnu.org; Fri, 19 Jul 2013 21:46:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1V0MFp-0002iV-6K for qemu-devel@nongnu.org; Fri, 19 Jul 2013 21:46:38 -0400 Received: from e28smtp03.in.ibm.com ([122.248.162.3]:52339) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V0MFo-0002iD-Iw for qemu-devel@nongnu.org; Fri, 19 Jul 2013 21:46:37 -0400 Received: from /spool/local by e28smtp03.in.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Sat, 20 Jul 2013 07:09:50 +0530 Received: from d28dlp01.in.ibm.com (9.184.220.126) by e28smtp03.in.ibm.com (192.168.1.133) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Sat, 20 Jul 2013 07:09:49 +0530 Received: from d28relay04.in.ibm.com (d28relay04.in.ibm.com [9.184.220.61]) by d28dlp01.in.ibm.com (Postfix) with ESMTP id 17776E0053 for ; Sat, 20 Jul 2013 07:16:29 +0530 (IST) Received: from d28av01.in.ibm.com (d28av01.in.ibm.com [9.184.220.63]) by d28relay04.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id r6K1kSvC20840528 for ; Sat, 20 Jul 2013 07:16:28 +0530 Received: from d28av01.in.ibm.com (loopback [127.0.0.1]) by d28av01.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id r6K1kU85022173 for ; Sat, 20 Jul 2013 01:46:31 GMT Received: from RH63Wenchao ([9.77.181.206]) by d28av01.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id r6K1iXRu018239; Sat, 20 Jul 2013 01:46:29 GMT From: Wenchao Xia To: qemu-devel@nongnu.org Date: Sat, 20 Jul 2013 09:44:10 +0800 Message-Id: <1374284656-12025-9-git-send-email-xiawenc@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1374284656-12025-1-git-send-email-xiawenc@linux.vnet.ibm.com> References: <1374284656-12025-1-git-send-email-xiawenc@linux.vnet.ibm.com> X-TM-AS-MML: No X-Content-Scanned: Fidelis XPS MAILER x-cbid: 13072001-3864-0000-0000-0000092AD5F8 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 122.248.162.3 Cc: pbonzini@redhat.com, Wenchao Xia , armbru@redhat.com, lcapitulino@redhat.com Subject: [Qemu-devel] [PATCH V7 08/13] monitor: refine parse_cmdline() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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. One change of this function is that, when the valid args in input exceed the limit of MAX_ARGS, it fails now, instead of return with MAX_ARGS of parsed args in old code. This should not impact much since it is rare that user input many args in monitor's "help" and auto complete scenario. Signed-off-by: Wenchao Xia Reviewed-by: Eric Blake --- monitor.c | 51 ++++++++++++++++++++++++++++++++++++++++----------- 1 files changed, 40 insertions(+), 11 deletions(-) diff --git a/monitor.c b/monitor.c index 177eb85..b68e145 100644 --- a/monitor.c +++ b/monitor.c @@ -809,9 +809,33 @@ 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; + + assert(nb_args <= MAX_ARGS); + + 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. If cmdline contains more, it will + * return with failure. + */ +static int parse_cmdline(const char *cmdline, + int *pnb_args, char **args) { const char *p; int nb_args, ret; @@ -827,16 +851,21 @@ static void parse_cmdline(const char *cmdline, break; } if (nb_args >= MAX_ARGS) { - break; + goto fail; } 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, @@ -4152,7 +4181,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]); @@ -4242,9 +4273,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)