lkml.org 
[lkml]   [2019]   [Oct]   [30]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
From
Subject[PATCH] lib: string: reduce unnecessary loop in strncpy
Date
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
\
 
 \ /
  Last update: 2019-10-30 15:14    [W:0.051 / U:0.204 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site