Received: by 2002:a25:824b:0:0:0:0:0 with SMTP id d11csp4424816ybn; Fri, 27 Sep 2019 23:58:08 -0700 (PDT) X-Google-Smtp-Source: APXvYqw27/Uq4Qdrb7OVD/wOGwSkkEZoWV3W88bTV3JFJM5Jp9QTipyr2C9rlVWZgB0vVsbmvKP8 X-Received: by 2002:a17:906:f204:: with SMTP id gt4mr10860636ejb.299.1569653888153; Fri, 27 Sep 2019 23:58:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1569653888; cv=none; d=google.com; s=arc-20160816; b=malpUXslqQ8OdvaG5mFmYvLTiXx4w+Tg/P7NbvNbWNikL4lMT8NFwvlHEQaZCMhCta a/xMWuDj2oeeiITLkwvPN3rXDmuHL44tJ9VNGLyUqK2F2kvmD2PGbBzinFiGH60sVZ8w pOUjoBWUXKnJl145R/sxKzLpiK8jaleLXUxGtK4wk+8SZoMoVJun8rIr1oBT7/QGUBCv 7GKW1p1cMZ2Jl6IQcKaOFLzPmhh5RqajNy40thicqbjeV6RlU1nT7rcG3cMeKEHV7OXb q3wKIvUQ7XGhxLMPuenwylnLjx3ZcVsBFMfihrLjT+trjn6UeI53KoXS/xeRVVt2YgqQ qC6w== 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; bh=Jk43IaiJVS/OeXrYaQdUfOaehG8kWVefX6BnZIF41BU=; b=eA5CtJ8DmK92o2qm0iUa8L0HZcd1Z9mjNgs/Z6WxFlcKVXltT+l7OUT7UqazSsH1Z3 Qhi7NhgBhIfsQh9RaVQCwriWtJEib6974/mysSR3VePwZd7qXmIhO0rKMgLWOmLxiTR9 U5Kk9zyISKK1ZZP3z5BIVI6bZmQftGNWHzkAhPjpg812LYAArmEZBFWv+es5ZfT7G/7d 3iCrgrSqtGbZQXKlcaQdTItYTizL2PiIGoPAeA+wn+u6lvCM482mzk8wctE4ldmtz4Gl Lp6IC/C974z4ncIv2ZXLHfRSmYQNYrz45/eEoIb+LZKMxD0JOct/1tk1BvnXUTTVhfoF IN7g== 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 c9si3166041eda.229.2019.09.27.23.57.13; Fri, 27 Sep 2019 23:58:08 -0700 (PDT) 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 S1725897AbfI1G5I (ORCPT + 99 others); Sat, 28 Sep 2019 02:57:08 -0400 Received: from 178.115.242.59.static.drei.at ([178.115.242.59]:33763 "EHLO mail.osadl.at" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725283AbfI1G5I (ORCPT ); Sat, 28 Sep 2019 02:57:08 -0400 X-Greylist: delayed 583 seconds by postgrey-1.27 at vger.kernel.org; Sat, 28 Sep 2019 02:57:07 EDT Received: by mail.osadl.at (Postfix, from userid 1001) id 98F565C0864; Sat, 28 Sep 2019 08:47:20 +0200 (CEST) Date: Sat, 28 Sep 2019 08:47:20 +0200 From: Nicholas Mc Guire To: Yizhuo Cc: csong@cs.ucr.edu, zhiyunq@cs.ucr.edu, Jonathan Cameron , Hartmut Knaack , Lars-Peter Clausen , Peter Meerwald-Stadler , Kevin Hilman , Martin Blumenstingl , Neil Armstrong , Thomas Gleixner , Nicholas Mc Guire , linux-iio@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-amlogic@lists.infradead.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] iio: adc: meson-saradc: Variables could be uninitalized if regmap_read() fails Message-ID: <20190928064720.GA24515@osadl.at> References: <20190928004642.28932-1-yzhai003@ucr.edu> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190928004642.28932-1-yzhai003@ucr.edu> User-Agent: Mutt/1.5.23 (2014-03-12) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Sep 27, 2019 at 05:46:41PM -0700, Yizhuo wrote: > Several functions in this file are trying to use regmap_read() to > initialize the specific variable, however, if regmap_read() fails, > the variable could be uninitialized but used directly, which is > potentially unsafe. The return value of regmap_read() should be > checked and handled. > > Signed-off-by: Yizhuo Just a few minor style issues - contentwise it look correct to me. Reviewed-by: Nicholas Mc Guire > --- > drivers/iio/adc/meson_saradc.c | 28 +++++++++++++++++++++++----- > 1 file changed, 23 insertions(+), 5 deletions(-) > > diff --git a/drivers/iio/adc/meson_saradc.c b/drivers/iio/adc/meson_saradc.c > index 7b28d045d271..c032a64108b4 100644 > --- a/drivers/iio/adc/meson_saradc.c > +++ b/drivers/iio/adc/meson_saradc.c > @@ -323,6 +323,7 @@ static int meson_sar_adc_wait_busy_clear(struct iio_dev *indio_dev) > { > struct meson_sar_adc_priv *priv = iio_priv(indio_dev); > int regval, timeout = 10000; > + int ret; > > /* > * NOTE: we need a small delay before reading the status, otherwise > @@ -331,7 +332,9 @@ static int meson_sar_adc_wait_busy_clear(struct iio_dev *indio_dev) > */ > do { > udelay(1); > - regmap_read(priv->regmap, MESON_SAR_ADC_REG0, ®val); > + ret = regmap_read(priv->regmap, MESON_SAR_ADC_REG0, ®val); > + if (ret) > + return ret; > } while (FIELD_GET(MESON_SAR_ADC_REG0_BUSY_MASK, regval) && timeout--); > > if (timeout < 0) > @@ -358,7 +361,11 @@ static int meson_sar_adc_read_raw_sample(struct iio_dev *indio_dev, any reason not to declear ret in the declaration block ? so just for consistency with coding style within meson_saradc.c this might be: int regval, fifo_chan, fifo_val, count; + int ret; > return -EINVAL; > } > > - regmap_read(priv->regmap, MESON_SAR_ADC_FIFO_RD, ®val); > + int ret = regmap_read(priv->regmap, MESON_SAR_ADC_FIFO_RD, ®val); + ret = regmap_read(priv->regmap, MESON_SAR_ADC_FIFO_RD, ®val); > + > + if (ret) > + return ret; > + > fifo_chan = FIELD_GET(MESON_SAR_ADC_FIFO_RD_CHAN_ID_MASK, regval); > if (fifo_chan != chan->address) { > dev_err(&indio_dev->dev, > @@ -491,6 +498,7 @@ static int meson_sar_adc_lock(struct iio_dev *indio_dev) > { > struct meson_sar_adc_priv *priv = iio_priv(indio_dev); > int val, timeout = 10000; > + int ret; > > mutex_lock(&indio_dev->mlock); > > @@ -506,7 +514,10 @@ static int meson_sar_adc_lock(struct iio_dev *indio_dev) > */ > do { > udelay(1); > - regmap_read(priv->regmap, MESON_SAR_ADC_DELAY, &val); > + ret = regmap_read(priv->regmap, > + MESON_SAR_ADC_DELAY, &val); checkpatch does not fuss here but the continuation should be alligned witht the ( here > + if (ret) > + return ret; > } while (val & MESON_SAR_ADC_DELAY_BL30_BUSY && timeout--); > > if (timeout < 0) { > @@ -784,7 +795,10 @@ static int meson_sar_adc_init(struct iio_dev *indio_dev) > * BL30 to make sure BL30 gets the values it expects when > * reading the temperature sensor. > */ > - regmap_read(priv->regmap, MESON_SAR_ADC_REG3, ®val); > + ret = regmap_read(priv->regmap, MESON_SAR_ADC_REG3, ®val); > + if (ret) > + return ret; > + > if (regval & MESON_SAR_ADC_REG3_BL30_INITIALIZED) > return 0; > } > @@ -1014,7 +1028,11 @@ static irqreturn_t meson_sar_adc_irq(int irq, void *data) > unsigned int cnt, threshold; > u32 regval; same as above + int ret; > > - regmap_read(priv->regmap, MESON_SAR_ADC_REG0, ®val); > + int ret = regmap_read(priv->regmap, MESON_SAR_ADC_REG0, ®val); + ret = regmap_read(priv->regmap, MESON_SAR_ADC_REG0, ®val); > + > + if (ret) > + return ret; > + > cnt = FIELD_GET(MESON_SAR_ADC_REG0_FIFO_COUNT_MASK, regval); > threshold = FIELD_GET(MESON_SAR_ADC_REG0_FIFO_CNT_IRQ_MASK, regval); > > -- > 2.17.1 >