* [PATCH] crypto: essiv - Replace memcpy() + NUL-termination with strscpy()
@ 2025-03-16 21:15 Thorsten Blum
2025-03-21 11:07 ` Herbert Xu
2025-03-23 10:20 ` David Laight
0 siblings, 2 replies; 4+ messages in thread
From: Thorsten Blum @ 2025-03-16 21:15 UTC (permalink / raw)
To: Herbert Xu, David S. Miller; +Cc: Thorsten Blum, linux-crypto, linux-kernel
Use strscpy() to copy the NUL-terminated string 'p' to the destination
buffer instead of using memcpy() followed by a manual NUL-termination.
No functional changes intended.
Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
---
crypto/essiv.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/crypto/essiv.c b/crypto/essiv.c
index 1c00c3324058..ec0ec8992c2d 100644
--- a/crypto/essiv.c
+++ b/crypto/essiv.c
@@ -405,8 +405,7 @@ static bool parse_cipher_name(char *essiv_cipher_name, const char *cra_name)
if (len >= CRYPTO_MAX_ALG_NAME)
return false;
- memcpy(essiv_cipher_name, p, len);
- essiv_cipher_name[len] = '\0';
+ strscpy(essiv_cipher_name, p, len + 1);
return true;
}
--
2.48.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] crypto: essiv - Replace memcpy() + NUL-termination with strscpy()
2025-03-16 21:15 [PATCH] crypto: essiv - Replace memcpy() + NUL-termination with strscpy() Thorsten Blum
@ 2025-03-21 11:07 ` Herbert Xu
2025-03-23 10:20 ` David Laight
1 sibling, 0 replies; 4+ messages in thread
From: Herbert Xu @ 2025-03-21 11:07 UTC (permalink / raw)
To: Thorsten Blum; +Cc: David S. Miller, linux-crypto, linux-kernel
On Sun, Mar 16, 2025 at 10:15:04PM +0100, Thorsten Blum wrote:
> Use strscpy() to copy the NUL-terminated string 'p' to the destination
> buffer instead of using memcpy() followed by a manual NUL-termination.
>
> No functional changes intended.
>
> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
> ---
> crypto/essiv.c | 3 +--
> 1 file changed, 1 insertion(+), 2 deletions(-)
Patch applied. Thanks.
--
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] crypto: essiv - Replace memcpy() + NUL-termination with strscpy()
2025-03-16 21:15 [PATCH] crypto: essiv - Replace memcpy() + NUL-termination with strscpy() Thorsten Blum
2025-03-21 11:07 ` Herbert Xu
@ 2025-03-23 10:20 ` David Laight
2025-03-23 13:08 ` Thorsten Blum
1 sibling, 1 reply; 4+ messages in thread
From: David Laight @ 2025-03-23 10:20 UTC (permalink / raw)
To: Thorsten Blum; +Cc: Herbert Xu, David S. Miller, linux-crypto, linux-kernel
On Sun, 16 Mar 2025 22:15:04 +0100
Thorsten Blum <thorsten.blum@linux.dev> wrote:
> Use strscpy() to copy the NUL-terminated string 'p' to the destination
> buffer instead of using memcpy() followed by a manual NUL-termination.
>
> No functional changes intended.
>
> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
> ---
> crypto/essiv.c | 3 +--
> 1 file changed, 1 insertion(+), 2 deletions(-)
>
> diff --git a/crypto/essiv.c b/crypto/essiv.c
> index 1c00c3324058..ec0ec8992c2d 100644
> --- a/crypto/essiv.c
> +++ b/crypto/essiv.c
> @@ -405,8 +405,7 @@ static bool parse_cipher_name(char *essiv_cipher_name, const char *cra_name)
> if (len >= CRYPTO_MAX_ALG_NAME)
> return false;
>
> - memcpy(essiv_cipher_name, p, len);
> - essiv_cipher_name[len] = '\0';
> + strscpy(essiv_cipher_name, p, len + 1);
That is just 'so wrong'.
The 'len' argument to strscpy() is supposed to be the length of the
buffer (in order to avoid overflow) not the number of characters.
In this case the bound check is before the copy (and the buffer assumed
to be the right size!)
So memcpy() + terminate is exactly correct.
The warning gcc emits for strncpy() when the length depends on the source
string is equally applicable to strscpy().
David
> return true;
> }
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] crypto: essiv - Replace memcpy() + NUL-termination with strscpy()
2025-03-23 10:20 ` David Laight
@ 2025-03-23 13:08 ` Thorsten Blum
0 siblings, 0 replies; 4+ messages in thread
From: Thorsten Blum @ 2025-03-23 13:08 UTC (permalink / raw)
To: David Laight; +Cc: Herbert Xu, David S. Miller, linux-crypto, linux-kernel
Hi David,
On 23. Mar 2025, at 11:20, David Laight wrote:
> On Sun, 16 Mar 2025 22:15:04 +0100 Thorsten Blum wrote:
>
>> Use strscpy() to copy the NUL-terminated string 'p' to the destination
>> buffer instead of using memcpy() followed by a manual NUL-termination.
>>
>> No functional changes intended.
>>
>> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
>> ---
>
> The 'len' argument to strscpy() is supposed to be the length of the
> buffer (in order to avoid overflow) not the number of characters.
Not necessarily, see linux/string.h:
/**
* strscpy - Copy a C-string into a sized buffer
* ...
* The size argument @... is only required when @dst is not an array, or
* when the copy needs to be smaller than sizeof(@dst).
* ...
*/
> In this case the bound check is before the copy (and the buffer assumed
> to be the right size!)
> So memcpy() + terminate is exactly correct.
Yes, this is simply a refactoring, there's nothing wrong with memcpy()
followed by a manual NUL-termination.
However, strscpy() is shorter and semantically better imo because we're
copying C-strings and not just raw bytes. strscpy() also has additional
compile-time checks regarding C-strings that memcpy() doesn't.
Thanks,
Thorsten
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2025-03-23 13:09 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-16 21:15 [PATCH] crypto: essiv - Replace memcpy() + NUL-termination with strscpy() Thorsten Blum
2025-03-21 11:07 ` Herbert Xu
2025-03-23 10:20 ` David Laight
2025-03-23 13:08 ` Thorsten Blum
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox