From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40518) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bwZqc-00047S-Hz for qemu-devel@nongnu.org; Tue, 18 Oct 2016 15:14:51 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bwZqX-0000Ew-I7 for qemu-devel@nongnu.org; Tue, 18 Oct 2016 15:14:50 -0400 Received: from mail-yb0-x241.google.com ([2607:f8b0:4002:c09::241]:34815) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1bwZqX-0000Em-DR for qemu-devel@nongnu.org; Tue, 18 Oct 2016 15:14:45 -0400 Received: by mail-yb0-x241.google.com with SMTP id 191so60898ybv.1 for ; Tue, 18 Oct 2016 12:14:45 -0700 (PDT) References: <20161018145620.20658-1-bobby.prani@gmail.com> <20161018145620.20658-2-bobby.prani@gmail.com> <20161018183441.GB9586@flamenco> From: Pranith Kumar In-reply-to: Date: Tue, 18 Oct 2016 15:14:42 -0400 Message-ID: <87twc9cvvh.fsf@gmail.com> MIME-Version: 1.0 Content-Type: text/plain Subject: Re: [Qemu-devel] [PATCH 2/2] translate-all: Use proper type List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Eric Blake Cc: "Emilio G. Cota" , Paolo Bonzini , Richard Henderson , "open list:Overall" , Peter Crosthwaite Eric Blake writes: > On 10/18/2016 01:34 PM, Emilio G. Cota wrote: >> +++ b/translate-all.c >> @@ -405,23 +405,23 @@ static void page_init(void) >> static PageDesc *page_find_alloc(tb_page_addr_t index, int alloc) >> { >> PageDesc *pd; >> void **lp; >> int i; >> >> /* Level 1. Always allocated. */ >> lp = l1_map + ((index >> V_L1_SHIFT) & (V_L1_SIZE - 1)); >> >> /* Level 2..N-1. */ >> for (i = V_L1_SHIFT / V_L2_BITS - 1; i > 0; i--) { >> - void **p = atomic_rcu_read(lp); >> + void *p = atomic_rcu_read(lp); >> >> if (p == NULL) { >> if (!alloc) { >> return NULL; >> } >> p = g_new0(void *, V_L2_SIZE); >> atomic_rcu_set(lp, p); >> } >> >> lp = p + ((index >> (i * V_L2_BITS)) & (V_L2_SIZE - 1)); > > Pointer addition of 'void *' plus an offset is undefined (gcc, and > presumably clang, have an extension that treats it the same as computing > an offset to a 'char *'; but some compilers choke); this is because > sizeof(void) is unknown, so you don't know what stride to make for each > offset. Or put another way, 'p + offset' is the same as > '&((*p)[offset])', but (*p)[offset] is ill-defined when p is the opaque > type void. > > Pointer addition of 'void **' plus an offset is well-defined, because > sizeof(void*) is well-defined and therefore the stride (4 or 8) makes > sense. Or in array notation, computing '&((*p)[offset]) means we are > skipping to the offset array entry where p is the start of the array of > void* pointers. > Indeed. I missed that crucial detail. I would prefer explicitly casting to 'void **' for p, since that is not the type of what is being returned by atomic_rcu_read(). The joys of void pointer arithmetic, TIL. -- Pranith