Re: [PATCH] string: use min in sized_strscpy

From: Andrew Morton

Date: Tue May 19 2026 - 15:37:45 EST


On Thu, 14 May 2026 18:56:03 +0200 Thorsten Blum <thorsten.blum@xxxxxxxxx> wrote:

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

Why is this code so messy. Never seen so many typecasts per inch.

> @@ -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))

That looks like IS_ALIGNED()?

> + max = min(PAGE_SIZE - ((long)src & (PAGE_SIZE - 1)), max);

That looks like a dog's breakfast.

And a bit like ALIGN_DOWN. Not quite, but I'm sure we have helpers for
whatever this is doing.

> #else
> /* If src or dest is unaligned, don't do word-at-a-time. */
> if (((long) dest | (long) src) & (sizeof(long) - 1))

gargh.


Oh well, not your fault. I'll grab the patch, thanks.