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: Mon, 3 Aug 2026 13:39:48 +0200 [thread overview]
Message-ID: <anB4niGGaJANJtGf@devuan> (raw)
In-Reply-To: <c4d1d3f9-0c47-45aa-afa4-66dff49c1188@cs.ucla.edu>
[-- Attachment #1: Type: text/plain, Size: 4987 bytes --]
Hi Paul,
> Date: 2026-08-02 22:28:00-0500
> From: Paul Eggert <eggert@cs.ucla.edu>
>
> On 8/2/26 15:31, Alejandro Colomar wrote:
> > 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)
>
> That's a bad implementation of strndupa for several reasons. (Some reasons
> are: it evaluates n multiple times,
That can be solved with a local variable. I didn't worry about it,
because I don't pass n++ to this thing.
> it overallocates stack space when
> strnlen (s, n) < n,
True; I agree that for glibc it makes sense to implement it with
memcpy(3). However, for code that's not in a public library --and thus
can afford to be wasteful--, and must implement strndupa(3) to
workaround the fact that musl doesn't provide it, then I think the
simplicity of the strncat(3)-using implementation is worth it.
> it obviously has undefined behavior when n == SIZE_MAX,
That's also true of n==456789. Anything not trivially small is already
UB with alloca(3).
> and it less obviously has undefined behavior because it uses alloca as the
> argument of a function call.)
Is this still an issue? I thought we were past that limitation, but
I was actually concerned when I wrote that code.
The manual page does warn about this:
On many systems alloca() cannot be used inside the list of
arguments of a function call, because the stack space re‐
served by alloca() would appear on the stack in the middle
of the space for the function arguments.
But since it happens to work in all modern systems I've used, I thought
this was probably a thing of the past. It would be good to be more
specific about this. If you know which systems are affected, it'd be
interesting if you could send a patch for the alloca(3) manual page.
> Fixing its problems makes it obvious that
> strndupa should not be implemented via strncat; it's much saner to use
> memcpy. Which is why glibc does it that way.
Actually, I've just realized that the few uses of strndupa(3) that we
had in shadow-utils disappeared when we removed the logoutd(8) program
in the latest release; thus, we can get rid of our strndupa(3)
implementation. We still use strndup(3), though.
We still have one direct use of strncat(3) which I've been wondering
whether we can get rid of, but every alternative I try seems to be worse
than strncat(3).
/*
* is_my_tty -- determine if "tty" is the same TTY stdin is using
*/
static bool
is_my_tty(const char tty[UTX_LINESIZE])
{
char full_tty[STRLEN("/dev/") + UTX_LINESIZE + 1];
char my_tty[countof(full_tty)];
stpcpy(full_tty, "");
if (tty[0] != '/')
strcpy (full_tty, "/dev/");
strncat(full_tty, tty, UTX_LINESIZE);
if (ttyname_ra(STDIN_FILENO, my_tty) != 0) {
(void) puts (_("Unable to determine your tty name."));
exit (EXIT_FAILURE);
}
return streq(full_tty, my_tty);
}
If you think this could be improved with something else, it'd be
interesting to hear some advice.
> As for the glibc manual's wording in this area, I readily concede that the
> the wording should be toned down (calling programmers "lazy" is just
> counterproductive),
Thanks!
> but its technical aspects are pretty much on target.
> Although one might valiantly argue that strncat etc. are about substrings,
> that's not their original design, that's not what they're good at, and
> that's not what they're mostly used for. What they're designed for, and what
> they're good at, and what they're mostly used for is arbitrary truncation of
> strings and string-like data,
I'm not so sure about the correctness of this claim. I'd be interested
in learning more about its design, if anyone here knows about it.
Here's what I can read in the V7 sources:
alx@devuan:~/src/unix/unix/v7$ grep -rn 'strncat('
usr/src/cmd/dumpdir.c:153: strncat(prefix, dir.d_name, sizeof(dir.d_name));
usr/src/cmd/login.c:107: strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
usr/src/libc/gen/strncat.c:8:strncat(s1, s2, n)
usr/man/man3/string.3:9:.B char *strncat(s1, s2, n)
There are exactly two calls in V7.
The dumpdir.c call is a perfect example of copying a nonstring
(a fixed-size null-padded character array, in this case) into a string
*without* truncation.
The login.c call indeed seems to be a use for truncation.
These are two very different uses, and it'd be interesting which one
came first, and why the other one was used later.
> something that goes against the GNU
> programming guidelines, and something that programmers should be warned
> against.
I agree. I don't think the current documentation does warn against that
use correctly, though.
Have a lovely day!
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-03 11:39 UTC|newest]
Thread overview: 134+ 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
2026-08-03 13:42 ` Joseph Myers
2026-08-03 14:22 ` Alejandro Colomar
2026-08-03 14:38 ` on glibc forking/reclaiming its man pages (was: on the irresponsibility of pursuing C language reform) G. Branden Robinson
2026-08-03 14:44 ` Joseph Myers
2026-08-03 15:18 ` G. Branden Robinson
[not found] ` <CAETFuj2OwoyK9J85r2f0RoXbHbXKA4gQJ=JZ-7=QoqGcMwk0+Q@mail.gmail.com>
2026-08-01 22:44 ` on the irresponsibility of pursuing C language reform 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-03 17:58 ` Mark Harris
2026-08-03 18:47 ` Alejandro Colomar
2026-08-03 20:14 ` Mark Harris
2026-08-03 23:36 ` Alejandro Colomar
2026-08-04 3:25 ` Mark Harris
2026-08-03 14:10 ` proposed revision to memory.h(3head) (was: on the irresponsibility of pursuing C language reform) Joseph Myers
2026-08-03 14:31 ` 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
2026-08-03 3:28 ` Paul Eggert
2026-08-03 11:39 ` Alejandro Colomar [this message]
2026-08-03 13:23 ` Joseph Myers
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-08-04 2:35 ` Thorsten Glaser
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-03 2:09 ` G. Branden Robinson
2026-08-03 12:21 ` Alejandro Colomar
2026-08-03 14:05 ` Sam James
2026-08-03 14:40 ` Alejandro Colomar
2026-08-03 15:34 ` G. Branden Robinson
2026-08-03 16:11 ` Alejandro Colomar
2026-08-03 16:15 ` Sam James
2026-08-03 16:43 ` Alejandro Colomar
2026-08-03 10:28 ` the Linux man-pages as an educational tool... and a bit about C Αγαθοκλής Χατζημανίκας
2026-08-03 14:03 ` the Linux man-pages as an educational tool Sam James
2026-08-03 14:28 ` Alejandro Colomar
2026-08-04 2:39 ` Collin Funk
2026-08-03 16:09 ` on project management (was: the Linux man-pages as an educational tool) G. Branden Robinson
2026-08-03 19:46 ` enh
2026-08-03 21:09 ` on project management Arsen Arsenović
2026-08-02 23:30 ` the Linux man-pages as an educational tool Alejandro Colomar
2026-08-03 11:00 ` Arsen Arsenović
2026-08-03 16:07 ` Jeffrey Walton
2026-08-03 16:17 ` Alejandro Colomar
2026-08-03 19:31 ` Joseph Myers
2026-08-03 19:47 ` Alejandro Colomar
2026-08-03 13:51 ` When and why realloc(,0) was broken in glibc in 1999 Alejandro Colomar
2026-08-03 12:35 ` the Linux man-pages as an educational tool Bruno Haible
2026-08-03 13:07 ` Alejandro Colomar
2026-08-03 19:45 ` Joseph Myers
2026-08-03 19:50 ` 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=anB4niGGaJANJtGf@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