Received: by 10.223.176.5 with SMTP id f5csp1168558wra; Wed, 7 Feb 2018 14:01:56 -0800 (PST) X-Google-Smtp-Source: AH8x227IcPgWoF7EJU9xZjpeFUdc7JySI4G4pBQuL1uTLtbLy/6m63h9f13xYhMZ1iojIaWjyXQy X-Received: by 10.99.111.8 with SMTP id k8mr6089627pgc.262.1518040916765; Wed, 07 Feb 2018 14:01:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518040916; cv=none; d=google.com; s=arc-20160816; b=Us+DY2LXfy5n6pziWd73EV3dpS0oao1bG1AHmjhh2JyoVB4DnAr87v6nzz9c53zIjM CVRhMtO/WEOZlTyp5CI6vKYrVNRW6Mlq1T02E+dm8O3q3pGlagyK7hPiqN7GQhrOEu0v +5uoyGt+A8AuB5H/oWTNpeGn0omGnlQJU0stNaBuDMnVIiGEzgfJIza3/MnVh0fI+ftv IGiWshtFdB9zX5oJSfe/za9i/rOW5NWCqtETpuYs/pr89GkqTec6rqHa/nTuI5Iw1Puw dUkjMmQGthr+CpykWBbphwa6Z+0JBso9GzPgqeYwD/uSu27MLYwExGAEqNOnM6QppkVj OG6A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:arc-authentication-results; bh=fwkE0lmAv3GD97+8/Fo4VJKOB6EpqxIbTZk9y3wSslU=; b=iXyc8hILpstb5pu1gebzBOOnYCK4Df+4F3Xct//cjSy1E8wH4lPGuEgUiuOSRUjtDD 16OpMCQPOl+4EDFyc0WIhCOLwtmD7HIb3Fe4kshHttqkchxwJzM41m5v1Tw74f/OdCrJ +fQlKRN9N1wMYS7pMYhu/t6rfo2xXHHzTrV8kkmvdzXux7/AF1mrD7skLzgSq4w3XS1o +TDuzBdiF0tjE4zKoATUzU8xEfinEEqXAuxGHvFUSsRr3mJ5o1Ty+DqguULvwIfiTwGi BVmpBX+2S4jMj7ueGY/IKyQUsOm4LSZb6x/TGqQ2C2Iz2bAIt85mnefKvp2aUFP2VVoz AWlQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b1si1447706pgc.807.2018.02.07.14.01.42; Wed, 07 Feb 2018 14:01:56 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752016AbeBGV7u (ORCPT + 99 others); Wed, 7 Feb 2018 16:59:50 -0500 Received: from nblzone-211-213.nblnetworks.fi ([83.145.211.213]:59104 "EHLO hillosipuli.retiisi.org.uk" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1750736AbeBGV7t (ORCPT ); Wed, 7 Feb 2018 16:59:49 -0500 Received: from valkosipuli.localdomain (valkosipuli.retiisi.org.uk [IPv6:2001:1bc8:1a6:d3d5::80:2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by hillosipuli.retiisi.org.uk (Postfix) with ESMTPS id BC8D6600DC; Wed, 7 Feb 2018 23:59:46 +0200 (EET) Received: from sakke by valkosipuli.localdomain with local (Exim 4.89) (envelope-from ) id 1ejXko-0001yQ-85; Wed, 07 Feb 2018 23:59:46 +0200 Date: Wed, 7 Feb 2018 23:59:46 +0200 From: Sakari Ailus To: "Gustavo A. R. Silva" Cc: Mauro Carvalho Chehab , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" Subject: Re: [PATCH v3 4/8] i2c: ov9650: use 64-bit arithmetic instead of 32-bit Message-ID: <20180207215944.quwowjy52dclk7uc@valkosipuli.retiisi.org.uk> References: <6f6fd607cf3428d6ab115f1deaa82c4963b170f1.1517929336.git.gustavo@embeddedor.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <6f6fd607cf3428d6ab115f1deaa82c4963b170f1.1517929336.git.gustavo@embeddedor.com> User-Agent: NeoMutt/20170113 (1.7.2) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Gustavo, On Tue, Feb 06, 2018 at 10:47:50AM -0600, Gustavo A. R. Silva wrote: > Add suffix ULL to constants 10000 and 1000000 in order to give the > compiler complete information about the proper arithmetic to use. > Notice that these constants are used in contexts that expect > expressions of type u64 (64 bits, unsigned). > > The following expressions: > > (u64)(fi->interval.numerator * 10000) > (u64)(iv->interval.numerator * 10000) > fiv->interval.numerator * 1000000 / fiv->interval.denominator > > are currently being evaluated using 32-bit arithmetic. > > Notice that those casts to u64 for the first two expressions are only > effective after such expressions are evaluated using 32-bit arithmetic, > which leads to potential integer overflows. So based on those casts, it > seems that the original intention of the code is to actually use 64-bit > arithmetic instead of 32-bit. > > Also, notice that once the suffix ULL is added to the constants, the > outer casts to u64 are no longer needed. > > Addresses-Coverity-ID: 1324146 ("Unintentional integer overflow") > Fixes: 84a15ded76ec ("[media] V4L: Add driver for OV9650/52 image sensors") > Fixes: 79211c8ed19c ("remove abs64()") > Signed-off-by: Gustavo A. R. Silva > --- > Changes in v2: > - Update subject and changelog to better reflect the proposed code changes. > - Add suffix ULL to constants instead of casting variables. > - Remove unnecessary casts to u64 as part of the code change. > - Extend the same code change to other similar expressions. > > Changes in v3: > - None. > > drivers/media/i2c/ov9650.c | 9 +++++---- > 1 file changed, 5 insertions(+), 4 deletions(-) > > diff --git a/drivers/media/i2c/ov9650.c b/drivers/media/i2c/ov9650.c > index e519f27..e716e98 100644 > --- a/drivers/media/i2c/ov9650.c > +++ b/drivers/media/i2c/ov9650.c > @@ -1130,7 +1130,7 @@ static int __ov965x_set_frame_interval(struct ov965x *ov965x, > if (fi->interval.denominator == 0) > return -EINVAL; > > - req_int = (u64)(fi->interval.numerator * 10000) / > + req_int = fi->interval.numerator * 10000ULL / > fi->interval.denominator; This has been addressed by your earlier patch "i2c: ov9650: fix potential integer overflow in __ov965x_set_frame_interval" I tweaked a little. It's not in media tree master yet. > > for (i = 0; i < ARRAY_SIZE(ov965x_intervals); i++) { > @@ -1139,7 +1139,7 @@ static int __ov965x_set_frame_interval(struct ov965x *ov965x, > if (mbus_fmt->width != iv->size.width || > mbus_fmt->height != iv->size.height) > continue; > - err = abs((u64)(iv->interval.numerator * 10000) / > + err = abs(iv->interval.numerator * 10000ULL / This and the chunk below won't work on e.g. 32-bit ARM. do_div(), please. > iv->interval.denominator - req_int); > if (err < min_err) { > fiv = iv; > @@ -1148,8 +1148,9 @@ static int __ov965x_set_frame_interval(struct ov965x *ov965x, > } > ov965x->fiv = fiv; > > - v4l2_dbg(1, debug, &ov965x->sd, "Changed frame interval to %u us\n", > - fiv->interval.numerator * 1000000 / fiv->interval.denominator); > + v4l2_dbg(1, debug, &ov965x->sd, "Changed frame interval to %llu us\n", > + fiv->interval.numerator * 1000000ULL / > + fiv->interval.denominator); > > return 0; > } -- Regards, Sakari Ailus e-mail: sakari.ailus@iki.fi