crypto: essiv - Replace memcpy() + NUL-termination with strscpy()
PATCH 1/1 crypto: essiv - Replace memcpy() + NUL-termination with strscpy()
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.
--- crypto/essiv.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
crypto/essiv.c +1 -2
@@ -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; }
Signature · 1 line
2.48.1
Herbert Xu
maintainerQuoted 9 lines · Thorsten Blum
Use
strscpy()to copy the NUL-terminated string 'p' to the destination buffer instead of usingmemcpy()followed by a manual NUL-termination.No functional changes intended.
Signed-off-by Thorsten Blum <[email protected]>--- crypto/essiv.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
Patch applied. Thanks.
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Quoted 15 lines · On Sun, 16 Mar 2025 22:15:04 +0100 Thorsten Blum <[email protected]>…
Use
strscpy()to copy the NUL-terminated string 'p' to the destination buffer instead of usingmemcpy()followed by a manual NUL-termination.No functional changes intended.
Signed-off-by Thorsten Blum <[email protected]>--- crypto/essiv.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)crypto/essiv.c +0
-0
crypto/essiv.c static bool parse_cipher_name(char *essiv_cipher_name, const char *cra_name) · line 408
Quoted 6 lines
crypto/essiv.c +1
-2@@ -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
Quoted 3 lines
crypto/essiv.c +0
-0return true; }
Hi David,
Quoted 5 lines · David Laight
Quoted 3 lines · 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 usingmemcpy()followed by a manual NUL-termination.
Quoted 7 lines
Quoted 4 lines
No functional changes intended.
Signed-off-by Thorsten Blum <[email protected]>---
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).
* ...
*/
Quoted 3 lines
In this case the bound check is before the copy (and the buffer assumed to be the right size!)
Somemcpy()+ 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