archive mirror
 help / color / mirror / Atom feed
From: Rasmus Villemoes <>
To: Liu Xiang <>,
Subject: Re: [PATCH] lib: string: reduce unnecessary loop in strncpy
Date: Wed, 30 Oct 2019 15:59:58 +0100	[thread overview]
Message-ID: <> (raw)
In-Reply-To: <>

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.


  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:

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \ \ \ \ \

* 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).