The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH] string: use min in sized_strscpy
@ 2026-05-14 16:56 Thorsten Blum
  2026-05-15  6:45 ` Andy Shevchenko
  0 siblings, 1 reply; 3+ messages in thread
From: Thorsten Blum @ 2026-05-14 16:56 UTC (permalink / raw)
  To: Andrew Morton, Kees Cook, Andy Shevchenko
  Cc: Thorsten Blum, linux-kernel, linux-hardening

Use min() and drop the limit variable to simplify sized_strscpy().

Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
---
 lib/string.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/lib/string.c b/lib/string.c
index b632c71df1a5..1f9297e9776a 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -21,6 +21,7 @@
 #include <linux/errno.h>
 #include <linux/limits.h>
 #include <linux/linkage.h>
+#include <linux/minmax.h>
 #include <linux/stddef.h>
 #include <linux/string.h>
 #include <linux/types.h>
@@ -125,11 +126,8 @@ ssize_t sized_strscpy(char *dest, const char *src, size_t count)
 	 * If src is unaligned, don't cross a page boundary,
 	 * since we don't know if the next page is mapped.
 	 */
-	if ((long)src & (sizeof(long) - 1)) {
-		size_t limit = PAGE_SIZE - ((long)src & (PAGE_SIZE - 1));
-		if (limit < max)
-			max = limit;
-	}
+	if ((long)src & (sizeof(long) - 1))
+		max = min(PAGE_SIZE - ((long)src & (PAGE_SIZE - 1)), max);
 #else
 	/* If src or dest is unaligned, don't do word-at-a-time. */
 	if (((long) dest | (long) src) & (sizeof(long) - 1))

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

* Re: [PATCH] string: use min in sized_strscpy
  2026-05-14 16:56 [PATCH] string: use min in sized_strscpy Thorsten Blum
@ 2026-05-15  6:45 ` Andy Shevchenko
  2026-05-15 15:09   ` Thorsten Blum
  0 siblings, 1 reply; 3+ messages in thread
From: Andy Shevchenko @ 2026-05-15  6:45 UTC (permalink / raw)
  To: Thorsten Blum
  Cc: Andrew Morton, Kees Cook, Andy Shevchenko, linux-kernel,
	linux-hardening

On Thu, May 14, 2026 at 7:56 PM Thorsten Blum <thorsten.blum@linux.dev> wrote:
>
> Use min() and drop the limit variable to simplify sized_strscpy().

...

> -       if ((long)src & (sizeof(long) - 1)) {
> -               size_t limit = PAGE_SIZE - ((long)src & (PAGE_SIZE - 1));
> -               if (limit < max)
> -                       max = limit;
> -       }
> +       if ((long)src & (sizeof(long) - 1))
> +               max = min(PAGE_SIZE - ((long)src & (PAGE_SIZE - 1)), max);

Side note: Isn't simply
               max = min(PAGE_SIZE - offset_in_page(src), max);

? (One will need to include linux/mm.h for this, though.)

Moreover there are plenty of duplications to count the size in the
first page and even the similar min() as in
  fs/iomap/buffered-io.c:869
  arch/s390/kvm/gaccess.c:976
and many more...

Perhaps a new macro with a good (famous last words!) naming?

-- 
With Best Regards,
Andy Shevchenko

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

* Re: [PATCH] string: use min in sized_strscpy
  2026-05-15  6:45 ` Andy Shevchenko
@ 2026-05-15 15:09   ` Thorsten Blum
  0 siblings, 0 replies; 3+ messages in thread
From: Thorsten Blum @ 2026-05-15 15:09 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Andrew Morton, Kees Cook, Andy Shevchenko, linux-kernel,
	linux-hardening

On Fri, May 15, 2026 at 09:45:08AM +0300, Andy Shevchenko wrote:
> On Thu, May 14, 2026 at 7:56 PM Thorsten Blum <thorsten.blum@linux.dev> wrote:
> >
> > Use min() and drop the limit variable to simplify sized_strscpy().
> 
> ...
> 
> > -       if ((long)src & (sizeof(long) - 1)) {
> > -               size_t limit = PAGE_SIZE - ((long)src & (PAGE_SIZE - 1));
> > -               if (limit < max)
> > -                       max = limit;
> > -       }
> > +       if ((long)src & (sizeof(long) - 1))
> > +               max = min(PAGE_SIZE - ((long)src & (PAGE_SIZE - 1)), max);
> 
> Side note: Isn't simply
>                max = min(PAGE_SIZE - offset_in_page(src), max);
> 
> ? (One will need to include linux/mm.h for this, though.)

I thought about it, but wasn't sure if pulling in all of linux/mm.h into
lib/string.c is worth it.

> Moreover there are plenty of duplications to count the size in the
> first page and even the similar min() as in
>   fs/iomap/buffered-io.c:869
>   arch/s390/kvm/gaccess.c:976
> and many more...
> 
> Perhaps a new macro with a good (famous last words!) naming?

How about adding

#define offset_in_page(p)	((unsigned long)(p) & ~PAGE_MASK)
#define bytes_to_page_end(p)	(PAGE_SIZE - offset_in_page(p))

to a new header, e.g. include/linux/page_helpers.h? There are about 50+
direct replacements and possibly more.

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

end of thread, other threads:[~2026-05-15 15:09 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-14 16:56 [PATCH] string: use min in sized_strscpy Thorsten Blum
2026-05-15  6:45 ` Andy Shevchenko
2026-05-15 15:09   ` Thorsten Blum

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox