Messages in this thread Patch in this message | | | From | Liu Xiang <> | Subject | [PATCH] lib: string: reduce unnecessary loop in strncpy | Date | Wed, 30 Oct 2019 22:14:19 +0800 |
| |
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
| |