Received: from mailman by lists.gnu.org with archive (Exim 4.43) id 1MWnyf-0003Dw-Ur for mharc-grub-devel@gnu.org; Fri, 31 Jul 2009 05:00:37 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MWnyd-0003De-DK for grub-devel@gnu.org; Fri, 31 Jul 2009 05:00:35 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MWnyX-0003DK-Oe for grub-devel@gnu.org; Fri, 31 Jul 2009 05:00:34 -0400 Received: from [199.232.76.173] (port=32886 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MWnyX-0003DH-Ko for grub-devel@gnu.org; Fri, 31 Jul 2009 05:00:29 -0400 Received: from mail-bw0-f222.google.com ([209.85.218.222]:40286) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1MWnyX-0000RT-7W for grub-devel@gnu.org; Fri, 31 Jul 2009 05:00:29 -0400 Received: by bwz22 with SMTP id 22so1363687bwz.42 for ; Fri, 31 Jul 2009 02:00:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:content-type :content-transfer-encoding; bh=r4KWWRMV3K6z9U2R342toNQCcFSYb9Ep6UWU/aGDr1o=; b=OrPl/OBxZT9/IY0RktRS53bHO9DIB6vQL5o05j++d0vdyDzibNQ4r1SOTXhoFVg4TQ Nma+zOepz7py8jgRYeMP6pzgAb5QpgpvMs8ot8Oap7/cD2VZQHyBhAsL2kjKbpkY5P9t pD6r0AeCcWkCbyrbdVH6fKfPLu5Cq2qb7n/pA= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=tLUBHpfFs2xYzbSEhrUv3zAGeKe5ajvqboeJd3e/gt3h4KubiAakZSrEhHtYbbNnT4 Yz6Tz03keIDFAhEj9RXQXCgRXjIGPbnn7ZcM8KAIseAQeX3ighAVjX/1+8I3GofCfE+j 8sfCdZzOkTW7H3kUtCyJ9GliOdYACZXLLPsrE= MIME-Version: 1.0 Received: by 10.204.103.12 with SMTP id i12mr2469794bko.192.1249030826407; Fri, 31 Jul 2009 02:00:26 -0700 (PDT) In-Reply-To: <1249013823.6669.20.camel@ct> References: <1249013823.6669.20.camel@ct> Date: Fri, 31 Jul 2009 11:00:26 +0200 Message-ID: From: "Vladimir 'phcoder' Serbinenko" To: The development of GRUB 2 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 2) Subject: Re: [BUGFIX] Incorrect count of argument with rescue parser 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: Fri, 31 Jul 2009 09:00:35 -0000 On Fri, Jul 31, 2009 at 6:17 AM, Pavel Roskin wrote: > On Fri, 2009-07-31 at 00:46 +0200, Vladimir 'phcoder' Serbinenko wrote: >> This patch fixes the parsing of two strings like following ones: >> "echo 1 " was parsed into "echo", "1", "" >> "echo $root" was parsed into "echo" (variable just disappeared) > > It would be helpful if you explain how to see the difference without > tracing grub_parser_split_cmdline() in the debugger. cat /hello error: file name required echo $root Empty line > > Also, it would be great if you explain the change. =A0A comment for the > newly added code would help understand it. =A0Otherwise it looks like the > previous comment still applies ("A special case for when the last > character was part of a variable"). > Sorry I forgot to move the comment together with code > Since you looked at the problem, perhaps you know why argc is > decremented before the exit. =A0I think it needs a comment. It's because till that point argc was count of tokens and afterwards it's a count of argument. Example "echo hello" gives 2 tokes "echo" and "hello" but command "echo" recieves only one argument: "hello". But probably this should be moved to ./kern/rescue_parser.c, ./script/lua/grub_lib.c and ./normal/completion.c (3 callers of this function) > > Also, grub_malloc() appears to allocate two extra pointers for argv (if > we consider that argc is decremented). =A0argv is not supposed to be null > terminated. =A0I'd rather allocate just enough memory so that we could > catch abusers by running grub-emu in valgrind. > > Anyway, the patch doesn't pass even minimal testing. =A0Pressing Tab in > grub-emu crashes it at normal/completion.c:424 > Ok. I looked into it. I triggered another bug. When someone parses any empty string he gets 0 tokens which means -1 arguments. Here is a fix: diff --git a/kern/rescue_parser.c b/kern/rescue_parser.c index 79f32b8..a93ca36 100644 --- a/kern/rescue_parser.c +++ b/kern/rescue_parser.c @@ -35,6 +35,9 @@ grub_rescue_parse_line (char *line, grub_reader_getline_t getline) if (grub_parser_split_cmdline (line, getline, &n, &args) || n < 0) return grub_errno; + if (n =3D=3D -1) + return GRUB_ERR_NONE; + /* In case of an assignment set the environment accordingly instead of calling a function. */ if (n =3D=3D 0 && grub_strchr (line, '=3D')) diff --git a/normal/completion.c b/normal/completion.c index 405976a..5c2e0d1 100644 --- a/normal/completion.c +++ b/normal/completion.c @@ -399,14 +399,18 @@ grub_normal_do_completion (char *buf, int *restore, if (grub_parser_split_cmdline (buf, 0, &argc, &argv)) return 0; - - current_word =3D argv[argc]; + + if (argc =3D=3D -1) + current_word =3D ""; + else + current_word =3D argv[argc]; + /* Determine the state the command line is in, depending on the state, it can be determined how to complete. */ cmdline_state =3D get_state (buf); - if (argc =3D=3D 0) + if (argc =3D=3D 0 || argc =3D=3D -1) { /* Complete a command. */ if (grub_command_iterate (iterate_command)) @@ -476,13 +480,15 @@ grub_normal_do_completion (char *buf, int *restore, goto fail; } - grub_free (argv[0]); + if (argc !=3D -1) + grub_free (argv[0]); grub_free (match); return ret; } fail: - grub_free (argv[0]); + if (argc !=3D -1) + grub_free (argv[0]); grub_free (match); grub_errno =3D GRUB_ERR_NONE; --=20 Regards Vladimir 'phcoder' Serbinenko Personal git repository: http://repo.or.cz/w/grub2/phcoder.git