From patchwork Tue Feb 6 16:47:50 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Gustavo A. R. Silva" X-Patchwork-Id: 10203597 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 0CD746020F for ; Tue, 6 Feb 2018 17:10:11 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F231F28BE2 for ; Tue, 6 Feb 2018 17:10:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EF70628C54; Tue, 6 Feb 2018 17:10:10 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 86E0C28D33 for ; Tue, 6 Feb 2018 17:10:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752528AbeBFRKJ (ORCPT ); Tue, 6 Feb 2018 12:10:09 -0500 Received: from gateway23.websitewelcome.com ([192.185.50.108]:47265 "EHLO gateway23.websitewelcome.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752286AbeBFRKI (ORCPT ); Tue, 6 Feb 2018 12:10:08 -0500 X-Greylist: delayed 1348 seconds by postgrey-1.27 at vger.kernel.org; Tue, 06 Feb 2018 12:10:08 EST Received: from cm17.websitewelcome.com (cm17.websitewelcome.com [100.42.49.20]) by gateway23.websitewelcome.com (Postfix) with ESMTP id 3FFF61204D for ; Tue, 6 Feb 2018 10:47:52 -0600 (CST) Received: from gator4166.hostgator.com ([108.167.133.22]) by cmsmtp with SMTP id j6PQeWj0kuMnyj6PQeDRYs; Tue, 06 Feb 2018 10:47:52 -0600 Received: from [189.175.4.238] (port=46656 helo=embeddedgus) by gator4166.hostgator.com with esmtpa (Exim 4.89_1) (envelope-from ) id 1ej6PP-000lQS-Lk; Tue, 06 Feb 2018 10:47:51 -0600 Date: Tue, 6 Feb 2018 10:47:50 -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 v3 4/8] i2c: ov9650: use 64-bit arithmetic instead of 32-bit Message-ID: <6f6fd607cf3428d6ab115f1deaa82c4963b170f1.1517929336.git.gustavo@embeddedor.com> References: MIME-Version: 1.0 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.175.4.238 X-Source-L: No X-Exim-ID: 1ej6PP-000lQS-Lk X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: (embeddedgus) [189.175.4.238]:46656 X-Source-Auth: gustavo@embeddedor.com X-Email-Count: 27 X-Source-Cap: Z3V6aWRpbmU7Z3V6aWRpbmU7Z2F0b3I0MTY2Lmhvc3RnYXRvci5jb20= X-Local-Domain: yes Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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; 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; }