linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] test_hexdump: Replace strncpy with memcpy
@ 2018-07-01 20:57 Guenter Roeck
  2018-07-04 21:23 ` Andy Shevchenko
  0 siblings, 1 reply; 2+ messages in thread
From: Guenter Roeck @ 2018-07-01 20:57 UTC (permalink / raw)
  To: linux-kernel; +Cc: Guenter Roeck

gcc 8.1.0 complains:

lib/test_hexdump.c:84:3: warning:
	'strncpy' output truncated before terminating
	nul copying as many bytes from a string as its length
lib/test_hexdump.c:82:19: note: length computed here

Using strncpy() is indeed less than perfect since the length of data to
be copied has already been determined with strlen(). Replace strncpy()
with memcpy() to address the warning and optimize the code a little.

Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
 lib/test_hexdump.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/test_hexdump.c b/lib/test_hexdump.c
index 3f415d8101f3..1c3c513add77 100644
--- a/lib/test_hexdump.c
+++ b/lib/test_hexdump.c
@@ -81,7 +81,7 @@ static void __init test_hexdump_prepare_test(size_t len, int rowsize,
 		const char *q = *result++;
 		size_t amount = strlen(q);
 
-		strncpy(p, q, amount);
+		memcpy(p, q, amount);
 		p += amount;
 
 		*p++ = ' ';
-- 
2.7.4


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] test_hexdump: Replace strncpy with memcpy
  2018-07-01 20:57 [PATCH] test_hexdump: Replace strncpy with memcpy Guenter Roeck
@ 2018-07-04 21:23 ` Andy Shevchenko
  0 siblings, 0 replies; 2+ messages in thread
From: Andy Shevchenko @ 2018-07-04 21:23 UTC (permalink / raw)
  To: Guenter Roeck; +Cc: Linux Kernel Mailing List

On Sun, Jul 1, 2018 at 11:57 PM, Guenter Roeck <linux@roeck-us.net> wrote:
> gcc 8.1.0 complains:
>
> lib/test_hexdump.c:84:3: warning:
>         'strncpy' output truncated before terminating
>         nul copying as many bytes from a string as its length
> lib/test_hexdump.c:82:19: note: length computed here
>
> Using strncpy() is indeed less than perfect since the length of data to
> be copied has already been determined with strlen(). Replace strncpy()
> with memcpy() to address the warning and optimize the code a little.
>

If it helps for some reason I'm not objecting to switch to memcpy().

Acked-by: Andy Shevchenko <andy.shevchenko@gmail.com>

> Signed-off-by: Guenter Roeck <linux@roeck-us.net>
> ---
>  lib/test_hexdump.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/lib/test_hexdump.c b/lib/test_hexdump.c
> index 3f415d8101f3..1c3c513add77 100644
> --- a/lib/test_hexdump.c
> +++ b/lib/test_hexdump.c
> @@ -81,7 +81,7 @@ static void __init test_hexdump_prepare_test(size_t len, int rowsize,
>                 const char *q = *result++;
>                 size_t amount = strlen(q);
>
> -               strncpy(p, q, amount);
> +               memcpy(p, q, amount);
>                 p += amount;
>
>                 *p++ = ' ';
> --
> 2.7.4
>



-- 
With Best Regards,
Andy Shevchenko

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-07-04 21:23 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-01 20:57 [PATCH] test_hexdump: Replace strncpy with memcpy Guenter Roeck
2018-07-04 21:23 ` Andy Shevchenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).