Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp3561981pxp; Tue, 8 Mar 2022 17:31:00 -0800 (PST) X-Google-Smtp-Source: ABdhPJzoNtVCZm+z8SD8PyU1Mn5tRX5YA7eo1HGva0rJ7HOSrhPMtw9804DEuBwe/qH4g4yPy057 X-Received: by 2002:a62:8453:0:b0:4f6:b00c:9388 with SMTP id k80-20020a628453000000b004f6b00c9388mr21455313pfd.84.1646789460067; Tue, 08 Mar 2022 17:31:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646789460; cv=none; d=google.com; s=arc-20160816; b=V/UFKxdMYszFy9FVoCPGj6hJW9VMTbECfB2/eXrcPMtM+oFwU6hclakX0L/+dGdLDg LBicjzjDfx6DqTemjK6n3oQfnnGFmOWs/Yrn/h++tE+GByNf0+BNdcBAnezIDMVtn1t4 vwFQk8sX9j3hxoP7xP55FjKUQYJ1zBs/OmT+29t8xoYWb7HD38cgmhlG0RBdt/ei97ig DYc0hMqpL1r42OKqqPOk3QbOMqHNWpVNOyGacTh5mjpn7Rt+StJWvaQC7jqeyWEr+T8E LVWI5AVd0cnSgLvjq192gAibnpKhBj28nBQG76bHKpayviKx26COH14Wacy7yWwVuZ8n 5Ysg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version; bh=NRiW3RZ2AP1B2mUP/uqprs0Cc9n2XE8CT7hah6binBY=; b=KBWqQ7mYE38RESsPG9Otq0rZHH/ua1A6/S0+o7iyMtJglaeRMjRT1eH4KAM+nnetc2 9Xh0iWe8EFTtlolYoQw8SS/ZNzLrYX8r1eAKnb9vtpdsaimTBplaWn77Poh3KtSB4zls aLKInXRP5Ao7Ia3px7Odf+6hLmNBbCfVOGKLjHA96LdGeYBkwT84La5x5JTqJUfEJbZ8 bH3H1j1IaVwrwve8j1cIe2/9rBc3OmOywVmiF9htGhYDypAYFdkuPqnnkEDBxu7nUSIo cA+nB4ybcw8m4stQ8tWidAeoQxRjRsEpRgVS/TXxKOjQKVdMsOdRam3tkfpNaZvY/MF5 bPbg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id y12-20020a056a00180c00b004f7265e58aasi517086pfa.205.2022.03.08.17.30.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Mar 2022 17:31:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 971A41B0BFF; Tue, 8 Mar 2022 16:19:01 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346667AbiCHMVm convert rfc822-to-8bit (ORCPT + 99 others); Tue, 8 Mar 2022 07:21:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42572 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230345AbiCHMVk (ORCPT ); Tue, 8 Mar 2022 07:21:40 -0500 Received: from mail-yb1-f180.google.com (mail-yb1-f180.google.com [209.85.219.180]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D403D45508 for ; Tue, 8 Mar 2022 04:20:43 -0800 (PST) Received: by mail-yb1-f180.google.com with SMTP id u10so17542314ybd.9 for ; Tue, 08 Mar 2022 04:20:43 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=8JVjoa74iunmuwBgS5aLlTqz0b8LZXNDf+BjGGWzCus=; b=pMje7NmLIME+STwntbPQ4DzVjBZ1upS/ggOZoZMlc/Jzjb8K0+1iYxrLFxP3zRe2LJ 0rSk07dLYQ3TH6hqfkrjNTYUPF2fXCDLsH1i/yJLUjW8rCG2y+C+60Sz03dTcDYcRL3w axJC7kK7aATDt4NhpP487KLRBW8JU2KA3+94vty1hfzip+VT2mL3L3whrQC6miRgIUou UK9iTc6FrsGXNf+67z70N93i10Kw/SmHjS/IWtP15MU6vwjyS66XIFPWUWZAb/wuM7p/ +fwf/E7oaJY6FLnTp2kwAPMxgJ1p5HODAUA2DkSSRO6XWZThS6VdbRX0znslxE/9rdUU neug== X-Gm-Message-State: AOAM531BcF7bGOgjb5T7gHU+Fe7QUyfJyF5TZr14f0bqHzYpan538nDj ys+9LxO+oxDr8/2SpmRV+/hugFRdtXajKRA0UQl2aDFL6v0u9Q== X-Received: by 2002:a25:d2d0:0:b0:628:7d69:b598 with SMTP id j199-20020a25d2d0000000b006287d69b598mr12208517ybg.381.1646742042945; Tue, 08 Mar 2022 04:20:42 -0800 (PST) MIME-Version: 1.0 References: <20220304124416.1181029-1-mailhol.vincent@wanadoo.fr> <20220307105810.1747024-1-alexandr.lobakin@intel.com> <20220307150750.1762040-1-alexandr.lobakin@intel.com> In-Reply-To: From: Vincent MAILHOL Date: Tue, 8 Mar 2022 21:20:32 +0900 Message-ID: Subject: Re: [PATCH] linux/bits.h: fix -Wtype-limits warnings in GENMASK_INPUT_CHECK() To: Andy Shevchenko Cc: Alexander Lobakin , Arnd Bergmann , Rikard Falkeborn , Andrew Morton , Linux Kernel Mailing List , Kees Cook Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8BIT X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue. 8 Mar 2022 à 01:30, Andy Shevchenko wrote: > On Mon, Mar 7, 2022 at 5:10 PM Alexander Lobakin > wrote: > > From: Vincent MAILHOL > > Date: Mon, 7 Mar 2022 22:50:56 +0900 > > ... > > > For example, people tend to make the following mistake: > > > > unsigned int i; > > > > for (i = 0; i ...) { > > ret = setup_something(array[i]); > > if (ret) > > goto unroll; > > } > > > > unroll: > > while (--i) > > unroll_something(array[i]); > > > > The loop will never end as `i` was declared as unsigned. > > -Wtype-limits catches this. > > This looks like a wrapping value issue, not sure if the type limits > makes logical sense. What I'm saying is that the waning is > controversial. It may help or it may make noise. > > > Not speaking of checking unsigned variables on < 0: > > > > unsigned int num; > > > > /* calculate_something() returns the number of something > > * or -ERRNO in case of an error > > */ > > num = calculate_something(); > > if (num < 0) > > ... > > Depends on the context. Here is a mistake, but there are plenty of > cases when it's okay to do so. I am curious to see which case you are thinking of. Personally, I see two cases, both with macro: 1/ Cases similar to GENMASK_INPUT_CHECK() in which the macro is made for generic purpose and in which there was no way to know in advance that one parameter would be unsigned and the other zero. 2/ Comparaison again a macro which might or might not be zero. e.g.: #ifdef FOO #define BAR 42 #else #define BAR 0 #endif void baz(void) { unsigned int i; if (i > BAR) /* yields -Wtype-limits if FOO is not defined. */ } And because of those two false positives, moving it to W=2 was a wise choice. But I am not aware of any use cases outside of macro where doing an: unsigned int num; /* ... */ if (num < 0) would be okay. At best it is dead code, at worse, it is a bug as pointed out by Alexander. I am not sure what I am missing here. > And in the above the variable name is > misleading with its semantics, The proper code should be > > unsigned int num; > int ret; > > ret = ... > if (ret < 0) > ... > num = ret; > > Again, the warning is controversial in my opinion. > > -- > With Best Regards, > Andy Shevchenko