diff mbox series

[RFC,1/3] powerpc/ps3: refactor strncpy usage attempt 1

Message ID 20230811-strncpy-arch-powerpc-platforms-ps3-v1-1-301052a5663e@google.com (mailing list archive)
State Superseded
Headers show
Series powerpc/ps3: refactor strncpy usage | expand

Commit Message

Justin Stitt Aug. 11, 2023, 9:19 p.m. UTC
This approach simply replicates the implementation of `make_field` which
means we drop `strncpy` for `memcpy`.

Signed-off-by: Justin Stitt <justinstitt@google.com>
---
 arch/powerpc/platforms/ps3/repository.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Michael Ellerman Aug. 14, 2023, 12:28 p.m. UTC | #1
Justin Stitt <justinstitt@google.com> writes:
> This approach simply replicates the implementation of `make_field` which
> means we drop `strncpy` for `memcpy`.
>
> Signed-off-by: Justin Stitt <justinstitt@google.com>
> ---
>  arch/powerpc/platforms/ps3/repository.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/arch/powerpc/platforms/ps3/repository.c b/arch/powerpc/platforms/ps3/repository.c
> index 205763061a2d..1abe33fbe529 100644
> --- a/arch/powerpc/platforms/ps3/repository.c
> +++ b/arch/powerpc/platforms/ps3/repository.c
> @@ -73,9 +73,9 @@ static void _dump_node(unsigned int lpar_id, u64 n1, u64 n2, u64 n3, u64 n4,
>  
>  static u64 make_first_field(const char *text, u64 index)
>  {
> -	u64 n;
> +	u64 n = 0;
>  
> -	strncpy((char *)&n, text, 8);
> +	memcpy((char *)&n, text, strnlen(text, sizeof(n)));
>  	return PS3_VENDOR_ID_NONE + (n >> 32) + index;
>  }

I guess it's a slight improvement, because people generally know
memcpy's behaviour better than strncpy.

The change log should be a bit more verbose and mention that the result
is the same, including the NULL padding done my strncpy().

cheers
diff mbox series

Patch

diff --git a/arch/powerpc/platforms/ps3/repository.c b/arch/powerpc/platforms/ps3/repository.c
index 205763061a2d..1abe33fbe529 100644
--- a/arch/powerpc/platforms/ps3/repository.c
+++ b/arch/powerpc/platforms/ps3/repository.c
@@ -73,9 +73,9 @@  static void _dump_node(unsigned int lpar_id, u64 n1, u64 n2, u64 n3, u64 n4,
 
 static u64 make_first_field(const char *text, u64 index)
 {
-	u64 n;
+	u64 n = 0;
 
-	strncpy((char *)&n, text, 8);
+	memcpy((char *)&n, text, strnlen(text, sizeof(n)));
 	return PS3_VENDOR_ID_NONE + (n >> 32) + index;
 }