Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp2003939rwb; Mon, 7 Nov 2022 08:06:48 -0800 (PST) X-Google-Smtp-Source: AMsMyM65CF2Q4wZ8pELVxSAK1PWGBEQ9ZYORvvDZYZmKBf7KCVIj7nNo+9YD+W2fdraJ30FlGRnt X-Received: by 2002:a50:d602:0:b0:463:a83b:6f89 with SMTP id x2-20020a50d602000000b00463a83b6f89mr36317172edi.366.1667837208297; Mon, 07 Nov 2022 08:06:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667837208; cv=none; d=google.com; s=arc-20160816; b=NfCo6iNTd+ItQ/zXS5qx/esYK3Y0m8EzdGuCMLsL1DXk23IegMZDnsJwgt+gfRFcqF 2nfUDyodgdFlFx0Q1f5+iPyiJJQJEuXMVqbIH1wR8eHCDBRh5EFOBCGSyvXM4yrN2J4m 8EWLoY3XxyRuMTV6qGQdzLHzwmTjTH2se2BdIagRRdhDsKVm+h5fvWb+pM+VwtDPbC8R SsqjV/HkBRvE5OUwwN9mx1CJyx2DaHmAaRe5YcoFu76kr1Gys8TJJ3YSPDu6ThYyeboi SxNVf4JXIkRlu50LRlJf+0424Qa98ZOnOaYg7+E0suP7meP0IWOZhljaOMktJVS9kGUK ernQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=bdBZ2E+tqtFYjBvnHlskTVDvtpeZp9uADuP2UHrCYmA=; b=uy6BJBspQ92AsNNzvL8aD0Y0EkYEwQzM69TW7Dsx3lL8ytmIdCX0+YeO34mc/o7nFP C2jZH28VysSmq9DiDndblkCbHPhhRlnQb6gpZaPmntNpHo9ajRhuCxas02oBkHjliPu9 rt8GxRdRnENmVAGDLL2n4lSPOMxI0kfhH7eeQHUdAmfLNlJc1tHT57KU7WPPc7vHF7BT kFYpjJ50xSzd76AfuKUyFTS7Vd/PGISFBCRZL5fD+KbgQ4yfXB9cvgJjpD0T+tRLv7T8 IFTNzvuvttgoCjx2hLnaGDJG5asR14ZpM4wFJkSqjFA6Nsp1Le5kAutcljxNr7sMSdhZ Q2Gg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@mailo.com header.s=mailo header.b=M9QgzjQV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mailo.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id dr14-20020a170907720e00b007417040d1c5si9650757ejc.823.2022.11.07.08.06.21; Mon, 07 Nov 2022 08:06:48 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=fail header.i=@mailo.com header.s=mailo header.b=M9QgzjQV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mailo.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232817AbiKGPKu (ORCPT + 92 others); Mon, 7 Nov 2022 10:10:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52248 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232795AbiKGPKr (ORCPT ); Mon, 7 Nov 2022 10:10:47 -0500 Received: from msg-2.mailo.com (msg-2.mailo.com [213.182.54.12]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1AEAC1DA6F for ; Mon, 7 Nov 2022 07:10:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=mailo.com; s=mailo; t=1667833832; bh=fWGybKtDfFOGqUPr2kqL0W0MYyr0KbOZJ5Z2jYMekJ4=; h=X-EA-Auth:Date:From:To:Cc:Subject:Message-ID:References: MIME-Version:Content-Type:In-Reply-To; b=M9QgzjQVdQicyx93zppupqrqFPXs5xJoRQ0zP7QEwCbcvGzTvvPO+b25MpxpNcGNw UbOi/a11rLOOgW7jUifRPyOFAxR690VkjIq8z8RB8GF66eTH0OB6e7rTo61kYe5u4z HiRiA6nkAkXAonJnezo1eWRXqqFbYMkErCtbhEOA= Received: by b-5.in.mailobj.net [192.168.90.15] with ESMTP via ip-206.mailobj.net [213.182.55.206] Mon, 7 Nov 2022 16:10:32 +0100 (CET) X-EA-Auth: louSqubUNPz5fcm9qrxopjABB2mp36VSUHdMcMhAB1qPpzUuCC1SPAaRJmtObPWxOX6uc3fSslaudEn05EyD/wfCZZRxKPAM Date: Mon, 7 Nov 2022 20:40:27 +0530 From: Deepak R Varma To: Dan Carpenter Cc: Greg Kroah-Hartman , linux-staging@lists.linux.dev, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] staging: most: video: use min_t() for comparison and assignment Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham 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 Mon, Nov 07, 2022 at 04:20:27PM +0300, Dan Carpenter wrote: > On Mon, Nov 07, 2022 at 09:50:39AM +0530, Deepak R Varma wrote: > > Simplify code by using min_t helper macro for logical evaluation > > and value assignment. Use the _t variant of min macro since the > > variable types are not same. > > This issue is identified by coccicheck using the minmax.cocci file. > > > > Signed-off-by: Deepak R Varma > > --- > > > > Changes in v2: > > 1. Revise patch description. No functional change. > > > > drivers/staging/most/video/video.c | 2 +- > > 1 file changed, 1 insertion(+), 1 deletion(-) > > > > diff --git a/drivers/staging/most/video/video.c b/drivers/staging/most/video/video.c > > index ffa97ef21ea5..d5cc7eea3b52 100644 > > --- a/drivers/staging/most/video/video.c > > +++ b/drivers/staging/most/video/video.c > > @@ -173,7 +173,7 @@ static ssize_t comp_vdev_read(struct file *filp, char __user *buf, > > while (count > 0 && data_ready(mdev)) { > > struct mbo *const mbo = get_top_mbo(mdev); > > int const rem = mbo->processed_length - fh->offs; > > - int const cnt = rem < count ? rem : count; > > + int const cnt = min_t(int, rem, count); > > TL;DR use size_t instead of int. Hi Dan, Thank you for reviewing the patch. Please see my queries inline. > > Using "int" here is wrong. size_t is unsigned long meaning that it has > 64 bits to use to represent positive values. (Let's ignore 32 bit > arches). You have chopped it down to say that it now has 31 bits for > positives and if BIT(31) is set then treat it as negative. Everything > which is larger than INT_MAX will be broken. I did worry about the truncation int might cause to the size_t variable, however, as the result is being assigned to an int, I decided to go for int to be the typecast for min_t. Also, won't size_t will force the int rem to be treated as unsigned value which will impact the comparison when rem indeed is negative. If rem will never be -ve, my worry will be void. Can you please correct if my understanding is wrong? Thanks, ./drv > > Fortunately, in this code the value of count will never go higher than > "INT_MAX - PAGE_SIZE" because Linus understands that it's easy to > introduce bugs like this. > > regards, > dan carpenter > >