linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Rasmus Villemoes <linux@rasmusvillemoes.dk>
To: Liu Xiang <liuxiang_1999@126.com>, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] lib: string: reduce unnecessary loop in strncpy
Date: Wed, 30 Oct 2019 15:59:58 +0100	[thread overview]
Message-ID: <c6452618-0161-eefc-0730-b10eac823228@rasmusvillemoes.dk> (raw)
In-Reply-To: <1572444859-3687-1-git-send-email-liuxiang_1999@126.com>

On 30/10/2019 15.14, Liu Xiang wrote:
> 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.

Please read "man strncpy".

There's a reason the loop is written in that somewhat convoluted way:
The behavior of strncpy is mandated by the C standard, and if the src
string is shorter than the destination buffer, the rest must be
0-filled. So if we hit a nul byte before running out of count, we keep
copying that nul byte to the rest of the destination.

Rasmus

  reply	other threads:[~2019-10-30 15:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-30 14:14 [PATCH] lib: string: reduce unnecessary loop in strncpy Liu Xiang
2019-10-30 14:59 ` Rasmus Villemoes [this message]
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=c6452618-0161-eefc-0730-b10eac823228@rasmusvillemoes.dk \
    --to=linux@rasmusvillemoes.dk \
    --cc=linux-kernel@vger.kernel.org \
    --cc=liuxiang_1999@126.com \
    /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;
as well as URLs for NNTP newsgroup(s).