All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alejandro Colomar <alx+linux-man@kernel.org>
To: Chan Lee <chanlee031012@gmail.com>
Cc: linux-man@vger.kernel.org
Subject: Re: [PATCH] man/man3/strncpy.3: Document lack of terminating null byte
Date: Fri, 30 Oct 2026 14:34:27 +0100	[thread overview]
Message-ID: <auSb0eBwb1W8sDX2@devuan> (raw)
In-Reply-To: <20261030025927.37758-1-chanlee031012@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2582 bytes --]

Hi Chan,

> Date: 2026-10-29 22:59:27-0400
> From: Chan Lee <chanlee031012@gmail.com>
>
> The lack of a terminating null byte is already shown by the examples in
> CAVEATS, but DESCRIPTION does not explain the condition under which it
> happens.  A reader who only reads DESCRIPTION may miss this behavior.
> 
> This patch makes this behavior easier to discover by stating explicitly
> that no terminating null byte is written when the source has dsize or
> more non-null bytes.
> 
> Signed-off-by: Chan Lee <chanlee031012@gmail.com>
> ---
>  man/man3/strncpy.3 | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/man/man3/strncpy.3 b/man/man3/strncpy.3
> index 9c476acb4..89da260fe 100644
> --- a/man/man3/strncpy.3
> +++ b/man/man3/strncpy.3
> @@ -26,6 +26,10 @@ into the array pointed to by
>  .IR dst .
>  If the source has too few non-null bytes to fill the destination,
>  it pads the destination with trailing null bytes.
> +If the source has
> +.I dsize
> +or more non-null bytes,
> +no terminating null byte is written to the destination.

Actually, this function never writes a *terminating* null byte.

It writes padding null bytes in some cases, but those should not be used
for terminating a string.

The wording of the page is written so that users are discouraged from
using this as a function for handling strings at all.

	NAME
	     strncpy - nonstring copy

	...

	DESCRIPTION
	     This  function  copies non‐null characters from the string
	     pointed to by src into the array pointed to  by  dst.   If
	     the source has too few non‐null bytes to fill the destina‐
	     tion,  it  pads  the destination with trailing null bytes.

	...

	CAVEATS
	     The name of this function  is  confusing.   This  function
	     produces  a  null‐padded  character sequence, not a string
	     (see string_copying(7)).  For example:


The lack of a terminating null byte is implicit in the fact that the
page never says it produces a string, and it says it copies non-null
bytes (plus optional padding).  The CAVEATS even explicitly say it
doesn't produce a string.

I think the proposed patch would be negative, in that it would confuse
readers to believe this function is appropriate for producing strings
under some conditions, while it is never appropriate for that.


Have a lovely day!
Alex

>  If the destination buffer,
>  limited by its size,
>  isn't large enough to hold the copy,
> -- 
> 2.50.1 (Apple Git-155)
> 
> 

-- 
<https://www.alejandro-colomar.es>

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

      reply	other threads:[~2026-10-30 13:34 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-30  2:59 [PATCH] man/man3/strncpy.3: Document lack of terminating null byte Chan Lee
2026-10-30 13:34 ` Alejandro Colomar [this message]

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=auSb0eBwb1W8sDX2@devuan \
    --to=alx+linux-man@kernel.org \
    --cc=chanlee031012@gmail.com \
    --cc=linux-man@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.