All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] kcsan: Use min() to fix Coccinelle warning
@ 2024-06-23 22:06 Thorsten Blum
  2024-06-24  7:02 ` Marco Elver
  0 siblings, 1 reply; 6+ messages in thread
From: Thorsten Blum @ 2024-06-23 22:06 UTC (permalink / raw)
  To: elver, dvyukov; +Cc: kasan-dev, linux-kernel, Thorsten Blum

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@toblux.com>
---
 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));
 
 	if (copy_from_user(kbuf, buf, read_len))
 		return -EFAULT;
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] kcsan: Use min() to fix Coccinelle warning
  2024-06-23 22:06 [PATCH] kcsan: Use min() to fix Coccinelle warning Thorsten Blum
@ 2024-06-24  7:02 ` Marco Elver
  2024-06-24  8:00   ` Thorsten Blum
  2024-06-28 14:51   ` David Laight
  0 siblings, 2 replies; 6+ messages in thread
From: Marco Elver @ 2024-06-24  7:02 UTC (permalink / raw)
  To: Thorsten Blum; +Cc: dvyukov, kasan-dev, linux-kernel

On Mon, 24 Jun 2024 at 00:08, Thorsten Blum <thorsten.blum@toblux.com> wrote:
>
> 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@toblux.com>

Reviewed-by: Marco Elver <elver@google.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 () )

>         if (copy_from_user(kbuf, buf, read_len))
>                 return -EFAULT;
> --
> 2.45.2

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] kcsan: Use min() to fix Coccinelle warning
  2024-06-24  7:02 ` Marco Elver
@ 2024-06-24  8:00   ` Thorsten Blum
  2024-06-24  8:44     ` Marco Elver
  2024-06-28 14:51   ` David Laight
  1 sibling, 1 reply; 6+ messages in thread
From: Thorsten Blum @ 2024-06-24  8:00 UTC (permalink / raw)
  To: Marco Elver; +Cc: dvyukov, kasan-dev, linux-kernel

On 24. Jun 2024, at 00:02, Marco Elver <elver@google.com> wrote:
> On Mon, 24 Jun 2024 at 00:08, Thorsten Blum <thorsten.blum@toblux.com> wrote:
>> 
>> 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@toblux.com>
> 
> Reviewed-by: Marco Elver <elver@google.com>
> 
> Thanks for polishing (but see below). Please compile-test with
> CONFIG_KCSAN=y if you haven't.

Yes, I compile-tested it with CONFIG_KCSAN=y, but forgot to mention it.

> While we're here polishing things this could be:
> 
> const size_t read_len = min(count, sizeof(kbuf) - 1);
> 
> ( +const, remove redundant () )

Should I submit a v2 or are you adding this already?

Thanks,
Thorsten

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] kcsan: Use min() to fix Coccinelle warning
  2024-06-24  8:00   ` Thorsten Blum
@ 2024-06-24  8:44     ` Marco Elver
  0 siblings, 0 replies; 6+ messages in thread
From: Marco Elver @ 2024-06-24  8:44 UTC (permalink / raw)
  To: Thorsten Blum; +Cc: dvyukov, kasan-dev, linux-kernel

On Mon, 24 Jun 2024 at 10:00, Thorsten Blum <thorsten.blum@toblux.com> wrote:
>
> On 24. Jun 2024, at 00:02, Marco Elver <elver@google.com> wrote:
> > On Mon, 24 Jun 2024 at 00:08, Thorsten Blum <thorsten.blum@toblux.com> wrote:
> >>
> >> 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@toblux.com>
> >
> > Reviewed-by: Marco Elver <elver@google.com>
> >
> > Thanks for polishing (but see below). Please compile-test with
> > CONFIG_KCSAN=y if you haven't.
>
> Yes, I compile-tested it with CONFIG_KCSAN=y, but forgot to mention it.
>
> > While we're here polishing things this could be:
> >
> > const size_t read_len = min(count, sizeof(kbuf) - 1);
> >
> > ( +const, remove redundant () )
>
> Should I submit a v2 or are you adding this already?

Sending a v2 is cleaner, and also Cc Paul E. McKenney
<paulmck@kernel.org>, because the KCSAN patches go through the -rcu
kernel tree.

Thanks,
-- Marco

^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: [PATCH] kcsan: Use min() to fix Coccinelle warning
  2024-06-24  7:02 ` Marco Elver
  2024-06-24  8:00   ` Thorsten Blum
@ 2024-06-28 14:51   ` David Laight
  2024-06-28 15:03     ` Marco Elver
  1 sibling, 1 reply; 6+ messages in thread
From: David Laight @ 2024-06-28 14:51 UTC (permalink / raw)
  To: 'Marco Elver', Thorsten Blum
  Cc: dvyukov@google.com, kasan-dev@googlegroups.com,
	linux-kernel@vger.kernel.org

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@toblux.com>
> 
> Reviewed-by: Marco Elver <elver@google.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.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] kcsan: Use min() to fix Coccinelle warning
  2024-06-28 14:51   ` David Laight
@ 2024-06-28 15:03     ` Marco Elver
  0 siblings, 0 replies; 6+ messages in thread
From: Marco Elver @ 2024-06-28 15:03 UTC (permalink / raw)
  To: David Laight
  Cc: Thorsten Blum, dvyukov@google.com, kasan-dev@googlegroups.com,
	linux-kernel@vger.kernel.org

On Fri, 28 Jun 2024 at 16:52, David Laight <David.Laight@aculab.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@toblux.com>
> >
> > Reviewed-by: Marco Elver <elver@google.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.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-06-28 15:04 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-06-23 22:06 [PATCH] kcsan: Use min() to fix Coccinelle warning Thorsten Blum
2024-06-24  7:02 ` Marco Elver
2024-06-24  8:00   ` Thorsten Blum
2024-06-24  8:44     ` Marco Elver
2024-06-28 14:51   ` David Laight
2024-06-28 15:03     ` Marco Elver

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.