[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <CANpmjNPoYaCVTHONGhN3ZJgd_yzUSMmjib+EBKbHG14xLnrQwg@mail.gmail.com>
Date: Fri, 28 Jun 2024 17:03:59 +0200
From: Marco Elver <elver@...gle.com>
To: David Laight <David.Laight@...lab.com>
Cc: Thorsten Blum <thorsten.blum@...lux.com>, "dvyukov@...gle.com" <dvyukov@...gle.com>,
"kasan-dev@...glegroups.com" <kasan-dev@...glegroups.com>,
"linux-kernel@...r.kernel.org" <linux-kernel@...r.kernel.org>
Subject: Re: [PATCH] kcsan: Use min() to fix Coccinelle warning
On Fri, 28 Jun 2024 at 16:52, David Laight <David.Laight@...lab.com> wrote:
>
> From: Marco Elver
> > Sent: 24 June 2024 08:03
> > >
> > > Fixes the following Coccinelle/coccicheck warning reported by
> > > minmax.cocci:
> > >
> > > WARNING opportunity for min()
> > >
> > > Use size_t instead of int for the result of min().
> > >
> > > Signed-off-by: Thorsten Blum <thorsten.blum@...lux.com>
> >
> > Reviewed-by: Marco Elver <elver@...gle.com>
> >
> > Thanks for polishing (but see below). Please compile-test with
> > CONFIG_KCSAN=y if you haven't.
> >
> > > ---
> > > kernel/kcsan/debugfs.c | 2 +-
> > > 1 file changed, 1 insertion(+), 1 deletion(-)
> > >
> > > diff --git a/kernel/kcsan/debugfs.c b/kernel/kcsan/debugfs.c
> > > index 1d1d1b0e4248..11b891fe6f7a 100644
> > > --- a/kernel/kcsan/debugfs.c
> > > +++ b/kernel/kcsan/debugfs.c
> > > @@ -225,7 +225,7 @@ debugfs_write(struct file *file, const char __user *buf, size_t count, loff_t *o
> > > {
> > > char kbuf[KSYM_NAME_LEN];
> > > char *arg;
> > > - int read_len = count < (sizeof(kbuf) - 1) ? count : (sizeof(kbuf) - 1);
> > > + size_t read_len = min(count, (sizeof(kbuf) - 1));
> >
> > While we're here polishing things this could be:
> >
> > const size_t read_len = min(count, sizeof(kbuf) - 1);
> >
> > ( +const, remove redundant () )
>
> Pretty much no one makes variables 'const', it mostly just makes the code harder to read.
This is very much subjective. In my subjective opinion, it makes the
code easier to understand and it'll be harder to introduce accidental
mistakes. For trivial cases like this it really doesn't matter though.
Powered by blists - more mailing lists