lkml.org 
[lkml]   [2021]   [Jul]   [27]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
From
Date
SubjectRe: [PATCH v2 1/3] base: mark 'no_warn' as unused
On Tue, Jul 27, 2021 at 1:17 PM Segher Boessenkool
<segher@kernel.crashing.org> wrote:
>
> On Tue, Jul 27, 2021 at 07:59:24PM +0200, Greg Kroah-Hartman wrote:
> > On Tue, Jul 27, 2021 at 10:39:49AM -0700, Nick Desaulniers wrote:
> > > I think warn_unused_result should only really be used for functions
> > > where the return value should be used 100% of the time.
> >
> > I too want a shiny new pony.
> >
> > But here in the real world, sometimes you have functions that for 99% of
> > the users, you do want them to check the return value, but when you use
> > them in core code or startup code, you "know" you are safe to ignore the
> > return value.
> >
> > That is the case here. We have other fun examples of where people have
> > tried to add error handling to code that runs at boot that have actually
> > introduced security errors and they justify it with "but you have to
> > check error values!"
> >
> > > If there are
> > > cases where it's ok to not check the return value, consider not using
> > > warn_unused_result on function declarations.
> >
> > Ok, so what do you do when you have a function like this where 99.9% of
> > the users need to check this? Do I really need to write a wrapper
> > function just for it so that I can use it "safely" in the core code
> > instead?
> >
> > Something like:
> >
> > void do_safe_thing_and_ignore_the_world(...)
> > {
> > __unused int error;
> >
> > error = do_thing(...);
> > }
> >
> > Or something else to get the compiler to be quiet about error being set
> > and never used?
>
> The simplest is to write
> if (do_thing()) {
> /* Nothing here, we can safely ignore the return value
> * here, because of X and Y and I don't know, I have no
> * idea actually why we can in this example. Hopefully
> * in real code people do have a good reason :-)
> */
> }
>
> which should work in *any* compiler, doesn't need any extension, is
> quite elegant, and encourages documenting why we ignore the return
> value here.
>
Or better still, use sysfs_create_link_nowarn() instead of
sysfs_create_link(). We'll just have to take the "__must_check"
attribute off the sysfs_create_link_nowarn() declaration.

-bw

\
 
 \ /
  Last update: 2021-07-27 22:23    [W:0.147 / U:1.448 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site