Re: [PATCH V2 1/2] string: Add stracpy and stracpy_pad mechanisms
From: Rasmus Villemoes
Date: Tue Jul 23 2019 - 10:37:07 EST
On 23/07/2019 15.51, Joe Perches wrote:
> Several uses of strlcpy and strscpy have had defects because the
> last argument of each function is misused or typoed.
>
> Add macro mechanisms to avoid this defect.
>
> stracpy (copy a string to a string array) must have a string
> array as the first argument (dest) and uses sizeof(dest) as the
> count of bytes to copy.
>
> These mechanisms verify that the dest argument is an array of
> char or other compatible types like u8 or s8 or equivalent.
Sorry, but "compatible types" has a very specific meaning in C, so
please don't use that word. And yes, the kernel disables -Wpointer-sign,
so passing an u8* or s8* when strscpy() expects a char* is silently
accepted, but does such code exist?
>
> V2: Use __same_type testing char[], signed char[], and unsigned char[]
> Rename to, from, and size, dest, src and count
count is just as bad as size in terms of "the expression src might
contain that identifier". But there's actually no reason to even declare
a local variable, just use ARRAY_SIZE() directly as the third argument
to strscpy().
Rasmus