Received: by 10.223.176.5 with SMTP id f5csp2930961wra; Mon, 5 Feb 2018 12:31:24 -0800 (PST) X-Google-Smtp-Source: AH8x226GMzKbGZss/hBQ1K5O+iGlfrfbcFBQy68WQjdLU2QqnqitHA1SbZN6nfwgQ18Fk+hhzr24 X-Received: by 2002:a17:902:242:: with SMTP id 60-v6mr49853plc.376.1517862684498; Mon, 05 Feb 2018 12:31:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517862684; cv=none; d=google.com; s=arc-20160816; b=TmGht7P2s6xxVMExVs7IczOX5axiYCuFfCHBSXvEXm3OlMGQoBsW+50FQ3GauR6VdR 7yqAFLA27pchAZ5oG2Pp6LreEo032BxBLZ7QZCAfTBHhNP6mt+BR6K8nkpDeiBQW25C3 8o6FqBj+zeUxJdx+zt480Lqs4CAiu2kV5VCtMahgxf1XBI8QMZHIGbfec2+IIxEGP5Qb YACGhsRpoEiZUA1cjFgNnVT1cb/rPiWvVHn3G9WSs1RUwejlKBfsMPKFS0lEsVyvqrtK 91x8Mk4heX5FUKxvysn9UNcLWIwpBHtTz27hKRbR+XdLQauo4w7Pd7/CSlYYg6FTNR1c AmlA== 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=mmfGAO4Td/wM39C8RdlTrVHrm8ovTFSbPmAjDhJ1QnM=; b=qpQ2A3cvDnYQZCXlmkFmRrAwjSf3kRkKBzYBJzTUsg3U8AgHus2gu+mrC3pT2eeoRp IADrzhZjRBbbUWPH8qwtAVlfW4/cZF16N4KTxJD7GTneqMvkcOceL/eXVVoAPCXOk4sQ fVj5asWgtYb30U+jdEi8i43qaXn51WhZkcGPbCTdlkI2iMvKjAs30yQMZRSSg7zjwBtN j/Rr0QMmS3hQGJnlOT5DftfQc57lPxTQGVJgji8ADHtfG978mbIY/ttOR2ApMWdbLEn8 sJ7pZezJsbfJMIBRPpmvdtTvCp42px//NOr3nwOl7l7BptGJPuYfyEPoa/UZdS3FpmtO tFqA== 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 c41-v6si7374608plj.682.2018.02.05.12.31.10; Mon, 05 Feb 2018 12:31:24 -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 S1751970AbeBEUad (ORCPT + 99 others); Mon, 5 Feb 2018 15:30:33 -0500 Received: from gateway23.websitewelcome.com ([192.185.48.104]:26706 "EHLO gateway23.websitewelcome.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751655AbeBEUaL (ORCPT ); Mon, 5 Feb 2018 15:30:11 -0500 Received: from cm14.websitewelcome.com (cm14.websitewelcome.com [100.42.49.7]) by gateway23.websitewelcome.com (Postfix) with ESMTP id 584E8211F3 for ; Mon, 5 Feb 2018 14:08:27 -0600 (CST) Received: from gator4166.hostgator.com ([108.167.133.22]) by cmsmtp with SMTP id in3zexL890aRHin3zeHwdb; Mon, 05 Feb 2018 14:08:27 -0600 Received: from [189.152.201.65] (port=46630 helo=embeddedgus) by gator4166.hostgator.com with esmtpa (Exim 4.89_1) (envelope-from ) id 1ein3y-003ImH-VK; Mon, 05 Feb 2018 14:08:27 -0600 Date: Mon, 5 Feb 2018 14:08:26 -0600 From: "Gustavo A. R. Silva" To: Mauro Carvalho Chehab Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" Subject: [PATCH v2 4/8] i2c: ov9650: use 64-bit arithmetic instead of 32-bit Message-ID: <6f6fd607cf3428d6ab115f1deaa82c4963b170f1.1517856716.git.gustavo@embeddedor.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator4166.hostgator.com X-AntiAbuse: Original Domain - vger.kernel.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - embeddedor.com X-BWhitelist: no X-Source-IP: 189.152.201.65 X-Source-L: No X-Exim-ID: 1ein3y-003ImH-VK X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: (embeddedgus) [189.152.201.65]:46630 X-Source-Auth: gustavo@embeddedor.com X-Email-Count: 24 X-Source-Cap: Z3V6aWRpbmU7Z3V6aWRpbmU7Z2F0b3I0MTY2Lmhvc3RnYXRvci5jb20= X-Local-Domain: yes Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. 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; 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 / 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; } -- 2.7.4