From patchwork Sun Sep 5 09:44:01 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Blue Swirl X-Patchwork-Id: 63824 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id C2200B70E2 for ; Sun, 5 Sep 2010 21:30:09 +1000 (EST) Received: from localhost ([127.0.0.1]:49124 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OsDQF-00031i-Gf for incoming@patchwork.ozlabs.org; Sun, 05 Sep 2010 07:30:07 -0400 Received: from [140.186.70.92] (port=48465 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OsD5U-000851-Fo for qemu-devel@nongnu.org; Sun, 05 Sep 2010 07:08:45 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OsBlt-0001me-Ui for qemu-devel@nongnu.org; Sun, 05 Sep 2010 05:44:23 -0400 Received: from mail-qy0-f180.google.com ([209.85.216.180]:33159) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OsBlt-0001mZ-QD for qemu-devel@nongnu.org; Sun, 05 Sep 2010 05:44:21 -0400 Received: by qyk31 with SMTP id 31so3732216qyk.4 for ; Sun, 05 Sep 2010 02:44:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:mime-version:received:in-reply-to :references:from:date:message-id:subject:to:cc:content-type :content-transfer-encoding; bh=kdjNa7rRU5X8fotVxKbj506HZHVou453r/G59sWiQL8=; b=FV3JolNWarvA2o3oZ5t0BIOL6UXN8iC388or8AMF0hMef75voE0e0VaEwD0fkxjK5S 5SwArXbN8d5YsBZY5+MPYyzrKiTKJf1pbIlZIZh0vtr9USPxwPFZfOeVJIL6L6tKRHbf yRDdK2bmGFvJuaJalTx7Hhs5eAYujyYGbILmc= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; b=rm/flKDzCV4f0/oZYcVvI7bWmJF3m57Sr4uVC3vwhSaewj0xUyZuP0Ah5wUfBh3BuL Bt6E33kNQoXED6xBnbYNjSNBEPxF03Lf5EGn7SU41v5CD2UEonyPkp/tDO6bhViAIeWW RmmBld8H0x6rhi/9LM1YZTjAiMqfJMQwg5MHU= Received: by 10.224.54.13 with SMTP id o13mr412900qag.369.1283679861089; Sun, 05 Sep 2010 02:44:21 -0700 (PDT) MIME-Version: 1.0 Received: by 10.229.240.135 with HTTP; Sun, 5 Sep 2010 02:44:01 -0700 (PDT) In-Reply-To: <20100905092636.GA17394@redhat.com> References: <20100905075403.GC16215@redhat.com> <20100905092636.GA17394@redhat.com> From: Blue Swirl Date: Sun, 5 Sep 2010 09:44:01 +0000 Message-ID: To: "Michael S. Tsirkin" X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) Cc: qemu-devel Subject: [Qemu-devel] Re: [PATCH 1/5] Suppress some gcc warnings with -Wtype-limits X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org On Sun, Sep 5, 2010 at 9:26 AM, Michael S. Tsirkin wrote: > On Sun, Sep 05, 2010 at 09:06:10AM +0000, Blue Swirl wrote: >> On Sun, Sep 5, 2010 at 7:54 AM, Michael S. Tsirkin wrote: >> > On Sat, Sep 04, 2010 at 05:21:24PM +0000, Blue Swirl wrote: >> >> In the unsigned number space, the checks can be merged into one, >> >> assuming that BLKDBG_EVEN_MAX is less than INT_MAX. Alternatively we >> >> could have: >> >>  -    if (event < 0 || event >= BLKDBG_EVENT_MAX) { >> >>  +    if ((int)event < 0 || event >= BLKDBG_EVENT_MAX) { >> >> >> >> This would also implement the check that the writer of this code was >> >> trying to make. >> >> The important thing to note is however is that the check as it is now >> >> is not correct. >> > >> > I agree. But it seems to indicate a bigger problem. >> > >> > If we are trying to pass in a negative value, which is not one >> > of enum values, using BlkDebugEvent as type is just confusing, >> > we should just pass int instead. >> >> AFAICT it's only possible to use the values listed in event_names in >> blkdebug.c, other values are rejected. So the check should actually be >> an assert() or it could even be removed. > > Sounds good. > >> >> >> How about adding assert(OMAP_EMIFS_BASE == 0) and commenting out the >> >> >> check? Then if the value changes, the need to add the comparison back >> >> >> will be obvious. >> >> > >> >> > This would work but it's weird.  The thing is it's currently a correct >> >> > code and the check may be useless but it's the optimiser's task to >> >> > remove it, not ours.  The compiler is not able to tell whether the >> >> > check makes sense or nott, because the compiler only has access to >> >> > preprocessed code.  So why should you let the compiler have anything >> >> > to say on it. >> >> >> >> Good point. I'll try to invent something better. >> > >> > Use #pragma to supress the warning? Maybe we could wrap this in a macro .. >> >> Those lines may also desynch silently with changes to OMAP_EMIFS_BASE. >> >> I think the assertion is still the best way, it ensures that something >> will happen if OMAP_EMIFS_BASE changes. We could for example remove >> OMAP_EMIFS_BASE entirely (it's only used for the check), but someone >> adding a new define could still forget to adjust the check anyway. > > We could replace it with a macro > #define OMAP_EMIFS_VALID(addr) ((target_phys_addr_t)addr < OMAP_EMIFF_BASE) > but all this does look artificial. And of course using type casts > is always scary ... > > Would it help to have some inline functions that do the range checking correctly? > We have a couple of range helpers in pci.h, these could be moved out > to range.h and we could add some more. As there act on u64 this will get > the type limits mostly automatically right. That seems to be the best solution, I get no warnings with this: static int omap_validate_imif_addr(struct omap_mpu_state_s *s, I'll add range.h and respin the patches. diff --git a/hw/omap1.c b/hw/omap1.c index b00f870..8bf88e7 100644 --- a/hw/omap1.c +++ b/hw/omap1.c @@ -3672,14 +3672,25 @@ static int omap_validate_emiff_addr(struct omap_mpu_state_s *s, return addr >= OMAP_EMIFF_BASE && addr < OMAP_EMIFF_BASE + s->sdram_size; } +/* Get last byte of a range from offset + length. + * Undefined for ranges that wrap around 0. */ +static inline uint64_t range_get_last(uint64_t offset, uint64_t len) +{ + return offset + len - 1; +} + +/* Check whether a given range covers a given byte. */ +static inline int range_covers_byte(uint64_t offset, uint64_t len, + uint64_t byte) +{ + return offset <= byte && byte <= range_get_last(offset, len); +} + static int omap_validate_emifs_addr(struct omap_mpu_state_s *s, target_phys_addr_t addr) { - /* If OMAP_EMIFS_BASE ever becomes nonzero, adjust the check below - to also include the lower bound check like - addr >= OMAP_EMIFS_BASE && addr < OMAP_EMIFF_BASE */ - assert(OMAP_EMIFS_BASE == 0); - return addr < OMAP_EMIFF_BASE; + return range_covers_byte(OMAP_EMIFS_BASE, + OMAP_EMIFF_BASE - OMAP_EMIFS_BASE, addr); }