[PATCH] lib: string: reduce unnecessary loop in strncpy

From: Liu Xiang
Date: Wed Oct 30 2019 - 10:14:33 EST


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@xxxxxxx>
---
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