Received: from mailman by lists.gnu.org with archive (Exim 4.43) id 1NXRBo-0003uo-K5 for mharc-grub-devel@gnu.org; Tue, 19 Jan 2010 22:25:04 -0500 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NXRBn-0003uA-6j for grub-devel@gnu.org; Tue, 19 Jan 2010 22:25:03 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1NXRBi-0003ro-R0 for grub-devel@gnu.org; Tue, 19 Jan 2010 22:25:02 -0500 Received: from [199.232.76.173] (port=38807 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NXRBi-0003rg-Ml for grub-devel@gnu.org; Tue, 19 Jan 2010 22:24:58 -0500 Received: from hurricane.eginity.com ([207.218.123.225]:56708 helo=zazu.eginity.com) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1NXRBi-0007Gi-DR for grub-devel@gnu.org; Tue, 19 Jan 2010 22:24:58 -0500 Received: from [192.168.1.51] (208.eginity.com [207.218.123.208]) (Authenticated sender: dmerillat@eginity.com) by zazu.eginity.com (Postfix) with ESMTPSA id 666724940 for ; Tue, 19 Jan 2010 22:24:57 -0500 (EST) Message-ID: <4B567789.5000606@dan.merillat.org> Date: Tue, 19 Jan 2010 22:24:57 -0500 From: Dan Merillat User-Agent: Mozilla-Thunderbird 2.0.0.22 (X11/20090707) MIME-Version: 1.0 To: The development of GNU GRUB References: <4B565B20.9050603@dan.merillat.org> <20100120021249.GP5847@riva.ucam.org> In-Reply-To: <20100120021249.GP5847@riva.ucam.org> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 3) Subject: Re: segfault in grub_device_iterate on 64bit platforms X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: The development of GNU GRUB List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 20 Jan 2010 03:25:03 -0000 Colin Watson wrote: >> Seriously though, where did someone see sizeof(p->next) and think it was >> a valid idiom? It allocates a pointer, not a structure. I've never >> seen it used before, and a quick rgrep shows it to be an anomaly in the >> grub source as well. It happens to work on 32bit processors for tiny >> structures because the default alignment for malloc is 8 bytes - and >> struct part_ent is two pointers. You end up overflowing into the >> padding and not trashing anything. > > I think you're a bit overly critical here, although the patch is still > sound. Here's the code from current trunk: I thought the Kayne West reference would have indicated that it was mostly tongue in cheek. The question was more serious than critical though - is there code out there that uses that for linked lists? I've never seen that particular idiom, but it wouldn't be the first time something that just happens to work has been passed on as gospel. > p = grub_malloc (sizeof (p->next) + grub_strlen (disk->name) + 1 + > grub_strlen (partition_name) + 1); > > sizeof (p->next) is perfectly reasonable on the face of it because the > structure being allocated is: > > struct part_ent > { > struct part_ent *next; > char name[0]; > }; Previously, the code looked like this: struct part_ent { struct part_ent *next; char *name; } *ents; ... Two pointers, with ->name referencing the return from asprintf: p = grub_malloc(sizeof(p->next)); ... p->name=grub_asprintf ("%s,%s", disk->name, partition_name); The bug was that you only were allocating the first pointer and not the char *. which happened to work on x86 due to malloc padding everything to 8 bytes. > To allocate a new instance of this structure, it does look at a quick > glance as though we need the size of the next pointer, plus however much > is going to be needed for name; so I can easily understand why it was > written this way and I think you're going a bit overboard in your > criticism. > The reason that it breaks is, of course, because of padding within the > struct needed to ensure alignment. Easily forgotten. Actually, it was a flat-out bug on every processor before, hence my criticism. The current code has a very subtle bug on some unusual systems with alignment larger than the pointer size. - Worse, it'd only show up for certain length strings! > I've committed your patches now. If it looks like: p = grub_malloc (sizeof (*p) + grub_strlen (disk->name) + 1 + grub_strlen (partition_name) + 1); then it should be correct.