Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4238837pxj; Tue, 25 May 2021 03:33:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzUh1wkGgitmA28Mnu7PWdTWJ++Zg10IgpLoAAn2dTegWCS1DGYLJdwKgNc9E6e/0BtklhV X-Received: by 2002:aa7:d7d5:: with SMTP id e21mr31290789eds.78.1621938799092; Tue, 25 May 2021 03:33:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621938799; cv=none; d=google.com; s=arc-20160816; b=QbWJV/IP3O2KDtPvz+Yhf0xbi9Zn9yU9Mn7v05YoZ1E4eHnFM7XwPbFikEFyiCNFOy HZ9EfSlJWdJ1FcyXITW0XBFcx6M9fONvUkF+LrmCEj9PjB5d4s9+UY3Spzrukkn6+0sL G2jIJBal5oCs3h29CzRiwKy+oJOTfDCeHxlTepraMMilZ+yPigLgLxMP+eo2CeEYmQQZ QbGYTAdGRTtowbWXC0NJnEG2dPvg7QjSZsIhz6Jis8MSpZXaumF46rC4q/RFuxG//OG6 aHjC3UFiF8tYCvWHyZCSNptdtxGrIYA9aet2N/MEjZTA6jMK/QQrETXCf9q3tSDsHnco IReg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dkim-signature; bh=/f4f0aZeBzeXlWyKfJdQ1/8QAd+9GfrpiTcDBRRwltI=; b=IdbyxGXhYBNvC123ucbZq1UFoNuJHLkEHGCEGXGbklM6I5rSMTNxcQGpjfMkMMqyQm 6kHMRnAKIIRK4ZsWAId2HAdJZuKn6H4geMFTqm/NPG0CG6iZgOpnusmfmd4Fp+6UJ3Ws 7Cyj9x9Ug/Fo4x36n397VHlB0Cc2nrtL7sjh2aaWpea5V0GomfYbz7Y4E/hiaVoSAWta xRfHKXSNHVI5yNLYO1CQWE3Bpxaw70BW3ad4nFfZOKweRufb9oiq2zJkjYvcsTSemSRP 4hgQAEidRPLNkFYQJLrMLVfw4iXPXkNkX65y7jVwszP4+wnqSgv2p/R3E2tYhaX4czb3 PCHQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=SU2hSmsR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h8si14770277edz.104.2021.05.25.03.32.56; Tue, 25 May 2021 03:33:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=SU2hSmsR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232784AbhEYJ5J (ORCPT + 99 others); Tue, 25 May 2021 05:57:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41326 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232635AbhEYJ5I (ORCPT ); Tue, 25 May 2021 05:57:08 -0400 Received: from mail-ej1-x631.google.com (mail-ej1-x631.google.com [IPv6:2a00:1450:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8C049C061756 for ; Tue, 25 May 2021 02:55:38 -0700 (PDT) Received: by mail-ej1-x631.google.com with SMTP id lg14so46320555ejb.9 for ; Tue, 25 May 2021 02:55:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=/f4f0aZeBzeXlWyKfJdQ1/8QAd+9GfrpiTcDBRRwltI=; b=SU2hSmsRmylGpFNYI0BcOgB9PyqmDrVC5VDUt2NFrsoKW1c7DhbIq09SdAdQA8gTMc SfzORijh8wbt8IUOyiMDKvEZRRcjm06GhhjV1Mg75BYEQYag3FG71i/iV+NE2aidHqCO XcINDVoKexgJbqhtw8MHwytUJc/g1UhkXV+pA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=/f4f0aZeBzeXlWyKfJdQ1/8QAd+9GfrpiTcDBRRwltI=; b=NRlHNhUEZcy4evAnktbQLVpULWtSTqZO1/tim2uEJCLV1Ldrhehdd5Ulqrn8wbNXL6 cwwAT1BXGd7rI+JXZ1L7acu0emO547F9m3MJnPNYuEs1uJkJyDGZzio7Up3FzzD1frt5 1L/vkGdpcbyJzsPNE2iWX2r857KRi6n8WzAxkstXh3JuhzOK2wjRPNZR0YILD4tDaRaP L958OGq/MDGatJlRAPyzKv3ll92/zQ22B3le8sa/AoDrxeostI35l0yi+O2oO8WVrpwn F1G5deAk1FmdNABi1ugpJG7pOaYhddZSR8wyqfg4fzudtHybft54coxslaOA0nlHMKXq Ezug== X-Gm-Message-State: AOAM531VuOQpvJUouF8YyTqRMjJUWMJuiOd3yM68qtLDMvX7A2HUdNdf CSxWwZKvNlmuaGJTBBA7ESpxVQ== X-Received: by 2002:a17:906:fccc:: with SMTP id qx12mr27810844ejb.21.1621936537051; Tue, 25 May 2021 02:55:37 -0700 (PDT) Received: from [192.168.1.149] ([80.208.74.47]) by smtp.gmail.com with ESMTPSA id g4sm10751126edw.8.2021.05.25.02.55.35 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 25 May 2021 02:55:36 -0700 (PDT) Subject: Re: [PATCH 1/2] lib: test_scanf: Fix incorrect use of type_min() with unsigned types To: Richard Fitzgerald , pmladek@suse.com, rostedt@goodmis.org, sergey.senozhatsky@gmail.com, andriy.shevchenko@linux.intel.com, w@1wt.eu, lkml@sdf.org, davem@davemloft.net, kuba@kernel.org Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, patches@opensource.cirrus.com References: <20210524155941.16376-1-rf@opensource.cirrus.com> <20210524155941.16376-2-rf@opensource.cirrus.com> From: Rasmus Villemoes Message-ID: Date: Tue, 25 May 2021 11:55:34 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 In-Reply-To: <20210524155941.16376-2-rf@opensource.cirrus.com> Content-Type: text/plain; charset=windows-1252 Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 24/05/2021 17.59, Richard Fitzgerald wrote: > sparse was producing warnings of the form: > > sparse: cast truncates bits from constant value (ffff0001 becomes 1) > > The problem was that value_representable_in_type() compared unsigned types > against type_min(). But type_min() is only valid for signed types because > it is calculating the value -type_max() - 1. ... and casts that to (T), so it does produce 0 as it should. E.g. for T==unsigned char, we get #define type_min(T) ((T)((T)-type_max(T)-(T)1)) (T)((T)-255 - (T)1) (T)(-256) which is 0 of type unsigned char. The minimum value of an > unsigned is obviously 0, so only type_max() need be tested. That part is true. But type_min and type_max have been carefully created to produce values of the appropriate type that actually represent the minimum/maximum representable in that type, without invoking UB. If this program doesn't produce the expected results for you, I'd be very interested in knowing your compiler version: #include #define is_signed_type(type) (((type)(-1)) < (type)1) #define __type_half_max(type) ((type)1 << (8*sizeof(type) - 1 - is_signed_type(type))) #define type_max(T) ((T)((__type_half_max(T) - 1) + __type_half_max(T))) #define type_min(T) ((T)((T)-type_max(T)-(T)1)) int main(int argc, char *argv[]) { #define p(T, PT, fmt) do { \ PT vmin = type_min(T); \ PT vmax = type_max(T); \ printf("min(%s) = "fmt", max(%s) = "fmt"\n",#T, vmin, #T, vmax); \ } while (0) p(_Bool, int, "%d"); p(unsigned char, int, "%d"); p(signed char, int, "%d"); p(unsigned int, unsigned int, "%u"); p(unsigned long long, unsigned long long, "%llu"); p(signed long long, signed long long, "%lld"); return 0; } > lib/test_scanf.c | 13 ++++++------- > 1 file changed, 6 insertions(+), 7 deletions(-) > > diff --git a/lib/test_scanf.c b/lib/test_scanf.c > index 8d577aec6c28..48ff5747a4da 100644 > --- a/lib/test_scanf.c > +++ b/lib/test_scanf.c > @@ -187,8 +187,8 @@ static const unsigned long long numbers[] __initconst = { > #define value_representable_in_type(T, val) \ > (is_signed_type(T) \ > ? ((long long)(val) >= type_min(T)) && ((long long)(val) <= type_max(T)) \ > - : ((unsigned long long)(val) >= type_min(T)) && \ > - ((unsigned long long)(val) <= type_max(T))) > + : ((unsigned long long)(val) <= type_max(T))) With or without this, these tests are tautological when T is "long long" or "unsigned long long". I don't know if that is intended. But it won't, say, exclude ~0ULL if that is in the numbers[] array from being treated as fitting in a "long long". Rasmus