linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] lib: string: reduce unnecessary loop in strncpy
@ 2019-10-30 14:14 Liu Xiang
  2019-10-30 14:59 ` Rasmus Villemoes
  0 siblings, 1 reply; 3+ messages in thread
From: Liu Xiang @ 2019-10-30 14:14 UTC (permalink / raw)
  To: linux-kernel; +Cc: liuxiang_1999

Now in strncpy, even src[0] is 0, loop will execute count times until
count is 0. It is better to exit the loop immediately when *src is 0.

Signed-off-by: Liu Xiang <liuxiang_1999@126.com>
---
 lib/string.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/lib/string.c b/lib/string.c
index 08ec58c..1065352 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -115,12 +115,8 @@ char *strncpy(char *dest, const char *src, size_t count)
 {
 	char *tmp = dest;
 
-	while (count) {
-		if ((*tmp = *src) != 0)
-			src++;
-		tmp++;
-		count--;
-	}
+	while (count-- && (*tmp++ = *src++) != '\0')
+		; /* nothing */
 	return dest;
 }
 EXPORT_SYMBOL(strncpy);
-- 
1.9.1


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

* Re: [PATCH] lib: string: reduce unnecessary loop in strncpy
  2019-10-30 14:14 [PATCH] lib: string: reduce unnecessary loop in strncpy Liu Xiang
@ 2019-10-30 14:59 ` Rasmus Villemoes
  2019-10-31  2:29   ` Liu Xiang
  0 siblings, 1 reply; 3+ messages in thread
From: Rasmus Villemoes @ 2019-10-30 14:59 UTC (permalink / raw)
  To: Liu Xiang, linux-kernel

On 30/10/2019 15.14, Liu Xiang wrote:
> Now in strncpy, even src[0] is 0, loop will execute count times until
> count is 0. It is better to exit the loop immediately when *src is 0.

Please read "man strncpy".

There's a reason the loop is written in that somewhat convoluted way:
The behavior of strncpy is mandated by the C standard, and if the src
string is shorter than the destination buffer, the rest must be
0-filled. So if we hit a nul byte before running out of count, we keep
copying that nul byte to the rest of the destination.

Rasmus

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

* Re:Re: [PATCH] lib: string: reduce unnecessary loop in strncpy
  2019-10-30 14:59 ` Rasmus Villemoes
@ 2019-10-31  2:29   ` Liu Xiang
  0 siblings, 0 replies; 3+ messages in thread
From: Liu Xiang @ 2019-10-31  2:29 UTC (permalink / raw)
  To: Rasmus Villemoes; +Cc: linux-kernel











At 2019-10-30 22:59:58, "Rasmus Villemoes" <linux@rasmusvillemoes.dk> wrote:
>On 30/10/2019 15.14, Liu Xiang wrote:
>> Now in strncpy, even src[0] is 0, loop will execute count times until
>> count is 0. It is better to exit the loop immediately when *src is 0.
>
>Please read "man strncpy".
>
>There's a reason the loop is written in that somewhat convoluted way:
>The behavior of strncpy is mandated by the C standard, and if the src
>string is shorter than the destination buffer, the rest must be
>0-filled. So if we hit a nul byte before running out of count, we keep
>copying that nul byte to the rest of the destination.
>
>Rasmus

Hi Rasmus,

Thanks for your explanation. I ignored the C library standard.

Regards

Liu Xiang

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

end of thread, other threads:[~2019-10-31  2:29 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-30 14:14 [PATCH] lib: string: reduce unnecessary loop in strncpy Liu Xiang
2019-10-30 14:59 ` Rasmus Villemoes
2019-10-31  2:29   ` Liu Xiang

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).