[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-Id: <1572444859-3687-1-git-send-email-liuxiang_1999@126.com>
Date: Wed, 30 Oct 2019 22:14:19 +0800
From: Liu Xiang <liuxiang_1999@....com>
To: linux-kernel@...r.kernel.org
Cc: liuxiang_1999@....com
Subject: [PATCH] lib: string: reduce unnecessary loop in strncpy
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@....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
Powered by blists - more mailing lists