Received: from mailman by lists.gnu.org with archive (Exim 4.43) id 1MjJ7L-0003PI-42 for mharc-grub-devel@gnu.org; Thu, 03 Sep 2009 16:41:15 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MjJ7J-0003Mh-8a for grub-devel@gnu.org; Thu, 03 Sep 2009 16:41:13 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MjJ7E-0003HE-J0 for grub-devel@gnu.org; Thu, 03 Sep 2009 16:41:12 -0400 Received: from [199.232.76.173] (port=57354 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MjJ7E-0003Gn-79 for grub-devel@gnu.org; Thu, 03 Sep 2009 16:41:08 -0400 Received: from mail-bw0-f220.google.com ([209.85.218.220]:44272) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1MjJ7D-0005OM-Nm for grub-devel@gnu.org; Thu, 03 Sep 2009 16:41:07 -0400 Received: by bwz20 with SMTP id 20so263925bwz.42 for ; Thu, 03 Sep 2009 13:41:06 -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=L/T7PmlLqooASnn2IUrwT9W4pjIKq1SzUYnKtpo+zGU=; b=J81esy0aBC6TsNFCeh0XnO3uhBUBpdgHdgWi5nF6Y/rFTJPGY1mhV8EvQ7DGel/6Fu 9EjBPQKteAPfWb1W5MLUXwCOyRSRJEfm2HS9YzWeqT5+lSqE2LbE+zrSOsoCoZJPT/QI TYZFe7qSzH7u3IKyY4CargI2bc0DqQO3iFbos= 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=xedzTp8ECLG/D4VsA1cqh/QAD5i46BMkeJguJi6v2uJJ4Lor9ly5NoAngSkrNU7r9d VM9kNIoILio6VZ8ExaRyHGQTfkcvc4oyeEbJztPh4GuP4VvKWuP0gCBx1KbFwgfA4cmp Jo7YxRj3uTBAlMLYSvZlFtFWeRd4MECAhNOtc= MIME-Version: 1.0 Received: by 10.204.160.65 with SMTP id m1mr8311656bkx.193.1252010466180; Thu, 03 Sep 2009 13:41:06 -0700 (PDT) In-Reply-To: References: Date: Thu, 3 Sep 2009 22:41:06 +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: [PATCH] Bug fix for loadenv.c 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: Thu, 03 Sep 2009 20:41:13 -0000 On Thu, Sep 3, 2009 at 9:20 PM, Bean wrote: > Hi, > > This is the bug fix for command/loadenv.c. Here is a small program to > illustrate what happen: Update your branch: 2009-09-01 Vladimir Serbinenko * commands/loadenv.c (check_blocklists): Fix off-by-one error. (write_blocklists): Likewise. > > #include > #include > > struct data > { > =A0struct data *next; > =A0int length; > }; > > struct data * > new_data () > { > =A0struct data *p; > > =A0p =3D malloc (sizeof (*p)); > =A0p->length =3D 1; > =A0p->next =3D 0; > > =A0return p; > } > > int > main () > { > =A0int index; > =A0struct data *head, *p; > > =A0head =3D new_data (); > =A0for (p =3D head, index =3D 0; p; p =3D p->next, index +=3D p->length) > =A0 =A0printf ("%d\n", index); > > =A0return 0; > } > > Compile it with gcc-4.0 from OSX: > i686-apple-dawin8-gcc-4.0.1 -Os test.c > > The assembly dump shows: > > L4: > =A0 =A0 =A0 =A0movl =A0 =A0%edi, 4(%esp) > =A0 =A0 =A0 =A0movl =A0 =A0-28(%ebp), %eax > =A0 =A0 =A0 =A0movl =A0 =A0%eax, (%esp) > =A0 =A0 =A0 =A0call =A0 =A0L_printf$stub > =A0 =A0 =A0 =A0movl =A0 =A0(%esi), %esi > =A0 =A0 =A0 =A0addl =A0 =A04(%esi), %edi > =A0 =A0 =A0 =A0jmp =A0 =A0 L4 > > Why would it uses unconditional jumps ? I think it's some over > optimization in gcc. We have index +=3D p->length after p =3D p->next, if > p is null, p->length would be invalid, so gcc figure that p would > never be null and the end condition is always false. In fact, the bug > is in the code itself, even if it tests p, in OS environment, access > null pointer is not allowed, so index +=3D p->length can't be executed > anyway. > > The correct way is: > > =A0for (p =3D head, index =3D 0; p;) > =A0{ > =A0 =A0printf ("%d\n", index); > =A0 p =3D p->next; > =A0if (p) > =A0 index +=3D p->length; > =A0} > > > -- > Bean > > gitgrub home: http://github.com/grub/grub/ > my fork page: http://github.com/bean123/grub/ > > _______________________________________________ > Grub-devel mailing list > Grub-devel@gnu.org > http://lists.gnu.org/mailman/listinfo/grub-devel > > --=20 Regards Vladimir 'phcoder' Serbinenko Personal git repository: http://repo.or.cz/w/grub2/phcoder.git