From: Liu Xiang <liuxiang_1999@126.com>
To: linux-kernel@vger.kernel.org
Cc: liuxiang_1999@126.com
Subject: [PATCH] lib: string: reduce unnecessary loop in strncpy
Date: Wed, 30 Oct 2019 22:14:19 +0800 [thread overview]
Message-ID: <1572444859-3687-1-git-send-email-liuxiang_1999@126.com> (raw)
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
next reply other threads:[~2019-10-30 14:14 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-10-30 14:14 Liu Xiang [this message]
2019-10-30 14:59 ` Rasmus Villemoes
2019-10-31 2:29 ` Liu Xiang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1572444859-3687-1-git-send-email-liuxiang_1999@126.com \
--to=liuxiang_1999@126.com \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®