Re: [PATCH] checkpatch: add a new check for strcpy/strlcpy uses
From: Joe Perches
Date: Tue Jan 05 2021 - 03:45:08 EST
On Tue, 2021-01-05 at 13:53 +0530, Dwaipayan Ray wrote:
> strcpy() performs no bounds checking on the destination buffer.
> This could result in linear overflows beyond the end of the buffer.
>
> strlcpy() reads the entire source buffer first. This read
> may exceed the destination size limit. This can be both inefficient
> and lead to linear read overflows.
>
> The safe replacement to both of these is to use strscpy() instead.
> Add a new checkpatch warning which alerts the user on finding usage of
> strcpy() or strlcpy().
I do not believe that strscpy is preferred over strcpy.
When the size of the output buffer is known to be larger
than the input, strcpy is faster.
There are about 2k uses of strcpy.
Is there a use where strcpy use actually matters?
I don't know offhand...
But I believe compilers do not optimize away the uses of strscpy
to a simple memcpy like they do for strcpy with a const from
strcpy(foo, "bar");
And lastly there is a existing strlcpy test in checkpatch.
commit 5dbdb2d87c29 ("checkpatch: prefer strscpy to strlcpy")