linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Arnd Bergmann <arnd@kernel.org>
To: Dongchun Zhu <dongchun.zhu@mediatek.com>
Cc: Mauro Carvalho Chehab <mchehab@kernel.org>,
	Nathan Chancellor <natechancellor@gmail.com>,
	Nick Desaulniers <ndesaulniers@google.com>,
	Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	Sakari Ailus <sakari.ailus@linux.intel.com>,
	Arnd Bergmann <arnd@arndb.de>,
	Linux Media Mailing List <linux-media@vger.kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	clang-built-linux <clang-built-linux@googlegroups.com>,
	tfiga@google.com, sj.huang@mediatek.com
Subject: Re: [PATCH] media: i2c: fix an uninitialized error code
Date: Fri, 4 Dec 2020 09:22:02 +0100	[thread overview]
Message-ID: <CAK8P3a0ZA4cLNL3kVN9qKjHyhWJXOA2LtsEakN0V9rcTBQaVNw@mail.gmail.com> (raw)
In-Reply-To: <1607047828.4733.172.camel@mhfsdcap03>

On Fri, Dec 4, 2020 at 3:10 AM Dongchun Zhu <dongchun.zhu@mediatek.com> wrote:
>
> Hi Arnd,
>
> Thanks for the patch.
>
> On Thu, 2020-12-03 at 23:29 +0100, Arnd Bergmann wrote:
> > From: Arnd Bergmann <arnd@arndb.de>
> >
> > Clang points out that the error handling in ov02a10_s_stream() is
> > broken, and just returns a random error code:
> >
> > drivers/media/i2c/ov02a10.c:537:6: warning: variable 'ret' is used uninitialized whenever 'if' condition is true [-Wsometimes-uninitialized]
> >         if (ov02a10->streaming == on)
> >             ^~~~~~~~~~~~~~~~~~~~~~~~
> > drivers/media/i2c/ov02a10.c:568:9: note: uninitialized use occurs here
> >         return ret;
> >                ^~~
> > drivers/media/i2c/ov02a10.c:537:2: note: remove the 'if' if its condition is always false
> >         if (ov02a10->streaming == on)
> >         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > drivers/media/i2c/ov02a10.c:533:9: note: initialize the variable 'ret' to silence this warning
> >         int ret;
> >
> > I assume that -EBUSY is the intended error code, so use that.
> >
> > Fixes: 91807efbe8ec ("media: i2c: add OV02A10 image sensor driver")
> > Signed-off-by: Arnd Bergmann <arnd@arndb.de>
> > ---
> >  drivers/media/i2c/ov02a10.c | 4 +++-
> >  1 file changed, 3 insertions(+), 1 deletion(-)
> >
> > diff --git a/drivers/media/i2c/ov02a10.c b/drivers/media/i2c/ov02a10.c
> > index 391718136ade..7ee9c904d9b5 100644
> > --- a/drivers/media/i2c/ov02a10.c
> > +++ b/drivers/media/i2c/ov02a10.c
> > @@ -534,8 +534,10 @@ static int ov02a10_s_stream(struct v4l2_subdev *sd, int on)
> >
> >       mutex_lock(&ov02a10->mutex);
> >
> > -     if (ov02a10->streaming == on)
> > +     if (ov02a10->streaming == on) {
> > +             ret = -EBUSY;
> >               goto unlock_and_return;
> > +     }
> >
> >       if (on) {
> >               ret = pm_runtime_get_sync(&client->dev);
>
> Only if sensor fails to stream on, ret can return a negative error code.
> Thus ret above needs to be initialized to '0'.

Ok, I sent a version 2.

> Also you could fix the clang error like this.
>
> static int ov02a10_s_stream(struct v4l2_subdev *sd, int on)
> {
>         struct ov02a10 *ov02a10 = to_ov02a10(sd);
>         ...
>         int ret = 0;
>
>         ...
>         if (ov02a10->streaming == on)
>                 goto unlock_and_return;
>
>         ...
> }

Sorry, I should have removed the last line of the output from
clang that suggested doing that. Initializing a local variable
in the declaration is generally a bad idea precisely because it
prevents the compiler from warning about a case like this where
the author forgot to set the correct error value.

We should really fix clang to not propagate that nonsense.

        Arnd

      reply	other threads:[~2020-12-04  8:23 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-03 22:29 [PATCH] media: i2c: fix an uninitialized error code Arnd Bergmann
2020-12-04  2:10 ` Dongchun Zhu
2020-12-04  8:22   ` Arnd Bergmann [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAK8P3a0ZA4cLNL3kVN9qKjHyhWJXOA2LtsEakN0V9rcTBQaVNw@mail.gmail.com \
    --to=arnd@kernel.org \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=arnd@arndb.de \
    --cc=clang-built-linux@googlegroups.com \
    --cc=dongchun.zhu@mediatek.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=mchehab@kernel.org \
    --cc=natechancellor@gmail.com \
    --cc=ndesaulniers@google.com \
    --cc=sakari.ailus@linux.intel.com \
    --cc=sj.huang@mediatek.com \
    --cc=tfiga@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).