From: Leo Yan <leo.yan@linaro.org>
To: Dan Carpenter <dan.carpenter@oracle.com>
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>,
Suzuki K Poulose <suzuki.poulose@arm.com>,
Alexander Shishkin <alexander.shishkin@linux.intel.com>,
kernel-janitors@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-arm-kernel@lists.infradead.org
Subject: Re: [PATCH] coresight: potential uninitialized variable in probe()
Date: Thu, 13 Jun 2019 17:56:37 +0800 [thread overview]
Message-ID: <20190613095637.GA5242@leoy-ThinkPad-X240s> (raw)
In-Reply-To: <20190613081419.GG1893@kadam>
Hi Dan,
On Thu, Jun 13, 2019 at 11:14:19AM +0300, Dan Carpenter wrote:
> On Thu, Jun 13, 2019 at 03:49:22PM +0800, Leo Yan wrote:
> > Hi Dan,
> >
> > On Wed, Jun 12, 2019 at 11:58:15PM -0700, Dan Carpenter wrote:
> > > The "drvdata->atclk" clock is optional, but if it gets set to an error
> > > pointer then we're accidentally return an uninitialized variable instead
> > > of success.
> >
> > You are right, thanks a lot for pointing out.
> >
> > I'd like to initialize 'ret = 0' at the head of function, so we can
> > has the same fashion with other CoreSight drivers (e.g. replicator).
> >
> > static int funnel_probe(struct device *dev, struct resource *res)
> > {
> > - int ret;
> > + int ret = 0;
> >
> > If you agree, could you send a new patch for this?
>
> Obviously that's an option I considered... The reason I didn't go with
> that is that a common bug that I see is:
>
> int ret = 0;
>
> p = kmalloc();
> if (!p)
> goto free_whatever;
>
> In my experience it's better to initialize the return as late as
> possible so that you get static checker warnings when you forget to set
> the error code.
Just want to check one thing, which static checker you are using?
I use sparse but it doesn't report this issue (I learned coccinelle
also can do this but it outputs verbose logs).
To be honest, I didn't often run static checker when committed patches,
but hope later can improve for this.
> Also I think my way is more readable. I like to make the success path
> as explicit as possible. I hate when people do things like:
>
> if (!ret)
> return ret;
>
> About 10% of the time when you see this it is a bug, but it's hard to
> tell because it's not readable like it would be if people did:
>
> if (!ret)
> return 0;
>
> Or sometimes you see things like:
>
> if (corner_case)
> goto free; /* success path */
>
> Without the "/* success path */ comment explaining why we're returning
> zero most readers will assume it's a mistake.
Thanks for sharing much knowledge; your change is okay for me.
I think the point is the good habit can avoid pitfall and traps :) [1]
Thanks,
Leo Yan
[1] https://www.amazon.com/C-Traps-Pitfalls-Andrew-Koenig/dp/0201179288
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
next prev parent reply other threads:[~2019-06-13 9:56 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-06-13 6:58 [PATCH] coresight: potential uninitialized variable in probe() Dan Carpenter
2019-06-13 7:49 ` Leo Yan
2019-06-13 8:14 ` Dan Carpenter
2019-06-13 9:56 ` Leo Yan [this message]
2019-06-13 10:10 ` Leo Yan
2019-06-17 19:27 ` Mathieu Poirier
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=20190613095637.GA5242@leoy-ThinkPad-X240s \
--to=leo.yan@linaro.org \
--cc=alexander.shishkin@linux.intel.com \
--cc=dan.carpenter@oracle.com \
--cc=kernel-janitors@vger.kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mathieu.poirier@linaro.org \
--cc=suzuki.poulose@arm.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).