[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <7060edadcbac4452bd70d7894a50568d@AcuMS.aculab.com>
Date: Sat, 10 Feb 2024 13:02:20 +0000
From: David Laight <David.Laight@...LAB.COM>
To: 'Pavel Machek' <pavel@....cz>, Justin Stitt <justinstitt@...gle.com>
CC: Kees Cook <keescook@...omium.org>, Andy Shevchenko <andy@...nel.org>,
Rasmus Villemoes <linux@...musvillemoes.dk>,
"linux-hardening@...r.kernel.org" <linux-hardening@...r.kernel.org>,
"linux-kernel@...r.kernel.org" <linux-kernel@...r.kernel.org>
Subject: RE: [RFC] string: Allow 2-argument strscpy()
From: Pavel Machek
> Sent: 07 February 2024 17:52
>
> > > Using sizeof(dst) is the overwhelmingly common case for strscpy().
> > > Instead of requiring this everywhere, allow a 2-argument version to be
> > > used that will use the sizeof() internally.
> >
> > Yeah, this is definitely the case. I have a ton of patches replacing
> > strncpy with strscpy [1] and many of them match the pattern of:
> > | strscpy(dest, src, sizeof(dest))
> >
> > BTW, this hack for function overloading is insane. Never really looked into
> > it before.
>
> This hack is insane, but this is also highly confusing, please don't
> do this.
An alternative would be to convert xxx(tgt, src, 0) to
xxx(tgt, src, sizeof (tgt) - that is when the specified
length is a compile-time constant zero.
Either with:
(__builtin_constat_p(len) && (len) == 0 ? sizeof (dst) : (len))
Or, leveraging is_constexpr() and doing (I've probably got the syntax wrong):
__Generic(0 ? (void *)(len) : (int *)0,
void *: len,
int *: sizeof (dst))
That probably needs a helper:
is_constzero(value, if_zero, if_non_zero)
to make it more generally useful.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
Powered by blists - more mailing lists