public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Dan Carpenter <error27@gmail.com>
To: Josua Mayer <josua@solid-run.com>
Cc: "kernel-janitors@vger.kernel.org"
	<kernel-janitors@vger.kernel.org>, Peter Rosin <peda@axentia.se>,
	"kees@kernel.org" <kees@kernel.org>,
	"thorsten.blum@linux.dev" <thorsten.blum@linux.dev>,
	"ulfh@kernel.org" <ulfh@kernel.org>,
	Wolfram Sang <wsa+renesas@sang-engineering.com>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: [bug report] mux: Add helper functions for getting optional and selected mux-state
Date: Mon, 20 Apr 2026 17:45:18 +0300	[thread overview]
Message-ID: <aeY7_meA7Qe0q8ld@stanley.mountain> (raw)
In-Reply-To: <f76494f0-e44e-49de-bee1-84399307f3f0@solid-run.com>

On Sun, Apr 19, 2026 at 10:16:30AM +0000, Josua Mayer wrote:
> Hi Dan,
> 
> Am 10.04.26 um 12:12 schrieb Dan Carpenter:
> > Hello Josua Mayer,
> >
> > Commit 993bcaf32c49 ("mux: Add helper functions for getting optional
> > and selected mux-state") from Feb 26, 2026 (linux-next), leads to the
> > following Smatch static checker warning:
> >
> > 	drivers/mux/core.c:640 mux_control_get()
> > 	warn: 'mux' is an error pointer or valid
> >
> > drivers/mux/core.c
> >     630  * mux_control_get() - Get the mux-control for a device.
> >     631  * @dev: The device that needs a mux-control.
> >     632  * @mux_name: The name identifying the mux-control.
> >     633  *
> >     634  * Return: A pointer to the mux-control, or an ERR_PTR with a negative errno.
> >     635  */
> >     636 struct mux_control *mux_control_get(struct device *dev, const char *mux_name)
> >     637 {
> >     638         struct mux_control *mux = mux_get(dev, mux_name, NULL, false);
> >
> > mux_get() can only return NULL if optional is true.
> 
> Yes, that is the intended contract. This means function can be simplified:
> 
> /**
>  * mux_control_get() - Get the mux-control for a device.
>  * @dev: The device that needs a mux-control.
>  * @mux_name: The name identifying the mux-control.
>  *
>  * Return: A pointer to the mux-control, or an ERR_PTR with a negative errno.
>  */
> struct mux_control *mux_control_get(struct device *dev, const char *mux_name)
> {
>     return mux_get(dev, mux_name, NULL, false);
> }
> EXPORT_SYMBOL_GPL(mux_control_get);
> 
> Is it okay to trust such transitive contracts and not check for NULL
> in an exported generic helper function?
> 

Yes.  If you don't pass "optional" then you don't need to check for
NULL.  If it's buggy, just fix it.  Predicting and working around future
bugs is never going to work.

> >
> >     639 
> > --> 640         if (!mux)
> >
> > this should be if (IS_ERR(mux)) {
> >
> >     641                 return ERR_PTR(-ENOENT);
> No, ENOENT is only the fix for unexpected NULL return,
> which must not be propagated to the caller.
> 
> Other errors should be returned to the caller unchanged.
> 

I wrote a blog explaining how mixed NULL and error pointers work.
https://staticthinking.wordpress.com/2022/08/01/mixing-error-pointers-and-null/

There are some subsystems where they use a "special" error pointer
like -ENOENT to mean "not found" instead of returning NULL but
inevitably someone is going to return -ENOENT for something else.

regards,
dan carpenter

      reply	other threads:[~2026-04-20 14:45 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <adjNEl0C8OgEjMI7@stanley.mountain>
2026-04-19 10:16 ` [bug report] mux: Add helper functions for getting optional and selected mux-state Josua Mayer
2026-04-20 14:45   ` Dan Carpenter [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=aeY7_meA7Qe0q8ld@stanley.mountain \
    --to=error27@gmail.com \
    --cc=josua@solid-run.com \
    --cc=kees@kernel.org \
    --cc=kernel-janitors@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peda@axentia.se \
    --cc=thorsten.blum@linux.dev \
    --cc=ulfh@kernel.org \
    --cc=wsa+renesas@sang-engineering.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