iio: addac: ad74413r: fix integer promotion bug in ad74413_get_input_current_offset()
Message ID | 20221118123209.1658420-1-linux@rasmusvillemoes.dk |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp169460wrr; Fri, 18 Nov 2022 04:35:26 -0800 (PST) X-Google-Smtp-Source: AA0mqf7z/Ws4S7ePLh7qyR+2JPwJhWZkId3OV1kdx3h3g3pYYLbFvxhgHTjO7li7vTumoEf5G7Wb X-Received: by 2002:a17:903:2787:b0:180:9675:3ec8 with SMTP id jw7-20020a170903278700b0018096753ec8mr7436688plb.108.1668774925965; Fri, 18 Nov 2022 04:35:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668774925; cv=none; d=google.com; s=arc-20160816; b=R/d0/IidKhl/BJ2EiGb9Eq1nmMDdV13LdDWWy+86v3BHST0DS3nBxggHjFo1e5P5rv JtjnM0trwRJZiPuRqaUuM0UGRav/t+6G9GfPTovZZwjwhvJL6C0j9Q+Hqbd+1AklaT6z jhno+VzRV5dYyiGeef/zwSe47ojFCysYD40dS53eNN71OHGuYkdmD7PaHpKY/hixrb4m 3UDxPVcbcJBREBb9UIb53Hff4GdJcXY2I8PCesvaPyiY30CeIZpI77zn+npukdCCrYw7 Hqc+NhQMNv2Hmaud6ufIvz1gstPsjUK8hRubW8QzM+esMKWCG2EElWTfYIsmfSrhKCFz eC2A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=8HEJJXAKUVernzz9Wo79oEymNliom5Q3VVumNx+M1W8=; b=oy1sN0/rPQlB11u4GbDepJHm7oMZR6UyQE6JM9/ueZhz1IyLuBy00PydzTj8uT1yEa QwVVVokGJy+mgS+lVKZv8EnC02OC3f7eiMK/uDVcw6j8oTEp81Ed23FGnp2nNIzToBv8 0ikWkuvP2OBMenfZUtVn9R/xqvwc3iRPAw1MRMcL0OG/Wd/lYIchWML7nkirjEPOO97S miLsmUuJNvZGNomPgf5GG8Mjmoy/kYRmkiGN06UKIzD4KBnh2XNulkMdxQynd4nTbdSN hMbMhg0AtM1wNoueBpRzClLraIEhAsStS7KE6daakbadBdPWEirRLRa5veFEZUIgI8hq FQYg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=ZHAsgUaI; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id jg21-20020a17090326d500b0018905bd4a58si606060plb.169.2022.11.18.04.35.12; Fri, 18 Nov 2022 04:35:25 -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=pass header.i=@rasmusvillemoes.dk header.s=google header.b=ZHAsgUaI; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235185AbiKRMdK (ORCPT <rfc822;kkmonlee@gmail.com> + 99 others); Fri, 18 Nov 2022 07:33:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44378 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241920AbiKRMcx (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Fri, 18 Nov 2022 07:32:53 -0500 Received: from mail-lj1-x235.google.com (mail-lj1-x235.google.com [IPv6:2a00:1450:4864:20::235]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 092596151C for <linux-kernel@vger.kernel.org>; Fri, 18 Nov 2022 04:32:40 -0800 (PST) Received: by mail-lj1-x235.google.com with SMTP id d3so6632298ljl.1 for <linux-kernel@vger.kernel.org>; Fri, 18 Nov 2022 04:32:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=8HEJJXAKUVernzz9Wo79oEymNliom5Q3VVumNx+M1W8=; b=ZHAsgUaIWQPKb7iP/4IhX7MSK56qD1Nu3eXRxrAMUGG2t6iZTKCtKcePZEEZ74Ix9P cj8eDHhW7tDMWCJozEgA2J9epPPJ/7RZ+6edwoM0qIWSXQkvMSaQrMTIrNM9pvrBlArr 2H+Sns5tB+WlXbcHzNE9V5XCKY1D4BeOUREWs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=8HEJJXAKUVernzz9Wo79oEymNliom5Q3VVumNx+M1W8=; b=7mXNJdXhNUr55BRPE77pXZcZhHMGzCB5gce1AGGzpfcBHO7Z8WGfM0VwYMlJx8dCK7 lRYbdwo/rlp/hLvjO/PfTX5dDn30FWhHE9rLzVSussYX7uliz5yZc72GGelgmgsgxFgU tYq3OsEZJ9fzpm7xQX6pPTBaOw/z/O4/USIV+FQNMEnpbEDTl8VD65xCpcd9oSGPgKFX NOcFEx4M6At7QcqOD9Q1TQfjpyo2/UZItbsfB/wCoy9Wq8T+q5P1MgoslAZGXs89jIpo 094F9WpsUwYd4pjtZygtkBcfXTwrMCr25M0fiJGYcbvT4jaqss+sEy4ocRoj0To7nZfz 9hLw== X-Gm-Message-State: ANoB5pn413OBfGtxZWBPvEt8eduPTqFL+2dxC3FY6TPHrP92vOROhHxG D9egfTvwSsxremRN32NFCXqu09kspY6ElSW5 X-Received: by 2002:a05:651c:12c1:b0:277:2fd5:482 with SMTP id 1-20020a05651c12c100b002772fd50482mr2619130lje.194.1668774758200; Fri, 18 Nov 2022 04:32:38 -0800 (PST) Received: from prevas-ravi.prevas.se ([81.216.59.226]) by smtp.gmail.com with ESMTPSA id u3-20020a05651220c300b00496d3e6b131sm639484lfr.234.2022.11.18.04.32.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Nov 2022 04:32:37 -0800 (PST) From: Rasmus Villemoes <linux@rasmusvillemoes.dk> To: Cosmin Tanislav <cosmin.tanislav@analog.com>, Lars-Peter Clausen <lars@metafoo.de>, Michael Hennerich <Michael.Hennerich@analog.com>, Jonathan Cameron <jic23@kernel.org> Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] iio: addac: ad74413r: fix integer promotion bug in ad74413_get_input_current_offset() Date: Fri, 18 Nov 2022 13:32:08 +0100 Message-Id: <20221118123209.1658420-1-linux@rasmusvillemoes.dk> X-Mailer: git-send-email 2.37.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=unavailable 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749837336832748492?= X-GMAIL-MSGID: =?utf-8?q?1749837336832748492?= |
Series |
iio: addac: ad74413r: fix integer promotion bug in ad74413_get_input_current_offset()
|
|
Commit Message
Rasmus Villemoes
Nov. 18, 2022, 12:32 p.m. UTC
The constant AD74413R_ADC_RESULT_MAX is defined via GENMASK, so its
type is "unsigned long".
Hence in the expression voltage_offset * AD74413R_ADC_RESULT_MAX,
voltage_offset is first promoted to unsigned long, and since it may be
negative, that results in a garbage value. For example, when range is
AD74413R_ADC_RANGE_5V_BI_DIR, voltage_offset is -2500 and
voltage_range is 5000, so the RHS of this assignment is, depending on
sizeof(long), either 826225UL or 3689348814709142UL, which after
truncation to int then results in either 826225 or 1972216214 being
the output from in_currentX_offset.
Casting to int avoids that promotion and results in the correct -32767
output.
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
drivers/iio/addac/ad74413r.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Comments
> From: Rasmus Villemoes <linux@rasmusvillemoes.dk> > Sent: Friday, November 18, 2022 1:32 PM > To: Tanislav, Cosmin <Cosmin.Tanislav@analog.com>; Lars-Peter Clausen > <lars@metafoo.de>; Hennerich, Michael <Michael.Hennerich@analog.com>; > Jonathan Cameron <jic23@kernel.org> > Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>; linux- > iio@vger.kernel.org; linux-kernel@vger.kernel.org > Subject: [PATCH] iio: addac: ad74413r: fix integer promotion bug in > ad74413_get_input_current_offset() > > [External] > > The constant AD74413R_ADC_RESULT_MAX is defined via GENMASK, so its > type is "unsigned long". > > Hence in the expression voltage_offset * AD74413R_ADC_RESULT_MAX, > voltage_offset is first promoted to unsigned long, and since it may be > negative, that results in a garbage value. For example, when range is > AD74413R_ADC_RANGE_5V_BI_DIR, voltage_offset is -2500 and > voltage_range is 5000, so the RHS of this assignment is, depending on > sizeof(long), either 826225UL or 3689348814709142UL, which after > truncation to int then results in either 826225 or 1972216214 being > the output from in_currentX_offset. > > Casting to int avoids that promotion and results in the correct -32767 > output. > > Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk> > --- After adding proper Fixes: tag, Reviewed-by: Nuno Sá <nuno.sa@analog.com>
On 18/11/2022 14.17, Sa, Nuno wrote: >> Casting to int avoids that promotion and results in the correct -32767 >> output. >> >> Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk> >> --- > > After adding proper Fixes: tag, > > Reviewed-by: Nuno Sá <nuno.sa@analog.com> That would be Fixes: fea251b6a5db (iio: addac: add AD74413R driver) Thanks, Rasmus
On Fri, 18 Nov 2022 14:29:23 +0100 Rasmus Villemoes <linux@rasmusvillemoes.dk> wrote: > On 18/11/2022 14.17, Sa, Nuno wrote: > > >> Casting to int avoids that promotion and results in the correct -32767 > >> output. > >> > >> Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk> > >> --- > > > > After adding proper Fixes: tag, > > > > Reviewed-by: Nuno Sá <nuno.sa@analog.com> > > That would be > > Fixes: fea251b6a5db (iio: addac: add AD74413R driver) > Applied to the togreg branch of iio.git (as very late in cycle) and marked for stable. Thanks, Jonathan > Thanks, > Rasmus >
diff --git a/drivers/iio/addac/ad74413r.c b/drivers/iio/addac/ad74413r.c index 899bcd83f40b..e0e130ba9d3e 100644 --- a/drivers/iio/addac/ad74413r.c +++ b/drivers/iio/addac/ad74413r.c @@ -691,7 +691,7 @@ static int ad74413_get_input_current_offset(struct ad74413r_state *st, if (ret) return ret; - *val = voltage_offset * AD74413R_ADC_RESULT_MAX / voltage_range; + *val = voltage_offset * (int)AD74413R_ADC_RESULT_MAX / voltage_range; return IIO_VAL_INT; }