Skip to contentSkip to conversation

man/man3/strncpy.3: Document lack of terminating null byte

PATCH In review Chan Lee 2 participants 2 messages Oct 30 Man-pages: manual pages for Linux -- Sections 2, 3, 4, 5, and 7

lore
Share & tools
lore

Chan Lee

author
#0 lore raw

PATCH 1/1 man/man3/strncpy.3: Document lack of terminating null byte

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.

---
 man/man3/strncpy.3 | 4 ++++
 1 file changed, 4 insertions(+)
man/man3/strncpy.3 +4 -0
index 9c476acb4..89da260fe 100644
@@ -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.
 If the destination buffer,
 limited by its size,
 isn't large enough to hold the copy,
Signature · 1 line
2.50.1 (Apple Git-155)
#1 lore raw

Hi Chan,

Quoted 21 lines

Date: 2026-10-29 22:59:27-0400
From: Chan Lee <[email protected]>

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.

---
 man/man3/strncpy.3 | 4 ++++
 1 file changed, 4 insertions(+)
man/man3/strncpy.3 +0 -0
index 9c476acb4..89da260fe 100644

man/man3/strncpy.3 into the array pointed to by · line 32

Quoted 7 lines
man/man3/strncpy.3 +4 -0
@@ -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

Quoted 7 lines
man/man3/strncpy.3 +0 -0
 If the destination buffer,
 limited by its size,
 isn't large enough to hold the copy,
Signature · 1 line
2.50.1 (Apple Git-155)
Signature · 1 line
<https://www.alejandro-colomar.es>