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

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