Received: from mailman by lists.gnu.org with archive (Exim 4.43) id 1MNxqY-00022O-Bd for mharc-grub-devel@gnu.org; Mon, 06 Jul 2009 19:43:42 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MNxqW-0001yW-P7 for grub-devel@gnu.org; Mon, 06 Jul 2009 19:43:40 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MNxqS-0001sa-05 for grub-devel@gnu.org; Mon, 06 Jul 2009 19:43:40 -0400 Received: from [199.232.76.173] (port=38305 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MNxqR-0001sS-Td for grub-devel@gnu.org; Mon, 06 Jul 2009 19:43:35 -0400 Received: from c60.cesmail.net ([216.154.195.49]:56425) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.60) (envelope-from ) id 1MNxqR-0008T1-2w for grub-devel@gnu.org; Mon, 06 Jul 2009 19:43:35 -0400 Received: from unknown (HELO smtprelay2.cesmail.net) ([192.168.1.112]) by c60.cesmail.net with ESMTP; 06 Jul 2009 19:43:32 -0400 Received: from mj.roinet.com (static-72-92-88-10.phlapa.fios.verizon.net [72.92.88.10]) by smtprelay2.cesmail.net (Postfix) with ESMTPSA id CE13A34C6A for ; Mon, 6 Jul 2009 19:51:46 -0400 (EDT) From: Pavel Roskin To: grub-devel@gnu.org Date: Mon, 06 Jul 2009 19:43:31 -0400 Message-ID: <20090706234330.31516.22217.stgit@mj.roinet.com> User-Agent: StGIT/0.14.3 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-detected-operating-system: by monty-python.gnu.org: Genre and OS details not recognized. Subject: [PATCH] Fix using freed memory for long option arguments X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: The development of GRUB 2 List-Id: The development of GRUB 2 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 06 Jul 2009 23:43:41 -0000 ChangeLog: * lib/arg.c (find_long_option): Remove. (find_long): Add `len' argument, make `s' const char *. (grub_arg_parse): Parse long options in place, not in a temporary buffer. --- lib/arg.c | 37 ++++++++++--------------------------- 1 files changed, 10 insertions(+), 27 deletions(-) diff --git a/lib/arg.c b/lib/arg.c index e614048..5f649d0 100644 --- a/lib/arg.c +++ b/lib/arg.c @@ -76,21 +76,8 @@ find_short (const struct grub_arg_option *options, char c) return found; } -static char * -find_long_option (char *s) -{ - char *argpos = grub_strchr (s, '='); - - if (argpos) - { - *argpos = '\0'; - return ++argpos; - } - return 0; -} - static struct grub_arg_option * -find_long (const struct grub_arg_option *options, char *s) +find_long (const struct grub_arg_option *options, const char *s, int len) { struct grub_arg_option *found = 0; auto struct grub_arg_option *fnd_long (const struct grub_arg_option *opt); @@ -99,7 +86,7 @@ find_long (const struct grub_arg_option *options, char *s) { while (opt->doc) { - if (opt->longarg && ! grub_strcmp (opt->longarg, s)) + if (opt->longarg && ! grub_strncmp (opt->longarg, s, len)) return (struct grub_arg_option *) opt; opt++; } @@ -244,7 +231,7 @@ grub_arg_parse (grub_extcmd_t cmd, int argc, char **argv, struct grub_arg_list *usr, char ***args, int *argnum) { int curarg; - char *longarg = 0; + int arglen; int complete = 0; char **argl = 0; int num = 0; @@ -328,14 +315,14 @@ grub_arg_parse (grub_extcmd_t cmd, int argc, char **argv, break; } - longarg = (char *) grub_strdup (arg); - if (! longarg) - goto fail; + option = grub_strchr (arg, '='); + if (option) { + arglen = option - arg - 2; + option++; + } else + arglen = grub_strlen (arg) - 2; - option = find_long_option (longarg); - arg = longarg; - - opt = find_long (cmd->options, arg + 2); + opt = find_long (cmd->options, arg + 2, arglen); if (! opt) { grub_error (GRUB_ERR_BAD_ARGUMENT, "Unknown argument `%s'\n", arg); @@ -402,8 +389,6 @@ grub_arg_parse (grub_extcmd_t cmd, int argc, char **argv, if (parse_option (cmd, opt->shortarg, 0, usr) || grub_errno) goto fail; } - grub_free (longarg); - longarg = 0; } complete = 1; @@ -412,7 +397,5 @@ grub_arg_parse (grub_extcmd_t cmd, int argc, char **argv, *argnum = num; fail: - grub_free (longarg); - return complete; }