From: Alejandro Colomar <alx@kernel.org>
To: Paul Eggert <eggert@cs.ucla.edu>
Cc: Steve Summit <scs@eskimo.com>,
bug-gnulib@gnu.org, libc-alpha@sourceware.org,
linux-man@vger.kernel.org
Subject: Re: on the irresponsibility of pursuing C language reform
Date: Sun, 2 Aug 2026 22:31:55 +0200 [thread overview]
Message-ID: <am-eAGL6OWqP9Yah@devuan> (raw)
In-Reply-To: <8715af47-867c-417a-8ef5-7b4b7ceb2c31@cs.ucla.edu>
[-- Attachment #1: Type: text/plain, Size: 9114 bytes --]
Hi Paul,
> Date: 2026-08-02 14:28:33-0500
> From: Paul Eggert <eggert@cs.ucla.edu>
>
> On 8/2/26 09:11, Alejandro Colomar wrote:
> > I do use it in new code still today in shadow-utils. I've heard tar(1)
> > also needs that, and a few other places.
>
> GNU tar proper has not used strncpy since 2018. It generally uses memcpy in
> the places it formerly used strncpy. Tar's 2018 change worked because the
> destinations were already zeroed out, so strncpy's zero-fill semantics were
> unnecessary and indeed a bit slower.
Ok; thanks! In shadow-utils we still need it, though.
> As for strncat, its API is a recipe for confusion
The semantics are okay. strncat(3) is for example useful for
implementing strndupa(3), which is quite useful (just like strndup(3))
if you use substrings or other fixed-width arrays.
#define strndupa(s, n) strncat(strcpy(alloca(n + 1), ""), s, n)
It's weird that there's no cpy version of it, though, but this is solved
with the usual strcpy(p,"") as first argument.
And the name is certainly bad in context, although an appropriate name
would be much more verbose: strcatfrommem(). Maybe we can live with the
name strncat(3).
> and almost nobody
> remembers how it works.
This is true, and in part, it's because of bad teaching. The fact that
GCC's diagnostics recommend bogus usage don't help. I think having it
in <memory.h> could help understand it and remember it.
> It is a poor design, plain and simple.
There's a need for taking a nonstring (i.e., a fixed-width null-padded
buffer, or a substring) and append it to a string. strncat(3) is good
for that use case.
> Although
> strncpy may have a use or two for obsolete non-string data structures that
Substrings are still necessary. If you want to copy the leading part of
a string until a given length, and append it to an existing string, you
need strncat(3). I guess this doesn't violate any guidelines.
For this use, in some sense, it's similar to memccpy(3). You don't use
it all the time, but when you need it, it's useful.
> violate GNU coding guidelines that have been in place since the 1980s,
> strncat has no such redeeming virtues. The current man page for strncat does
> readers a misservice by not saying so clearly.
I disagree that strncat(3) is dead, and will not document it as such.
Moving it to <memory.h> would signal that it's something less
appropriate for usual code using strings.
> In contrast, the glibc manual
> has reasonably decent warnings to users about how bad strncat is
> (strncpy/strlcpy/etc. too).
Let's review that.
5.5 Concatenating Strings
...
Programmers using the strcat or wcscat functions
(or the strlcat, strncat and wcsncat functions
defined in a later section, for that matter)
can easily be recognized as lazy and reckless.
Lazy can be a virtue, IMO. I wouldn't call them reckless. If they know
the prefix string is controlled and small, using the cat functions can
be wise: you don't waste much performance, and instead get a simple
program. Plan9's strecpy(2) is certainly faster, and doesn't add much
complexity, but it still adds a little bit of complexity, so it can
sometimes make sense to keep the source simple, at the expense of a few
cycles.
In almost all situations the
lengths of the participating strings are known
(it better should be
since how can one otherwise ensure
the allocated size of the buffer is sufficient?)
You may know an upper bound without caring about the exact value. Yes,
most of the time you know it, but I wouldn't dismiss those times where
you don't know (or, for simplicity, you don't want to know).
Or at least,
one could know them if one keeps track of
the results of the various function calls.
Indeed, but simplicity might call for not doing this. The compiler can
do it for us.
But then it is very inefficient to use strcat/wcscat.
The compiler can do it for us. Correct simple code is better than fast
code; and correct code can be optimized by compilers. Since, as you
said, the length is easy to find if you keep track of the return values
--and the compiler can keep track of them--, then it's a case for
improving optimizers.
A lot of time is wasted
finding the end of the destination string
so that the actual copying can start.
...
Whenever a programmer feels the need to use strcat
she or he should think twice and look through the program
to see whether the code cannot be rewritten
to take advantage of already calculated results.
As said, give me a better compiler, and I'll give you a faster program.
The related functions strlcat, strncat, wcscat and wcsncat
are almost always unnecessary, too.
Again:
it is almost always unnecessary to use functions like strcat.
Again, I don't agree.
---
5.6 Truncating Strings while Copying
...
Function: char * strncat (char *restrict to, const char *restrict from, size_t size)
strncat(3) does *not* truncate its input. It copies exactly as many
bytes as the source nonstring contains (identified by the pointer and
size), unless the source nonstring is shorter, of course, in which case,
it's not truncation. It doesn't belong in this section. This is part
of the reason why it's misunderstood and misremembered by people: nobody
explained it correctly to them.
...
This function is like strcat except that not more than size
bytes from from are appended to the end of to, and from need
not be null-terminated.
This is pretty much saying that a cat is similar to a fish, except that
the cat doesn't live in the sea, and it is a mammal.
A single null byte is also always
appended to to, so the total allocated size of to must be at
least size + 1 bytes longer than its initial length.
This is good advice, but not precise wording. If the source nonstring
is shorter than its reported size, then the total allocated size need
not be so large. Of course, it's good advice that the size is always
enough for the worst case, but the wording doesn't seem to be clear that
this is only advice, and not a requirement.
The strncat function could be implemented like this:
char *
strncat (char *to, const char *from, size_t size)
{
size_t len = strlen (to);
memcpy (to + len, from, strnlen (from, size));
to[len + strnlen (from, size)] = '\0';
return to;
}
This one might be faster and shorter (thanks to Mark for teaching me
this):
{
if (memccpy(to + strlen(to), from, '\0', size) == NULL)
strcpy(&to[strlen(to) + size], "");
return to;
}
...
As a companion to strncpy, strncat was designed for
now-rarely-used arrays consisting of non-null bytes followed
by zero or more null bytes.
But it also works for substrings. If you want to copy a prefix from
a string into a new string, strncat(3) --or the dup versions,
strndup[a](3)-- help.
However, As noted below, this function is generally a poor
choice for processing strings.
Yes, it's a poor choice for handling strings. By moving it to
<memory.h>, we signal that it's not for handling strings.
Also, this function has significant performance issues.
See Concatenating Strings.
Those are a problem of the optimizer, not of the programmer.
...
Because these functions can abruptly truncate strings or wide
strings, they are generally poor choices for processing them.
When copying or concatening multibyte strings, they can truncate
within a multibyte character so that the result is not a valid
multibyte string. When combining or concatenating multibyte or
wide strings, they may truncate the output after a combining
character, resulting in a corrupted grapheme. They can cause
bugs even when processing single-byte strings: for example, when
calculating an ASCII-only user name, a truncated name can
identify the wrong user.
strncat(3) does not truncate its input, and thus this doesn't tell much
about it. It rather compounds on the historic misunderstanding of the
function.
> To improve the man pages it should be waayyy higher priority
We don't need to prioritize. I have plenty of time to address both
issues. While I wrote string_copying(7) for clarifying what strncat(3)
is and is not (alongside all the other string-copying functions).
I need to revise that page, since I've learnt a lot since I wrote it.
I also need to add at least some paragraph in CAVEATS in strncat(3),
since readers might not find the other page at all --even if it's in
SEE ALSO--.
I'll address all of these.
> to fix their
> poor discussion of these truncation functions
That's a wrong categorization of this function. strncat(3) has nothing
to do with truncation (as said above).
> than to worry about whether
> the man page mentions <string.h> or some other header.
Have a lovely night!
Alex
--
<https://www.alejandro-colomar.es>
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
next prev parent reply other threads:[~2026-08-02 20:31 UTC|newest]
Thread overview: 93+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-31 21:18 [PATCH 0/2] alx-0097r1 - <memory.h>, the legitimate header for memcpy(3) et al Alejandro Colomar
2026-07-31 21:18 ` [PATCH 1/2] man/man3/{mem,strn}*(): SYNOPSIS, STANDARDS: Document these as provided by <memory.h> Alejandro Colomar
2026-07-31 21:23 ` Joseph Myers
2026-07-31 21:28 ` Alejandro Colomar
2026-07-31 21:54 ` Sam James
2026-07-31 22:18 ` Alejandro Colomar
2026-08-01 0:12 ` Alejandro Colomar
2026-08-01 14:43 ` Sam James
2026-07-31 21:51 ` on the irresponsibility of pursuing C language reform (was: [PATCH 1/2] man/man3/{mem,strn}*(): SYNOPSIS, STANDARDS: Document these as provided by <memory.h>) G. Branden Robinson
2026-07-31 21:59 ` on the irresponsibility of pursuing C language reform Sam James
2026-07-31 22:24 ` G. Branden Robinson
2026-07-31 23:19 ` Alejandro Colomar
2026-08-01 14:52 ` Sam James
2026-08-01 12:01 ` Alejandro Colomar
2026-08-01 12:04 ` Alejandro Colomar
2026-08-01 14:38 ` Sam James
2026-08-01 15:15 ` Alejandro Colomar
2026-08-01 16:10 ` Sam James
2026-08-01 17:09 ` Alejandro Colomar
2026-08-01 21:34 ` G. Branden Robinson
2026-08-01 22:22 ` Alejandro Colomar
2026-08-01 22:26 ` Alejandro Colomar
[not found] ` <CAETFuj2OwoyK9J85r2f0RoXbHbXKA4gQJ=JZ-7=QoqGcMwk0+Q@mail.gmail.com>
2026-08-01 22:44 ` Alejandro Colomar
2026-08-01 23:24 ` Alejandro Colomar
2026-08-01 23:53 ` proposed revision to memory.h(3head) (was: on the irresponsibility of pursuing C language reform) G. Branden Robinson
2026-08-02 0:27 ` Alejandro Colomar
2026-08-02 1:03 ` Alejandro Colomar
2026-08-03 0:47 ` proposed revision to memory.h(3head) Alejandro Colomar
2026-08-02 12:52 ` on the irresponsibility of pursuing C language reform Steve Summit
2026-08-02 13:17 ` Alejandro Colomar
2026-08-02 13:45 ` Steve Summit
2026-08-02 14:11 ` Alejandro Colomar
2026-08-02 19:28 ` Paul Eggert
2026-08-02 20:31 ` Alejandro Colomar [this message]
2026-08-01 20:18 ` G. Branden Robinson
2026-08-01 20:42 ` Alejandro Colomar
2026-08-01 20:45 ` Alejandro Colomar
2026-08-01 20:52 ` G. Branden Robinson
2026-08-01 21:12 ` Alejandro Colomar
2026-07-31 22:10 ` on the irresponsibility of pursuing C language reform (was: [PATCH 1/2] man/man3/{mem,strn}*(): SYNOPSIS, STANDARDS: Document these as provided by <memory.h>) Joseph Myers
2026-07-31 22:21 ` Alejandro Colomar
2026-07-31 22:28 ` [PATCH 1/2] man/man3/{mem,strn}*(): SYNOPSIS, STANDARDS: Document these as provided by <memory.h> G. Branden Robinson
2026-07-31 22:42 ` on the irresponsibility of pursuing C language reform (was: [PATCH 1/2] man/man3/{mem,strn}*(): SYNOPSIS, STANDARDS: Document these as provided by <memory.h>) Joseph Myers
2026-07-31 22:52 ` Alejandro Colomar
2026-07-31 23:11 ` Joseph Myers
2026-07-31 23:32 ` G. Branden Robinson
2026-08-01 12:39 ` Alejandro Colomar
2026-08-01 14:26 ` Christopher Bazley
2026-08-01 15:29 ` Alejandro Colomar
2026-07-31 23:45 ` on the irresponsibility of pursuing C language reform (was: " Alejandro Colomar
2026-08-01 12:39 ` Douglas McIlroy
2026-08-01 19:54 ` G. Branden Robinson
2026-08-01 20:35 ` Alejandro Colomar
2026-07-31 23:08 ` [PATCH 1/2] man/man3/{mem,strn}*(): SYNOPSIS, STANDARDS: Document these as provided by <memory.h> G. Branden Robinson
2026-07-31 23:28 ` Joseph Myers
2026-07-31 23:57 ` G. Branden Robinson
2026-08-01 0:06 ` Alejandro Colomar
2026-07-31 22:05 ` Alejandro Colomar
2026-07-31 22:16 ` Joseph Myers
2026-07-31 22:33 ` Alejandro Colomar
2026-07-31 23:48 ` [PATCH 1/2] man/man3/{mem, strn}*(): " Collin Funk
2026-07-31 23:52 ` Alejandro Colomar
2026-08-01 0:01 ` Alejandro Colomar
2026-07-31 21:19 ` [PATCH 2/2] man/man*/{string.3,memory.h.3head}: Move functions to a new page memory.h(3head) Alejandro Colomar
2026-07-31 21:20 ` [PATCH 0/2] alx-0097r1 - <memory.h>, the legitimate header for memcpy(3) et al Alejandro Colomar
2026-08-01 0:25 ` [PATCH v2] man/man3/mem*(): SYNOPSIS: Document non-standard mem*() functions as provided by <memory.h> Alejandro Colomar
2026-08-01 22:22 ` Bruno Haible
2026-08-01 22:38 ` Alejandro Colomar
2026-08-01 22:55 ` Bruno Haible
2026-08-01 23:10 ` Alejandro Colomar
2026-08-01 23:26 ` Collin Funk
2026-08-01 23:34 ` Alejandro Colomar
2026-08-01 23:29 ` Paul Eggert
2026-08-01 23:36 ` Alejandro Colomar
2026-08-02 0:08 ` the Linux man-pages as an educational tool (was: [PATCH v2] man/man3/mem*(): SYNOPSIS: Document non-standard mem*() functions as provided by <memory.h>) G. Branden Robinson
2026-08-02 0:45 ` Alejandro Colomar
2026-08-02 1:04 ` the Linux man-pages as an educational tool Collin Funk
2026-08-02 1:15 ` G. Branden Robinson
2026-08-02 1:15 ` Alejandro Colomar
2026-08-02 1:49 ` Collin Funk
2026-08-02 11:29 ` Alejandro Colomar
2026-08-02 11:47 ` Alejandro Colomar
2026-08-02 12:04 ` Alejandro Colomar
2026-08-02 21:23 ` Maciej W. Rozycki
2026-08-02 21:34 ` Alejandro Colomar
2026-08-02 23:08 ` Arsen Arsenović
2026-08-02 23:10 ` G. Branden Robinson
2026-08-02 23:27 ` Collin Funk
2026-08-02 23:37 ` Alejandro Colomar
2026-08-02 23:41 ` Alejandro Colomar
2026-08-02 23:42 ` Alejandro Colomar
2026-08-03 1:12 ` Alejandro Colomar
2026-08-02 23:30 ` Alejandro Colomar
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=am-eAGL6OWqP9Yah@devuan \
--to=alx@kernel.org \
--cc=bug-gnulib@gnu.org \
--cc=eggert@cs.ucla.edu \
--cc=libc-alpha@sourceware.org \
--cc=linux-man@vger.kernel.org \
--cc=scs@eskimo.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