RE: [RFC] string: Allow 2-argument strscpy()
From: David Laight
Date: Sat Feb 10 2024 - 08:51:56 EST
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.
A much simpler 'hack' - here defaulting in 16.
Although you'd probably want a compile-time check on the
number of arguments.
And convert 'sizeof 'non-array' to (say) -1 and error
a length that is a constant -1.
David
extern int func_2(void *, const void *, int);
#define func_1(_p0, _p1, len, ...) func_2(_p0, _p1, len)
#define func(_p0, ...) func_1(_p0, __VA_ARGS__, 16)
int f1(void *p0, const void *p1)
{
return func(p0, p1);
}
int f2(void *p0, const void *p1, int len)
{
return func(p0, p1, len);
}
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)