Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

After a number of decimal places, nobody gives a damn.


devel / comp.lang.c / Variadic functions

SubjectAuthor
* Variadic functionsMalcolm McLean
+- Re: Variadic functionsKaz Kylheku
+- Re: Variadic functionsKeith Thompson
+- Re: Variadic functionsLawrence D'Oliveiro
+- Re: Variadic functionsJames Kuyper
+* Re: Variadic functionsJanis Papanagnou
|`* Re: Variadic functionsMalcolm McLean
| +* Re: Variadic functionsSpiros Bousbouras
| |+* Re: Variadic functionsMalcolm McLean
| ||`- Re: Variadic functionsRichard Harnden
| |`* Re: Variadic functionsBlue-Maned_Hawk
| | `* Re: Variadic functionsLawrence D'Oliveiro
| |  +* Re: Variadic functionsMalcolm McLean
| |  |+- Re: Variadic functionsDavid Brown
| |  |`* Re: Variadic functionsScott Lurndal
| |  | +* Re: Variadic functionsLawrence D'Oliveiro
| |  | |`* Re: Variadic functionsDavid Brown
| |  | | +* Re: Variadic functionsJanis Papanagnou
| |  | | |`- Re: Variadic functionsDavid Brown
| |  | | +* Re: Variadic functionsLawrence D'Oliveiro
| |  | | |`* Re: Variadic functionsDavid Brown
| |  | | | `* Re: Variadic functionsLawrence D'Oliveiro
| |  | | |  `* Re: Variadic functionsKenny McCormack
| |  | | |   +- Re: Variadic functionsKaz Kylheku
| |  | | |   `- Re: Variadic functionsLawrence D'Oliveiro
| |  | | `* Re: Variadic functionsMalcolm McLean
| |  | |  +- Re: Variadic functionsKaz Kylheku
| |  | |  +- Re: Variadic functionsJames Kuyper
| |  | |  +- Re: Variadic functionsDavid Brown
| |  | |  `* Re: Variadic functionsKeith Thompson
| |  | |   `* Re: Variadic functionsMalcolm McLean
| |  | |    +* Re: Variadic functionsScott Lurndal
| |  | |    |+- Re: strings (was Re: Variadic functions)Lawrence D'Oliveiro
| |  | |    |`- Re: Variadic functionsDavid Brown
| |  | |    +* Re: Variadic functionsKaz Kylheku
| |  | |    |`- Re: Variadic functionsMalcolm McLean
| |  | |    `* Re: Variadic functionsDavid Brown
| |  | |     `* Re: Variadic functionsMalcolm McLean
| |  | |      `* Re: Variadic functionsDavid Brown
| |  | |       `* Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)Lawrence D'Oliveiro
| |  | |        +- Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)Chris M. Thomasson
| |  | |        +* Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)bart
| |  | |        |`* Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)Lawrence D'Oliveiro
| |  | |        | +* Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)bart
| |  | |        | |`- Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)Lawrence D'Oliveiro
| |  | |        | `* Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)Tim Rentsch
| |  | |        |  `* Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)Lawrence D'Oliveiro
| |  | |        |   +- Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)Kaz Kylheku
| |  | |        |   `* Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)Kenny McCormack
| |  | |        |    +- Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)Kaz Kylheku
| |  | |        |    `- Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)Lawrence D'Oliveiro
| |  | |        +- Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)Kenny McCormack
| |  | |        +* Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)Keith Thompson
| |  | |        |`* Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)Lawrence D'Oliveiro
| |  | |        | `* Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)Keith Thompson
| |  | |        |  +- Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)Lawrence D'Oliveiro
| |  | |        |  `* Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)jak
| |  | |        |   `- Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)David Brown
| |  | |        +- Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)David Brown
| |  | |        `* Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)Malcolm McLean
| |  | |         `* Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)Chris M. Thomasson
| |  | |          `* Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)David Brown
| |  | |           `- Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)Chris M. Thomasson
| |  | `* Re: Variadic functionsBlue-Maned_Hawk
| |  |  `* Re: Variadic functionsLawrence D'Oliveiro
| |  |   `* Re: Variadic functionsDavid Brown
| |  |    +* Re: Variadic functionsKaz Kylheku
| |  |    |+- Re: Variadic functionsScott Lurndal
| |  |    |+* Re: Variadic functionsKeith Thompson
| |  |    ||`* Re: Variadic functionsDavid Brown
| |  |    || `- Re: Variadic functionsKeith Thompson
| |  |    |+- Re: Variadic functionsJames Kuyper
| |  |    |`* Re: Variadic functionsDavid Brown
| |  |    | `* Re: Variadic functionsKeith Thompson
| |  |    |  `- Re: Variadic functionsDavid Brown
| |  |    +* Re: Variadic functionsLawrence D'Oliveiro
| |  |    |`- Re: Variadic functionsDavid Brown
| |  |    `* Re: Variadic functionsBlue-Maned_Hawk
| |  |     `- Re: Variadic functionsDavid Brown
| |  `- Re: Variadic functionsBlue-Maned_Hawk
| +* Re: Variadic functionsLawrence D'Oliveiro
| |`* Re: Variadic functionsMalcolm McLean
| | +- Re: Variadic functionsLawrence D'Oliveiro
| | +* Re: Variadic functionsbart
| | |+* Re: Variadic functionsMalcolm McLean
| | ||`* Re: Variadic functionsDavid Brown
| | || `* Re: Variadic functionsMalcolm McLean
| | ||  `* Re: Variadic functionsDavid Brown
| | ||   `* Re: Variadic functionsMalcolm McLean
| | ||    +* Re: Variadic functionsDavid Brown
| | ||    |`* Re: Variadic functionsMalcolm McLean
| | ||    | `* Re: Variadic functionsDavid Brown
| | ||    |  `* Re: Variadic functionsMalcolm McLean
| | ||    |   `- Re: Variadic functionsDavid Brown
| | ||    `* Re: Variadic functionsKaz Kylheku
| | ||     `* Re: Variadic functionsMalcolm McLean
| | ||      `- Re: Variadic functionsKaz Kylheku
| | |`- Re: Variadic functionsBlue-Maned_Hawk
| | +- Re: Variadic functionsKeith Thompson
| | `* Re: Variadic functionsLawrence D'Oliveiro
| |  `- Re: Variadic functionsKaz Kylheku
| `* Re: Variadic functionsLawrence D'Oliveiro
+* Re: Variadic functionsTim Rentsch
+- Re: Variadic functionsBlue-Maned_Hawk
`- Re: Variadic functionsChris M. Thomasson

Pages:12345
Re: Variadic functions

<uoqie3$1o25r$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=32969&group=comp.lang.c#32969

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.brown@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Variadic functions
Date: Wed, 24 Jan 2024 09:40:34 +0100
Organization: A noiseless patient Spider
Lines: 44
Message-ID: <uoqie3$1o25r$1@dont-email.me>
References: <uoed9m$387sh$1@dont-email.me> <uog6fs$3klco$1@dont-email.me>
<uoga8t$3l6mu$1@dont-email.me> <Kdccf9Gr26AQrYzxL@bongo-ra.co>
<pan$b84f3$321a3e4$17747387$71cad685@invalid.invalid>
<uomu5q$uj61$2@dont-email.me> <uonteh$16sb4$2@dont-email.me>
<xVQrN.249990$Wp_8.4423@fx17.iad> <uopc7g$1f17i$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 24 Jan 2024 08:40:35 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="65507ed2e59e1ed5857440a8f8a39c64";
logging-data="1837243"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18DGI3asICol5/LJCQQb00086qwTt4e73U="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:JhejNDLT6t5/lCRfSYTtthJrPPA=
In-Reply-To: <uopc7g$1f17i$2@dont-email.me>
Content-Language: en-GB
 by: David Brown - Wed, 24 Jan 2024 08:40 UTC

On 23/01/2024 22:48, Lawrence D'Oliveiro wrote:
> On Tue, 23 Jan 2024 15:27:25 GMT, Scott Lurndal wrote:
>
>> Huge security hole.
>
> That was the point I was trying to make, but it seemed to go over
> somebody’s head ...

If that "somebody" was me, then the it did not go over my head - it went
far to the side of it.

Chained strcat has no more and no less of a risk of buffer overflows
than any other way of combining strings without checking their size and
ensuring you have an appropriately sized destination buffer. There is
no more and no less danger in doing "strcat(strcat(strcat(..." than in
doing "strcat" at all - either you have ensured your buffers and sizes
are safe and that all your pointers really are terminated strings, or
you have a risk of overrun.

And a risk of overrun is not in any way synonymous with "huge security
hole". It is a risk of failure. How big that risk is depends on the
likelihood of the strings being too big for the target buffer, which is
totally impossible to estimate without real code - "huge" is no more
appropriate than "tiny". Similarly, the consequences of an overrun are
impossible to judge without much more information - thus "security hole"
is no more appropriate than guessing any other kind of failure. "Huge
security hole" and "tiny risk of pixel colour glitches" are equally
valid, due to a total lack of knowledge of the rest of the program.

So when someone has used "strcat" in a Usenet post, I would assume that
real-world code would take appropriate care to be sure the arguments
point to valid strings, and the destinations have enough space. It's
not unlikely that the real code actually uses "strncat" to reduce the
risk of failures.

And I assumed that you, too, know all this and considered it a side
issue, orthogonal to the actual interesting point about nested strcat
calls. That is, concatenating strings with nested strcat calls scales
as O(n²), while alternatives such as "sprintf" or nested "stpcpy" (POSIX
but not standard C) scales O(n).

But you might not have known that gcc will convert a set of nested
"strcat" calls into a call to "strlen" followed by calls to "stpcpy".

Re: Variadic functions

<uoqj5b$1o34g$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=32971&group=comp.lang.c#32971

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.brown@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Variadic functions
Date: Wed, 24 Jan 2024 09:52:59 +0100
Organization: A noiseless patient Spider
Lines: 17
Message-ID: <uoqj5b$1o34g$1@dont-email.me>
References: <uoed9m$387sh$1@dont-email.me> <uog6fs$3klco$1@dont-email.me>
<uoga8t$3l6mu$1@dont-email.me> <Kdccf9Gr26AQrYzxL@bongo-ra.co>
<pan$b84f3$321a3e4$17747387$71cad685@invalid.invalid>
<uomu5q$uj61$2@dont-email.me> <uonteh$16sb4$2@dont-email.me>
<xVQrN.249990$Wp_8.4423@fx17.iad>
<pan$514f$ac2be24d$a46eca3d$5aa9624@invalid.invalid>
<uopi9d$1fs5l$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 24 Jan 2024 08:52:59 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="65507ed2e59e1ed5857440a8f8a39c64";
logging-data="1838224"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+kl4wQluKKP14Wc5Ngd8rNGeKlzW0r24I="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:PIiSJxx3U6EayyRp+WzxsVRhmJk=
In-Reply-To: <uopi9d$1fs5l$2@dont-email.me>
Content-Language: en-GB
 by: David Brown - Wed, 24 Jan 2024 08:52 UTC

On 24/01/2024 00:31, Lawrence D'Oliveiro wrote:
> On Tue, 23 Jan 2024 22:28:03 -0000 (UTC), Blue-Maned_Hawk wrote:
>
>> strncat
>
> If you can figure out how to use it properly.

strncat is nasty, as "n" is the maximum number of characters to copy,
not the size of the destination buffer. strncpy is nasty as it wastes
effort needlessly writing null characters once the string is copied, and
does not guarantee a terminating null.

The common standard C str* functions are, IMHO, extraordinarily poorly
specified, and could easily have been very much easier to use correctly
and efficiently.

Re: Variadic functions

<uor193$1qf4t$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=32980&group=comp.lang.c#32980

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_papanagnou+ng@hotmail.com (Janis Papanagnou)
Newsgroups: comp.lang.c
Subject: Re: Variadic functions
Date: Wed, 24 Jan 2024 13:53:54 +0100
Organization: A noiseless patient Spider
Lines: 16
Message-ID: <uor193$1qf4t$1@dont-email.me>
References: <uoed9m$387sh$1@dont-email.me> <uog6fs$3klco$1@dont-email.me>
<uoga8t$3l6mu$1@dont-email.me> <Kdccf9Gr26AQrYzxL@bongo-ra.co>
<pan$b84f3$321a3e4$17747387$71cad685@invalid.invalid>
<uomu5q$uj61$2@dont-email.me> <uonteh$16sb4$2@dont-email.me>
<xVQrN.249990$Wp_8.4423@fx17.iad> <uopc7g$1f17i$2@dont-email.me>
<uoqie3$1o25r$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 24 Jan 2024 12:53:55 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="5cf70dd9151d44e9525f2aa8d96d1ce0";
logging-data="1916061"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+b2pbQ1uO9s1MogKSf+eIg"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:qJyPMOJ0aVbZgoqkcJph1LYNrzY=
In-Reply-To: <uoqie3$1o25r$1@dont-email.me>
 by: Janis Papanagnou - Wed, 24 Jan 2024 12:53 UTC

On 24.01.2024 09:40, David Brown wrote:
>
> [...] That is, concatenating strings with nested strcat calls scales
> as O(n²), while alternatives such as "sprintf" or nested "stpcpy" (POSIX
> but not standard C) scales O(n).

First I mistook "stpcpy" for a typo, but hey, it's there. - Nice, and
good to know it's there. Thanks.

> But you might not have known that gcc will convert a set of nested
> "strcat" calls into a call to "strlen" followed by calls to "stpcpy".

Interesting.

Janis

Re: Variadic functions

<uor4uu$1r10t$2@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=32986&group=comp.lang.c#32986

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.brown@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Variadic functions
Date: Wed, 24 Jan 2024 14:56:46 +0100
Organization: A noiseless patient Spider
Lines: 24
Message-ID: <uor4uu$1r10t$2@dont-email.me>
References: <uoed9m$387sh$1@dont-email.me> <uog6fs$3klco$1@dont-email.me>
<uoga8t$3l6mu$1@dont-email.me> <Kdccf9Gr26AQrYzxL@bongo-ra.co>
<pan$b84f3$321a3e4$17747387$71cad685@invalid.invalid>
<uomu5q$uj61$2@dont-email.me> <uonteh$16sb4$2@dont-email.me>
<xVQrN.249990$Wp_8.4423@fx17.iad> <uopc7g$1f17i$2@dont-email.me>
<uoqie3$1o25r$1@dont-email.me> <uor193$1qf4t$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 24 Jan 2024 13:56:46 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="65507ed2e59e1ed5857440a8f8a39c64";
logging-data="1934365"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19VIBXOVsByHoac5MbepNScY8DIihqpFUU="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:tAOBzV5eq7r8E72xhhtS/QepXzE=
In-Reply-To: <uor193$1qf4t$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Wed, 24 Jan 2024 13:56 UTC

On 24/01/2024 13:53, Janis Papanagnou wrote:
> On 24.01.2024 09:40, David Brown wrote:
>>
>> [...] That is, concatenating strings with nested strcat calls scales
>> as O(n²), while alternatives such as "sprintf" or nested "stpcpy" (POSIX
>> but not standard C) scales O(n).
>
> First I mistook "stpcpy" for a typo, but hey, it's there. - Nice, and
> good to know it's there. Thanks.

Note that it exists in POSIX (and therefore in most C libraries), but it
is not standard C.

>
>> But you might not have known that gcc will convert a set of nested
>> "strcat" calls into a call to "strlen" followed by calls to "stpcpy".
>
> Interesting.
>

gcc (and clang, and no doubt other compilers) can optimise a number of
standard library calls. This is, IME, very useful.

Re: Variadic functions

<20240124113737.421@kylheku.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=33021&group=comp.lang.c#33021

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.swapon.de!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 433-929-6894@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: Variadic functions
Date: Wed, 24 Jan 2024 19:50:27 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 56
Message-ID: <20240124113737.421@kylheku.com>
References: <uoed9m$387sh$1@dont-email.me> <uog6fs$3klco$1@dont-email.me>
<uoga8t$3l6mu$1@dont-email.me> <Kdccf9Gr26AQrYzxL@bongo-ra.co>
<pan$b84f3$321a3e4$17747387$71cad685@invalid.invalid>
<uomu5q$uj61$2@dont-email.me> <uonteh$16sb4$2@dont-email.me>
<xVQrN.249990$Wp_8.4423@fx17.iad>
<pan$514f$ac2be24d$a46eca3d$5aa9624@invalid.invalid>
<uopi9d$1fs5l$2@dont-email.me> <uoqj5b$1o34g$1@dont-email.me>
Injection-Date: Wed, 24 Jan 2024 19:50:27 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b53ada80e44a49509d1cc2e54e523b05";
logging-data="2048362"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/wJtArfY3yEXe+Z97E8tpqNtgnsjw4Sig="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:3733KM1+optsNNRhpnzWssKqT5M=
 by: Kaz Kylheku - Wed, 24 Jan 2024 19:50 UTC

On 2024-01-24, David Brown <david.brown@hesbynett.no> wrote:
> On 24/01/2024 00:31, Lawrence D'Oliveiro wrote:
>> On Tue, 23 Jan 2024 22:28:03 -0000 (UTC), Blue-Maned_Hawk wrote:
>>
>>> strncat
>>
>> If you can figure out how to use it properly.
>
> strncat is nasty, as "n" is the maximum number of characters to copy,
> not the size of the destination buffer. strncpy is nasty as it wastes
> effort needlessly writing null characters once the string is copied, and
> does not guarantee a terminating null.

I always assumed strncpy was intended for the following kind of
situation: placing a string into a non-null-terminated array intended
for data communication or storage.

struct rec {
char first_name[15]; // null padded, not terminated
char last_name[15]; // likewise
// ...
};

Internally, you work with these as null terminated strings,
copying them out to [16] buffers.

These get written to disk, so the data has to be cleanly
padded with nulls, even if a single null is enough to
terminate short fields. You don't want random garbage
from memory written out; it could be a security problem.

strncpy helps to prepare the fields:

char fn[16]; // null terminated
/* ... */
strncpy(rec->first_name, fn, 15); // correct in all cases

as well as in the other direction

strncpy(fn, rec->first_name, 15); // could use strcpy

I'm using hard-coded constants for simplicity.

If fn was initialized to zeros, and always used correctly to
store a null-terminated string, there is nothing to do;
a null is guaranteed to always be present at fn[15].
Otherwise we need fn[15] = 0.

So I think that's the nice use case for strncpy; unfortunately, over
most of its life, it got clumsily used for other use cases.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: Variadic functions

<%VdsN.304854$xHn7.54629@fx14.iad>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=33024&group=comp.lang.c#33024

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!feeder8.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!peer02.ams1!peer.ams1.xlned.com!news.xlned.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx14.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: scott@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: Variadic functions
Newsgroups: comp.lang.c
References: <uoed9m$387sh$1@dont-email.me> <uog6fs$3klco$1@dont-email.me> <uoga8t$3l6mu$1@dont-email.me> <Kdccf9Gr26AQrYzxL@bongo-ra.co> <pan$b84f3$321a3e4$17747387$71cad685@invalid.invalid> <uomu5q$uj61$2@dont-email.me> <uonteh$16sb4$2@dont-email.me> <xVQrN.249990$Wp_8.4423@fx17.iad> <pan$514f$ac2be24d$a46eca3d$5aa9624@invalid.invalid> <uopi9d$1fs5l$2@dont-email.me> <uoqj5b$1o34g$1@dont-email.me> <20240124113737.421@kylheku.com>
Lines: 42
Message-ID: <%VdsN.304854$xHn7.54629@fx14.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Wed, 24 Jan 2024 19:54:35 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Wed, 24 Jan 2024 19:54:35 GMT
X-Received-Bytes: 2565
 by: Scott Lurndal - Wed, 24 Jan 2024 19:54 UTC

Kaz Kylheku <433-929-6894@kylheku.com> writes:
>On 2024-01-24, David Brown <david.brown@hesbynett.no> wrote:
>> On 24/01/2024 00:31, Lawrence D'Oliveiro wrote:
>>> On Tue, 23 Jan 2024 22:28:03 -0000 (UTC), Blue-Maned_Hawk wrote:
>>>
>>>> strncat
>>>
>>> If you can figure out how to use it properly.
>>
>> strncat is nasty, as "n" is the maximum number of characters to copy,
>> not the size of the destination buffer. strncpy is nasty as it wastes
>> effort needlessly writing null characters once the string is copied, and
>> does not guarantee a terminating null.
>
>I always assumed strncpy was intended for the following kind of
>situation: placing a string into a non-null-terminated array intended
>for data communication or storage.
>
>struct rec {
> char first_name[15]; // null padded, not terminated
> char last_name[15]; // likewise
> // ...
>};
>
>Internally, you work with these as null terminated strings,
>copying them out to [16] buffers.
>
>These get written to disk, so the data has to be cleanly
>padded with nulls, even if a single null is enough to
>terminate short fields. You don't want random garbage
>from memory written out; it could be a security problem.
>
>strncpy helps to prepare the fields:
>
> char fn[16]; // null terminated
> /* ... */
> strncpy(rec->first_name, fn, 15); // correct in all cases

snprintf(fn, sizeof(fn), "%s", rec->first_name) is also correct in
all cases (and can tell you if it would have overflowed, if you're
interested).

Re: Variadic functions

<87mssu5w0u.fsf@nosuchdomain.example.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=33030&group=comp.lang.c#33030

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S.Thompson+u@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: Variadic functions
Date: Wed, 24 Jan 2024 12:14:25 -0800
Organization: None to speak of
Lines: 66
Message-ID: <87mssu5w0u.fsf@nosuchdomain.example.com>
References: <uoed9m$387sh$1@dont-email.me> <uog6fs$3klco$1@dont-email.me>
<uoga8t$3l6mu$1@dont-email.me> <Kdccf9Gr26AQrYzxL@bongo-ra.co>
<pan$b84f3$321a3e4$17747387$71cad685@invalid.invalid>
<uomu5q$uj61$2@dont-email.me> <uonteh$16sb4$2@dont-email.me>
<xVQrN.249990$Wp_8.4423@fx17.iad>
<pan$514f$ac2be24d$a46eca3d$5aa9624@invalid.invalid>
<uopi9d$1fs5l$2@dont-email.me> <uoqj5b$1o34g$1@dont-email.me>
<20240124113737.421@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="1a14796772ccb3100f0d224a23edc517";
logging-data="2054292"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+5Tsf+Tv+Ls1avcEJvGGnI"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:5eVTsP7qR97VYaCQ5Jsf+DNdHxg=
sha1:FV6LJeMCEL5BxYO5QMo9oUSiWy8=
 by: Keith Thompson - Wed, 24 Jan 2024 20:14 UTC

Kaz Kylheku <433-929-6894@kylheku.com> writes:
> On 2024-01-24, David Brown <david.brown@hesbynett.no> wrote:
>> On 24/01/2024 00:31, Lawrence D'Oliveiro wrote:
>>> On Tue, 23 Jan 2024 22:28:03 -0000 (UTC), Blue-Maned_Hawk wrote:
>>>
>>>> strncat
>>>
>>> If you can figure out how to use it properly.
>>
>> strncat is nasty, as "n" is the maximum number of characters to copy,
>> not the size of the destination buffer. strncpy is nasty as it wastes
>> effort needlessly writing null characters once the string is copied, and
>> does not guarantee a terminating null.
>
> I always assumed strncpy was intended for the following kind of
> situation: placing a string into a non-null-terminated array intended
> for data communication or storage.
>
> struct rec {
> char first_name[15]; // null padded, not terminated
> char last_name[15]; // likewise
> // ...
> };
>
> Internally, you work with these as null terminated strings,
> copying them out to [16] buffers.
>
> These get written to disk, so the data has to be cleanly
> padded with nulls, even if a single null is enough to
> terminate short fields. You don't want random garbage
> from memory written out; it could be a security problem.
>
> strncpy helps to prepare the fields:
>
> char fn[16]; // null terminated
> /* ... */
> strncpy(rec->first_name, fn, 15); // correct in all cases
>
> as well as in the other direction
>
> strncpy(fn, rec->first_name, 15); // could use strcpy
>
> I'm using hard-coded constants for simplicity.
>
> If fn was initialized to zeros, and always used correctly to
> store a null-terminated string, there is nothing to do;
> a null is guaranteed to always be present at fn[15].
> Otherwise we need fn[15] = 0.
>
> So I think that's the nice use case for strncpy; unfortunately, over
> most of its life, it got clumsily used for other use cases.

Agreed. The name "strncpy" suggests that it's just "strcpy" with bounds
checking, but it isn't. "strncat", on the other hand, pretty much *is*
"strcat" with bounds checking. (And calling strncat() after setting the
initial character of the target to '\0' actually does what some people
assume strncpy should do.)

I wrote about this a few years ago:

https://github.com/Keith-S-Thompson/the-flat-trantor-society/blob/master/002-strncpy.md

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Medtronic
void Void(void) { Void(); } /* The recursive call of the void */

Re: Variadic functions

<uors3q$1uro0$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=33039&group=comp.lang.c#33039

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c
Subject: Re: Variadic functions
Date: Wed, 24 Jan 2024 12:31:53 -0800
Organization: A noiseless patient Spider
Lines: 8
Message-ID: <uors3q$1uro0$1@dont-email.me>
References: <uoed9m$387sh$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 24 Jan 2024 20:31:54 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="eb65a73d5fc6fb0010bcf2681c155871";
logging-data="2060032"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19fIwEU7+votPzo3zbm1mat5A1fH72vWqM="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:i1G1Lo2NE2WAv6jORuG4M8ptBMc=
Content-Language: en-US
In-Reply-To: <uoed9m$387sh$1@dont-email.me>
 by: Chris M. Thomasson - Wed, 24 Jan 2024 20:31 UTC

On 1/19/2024 9:59 AM, Malcolm McLean wrote:
> Who has actually used a variadic function for any purpose other than
> implementing printf-style format strings?
>

Iirc, I used variadic parameters back during my experiments with C99
generics a while back. IIRC, I posted about them on this group. They
should be on topic for this thread if I can find my old posts.

Re: Variadic functions

<uos8v2$20nr3$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=33054&group=comp.lang.c#33054

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.lang.c
Subject: Re: Variadic functions
Date: Thu, 25 Jan 2024 00:11:14 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 5
Message-ID: <uos8v2$20nr3$1@dont-email.me>
References: <uoed9m$387sh$1@dont-email.me> <uog6fs$3klco$1@dont-email.me>
<uoga8t$3l6mu$1@dont-email.me> <Kdccf9Gr26AQrYzxL@bongo-ra.co>
<pan$b84f3$321a3e4$17747387$71cad685@invalid.invalid>
<uomu5q$uj61$2@dont-email.me> <uonteh$16sb4$2@dont-email.me>
<xVQrN.249990$Wp_8.4423@fx17.iad> <uopc7g$1f17i$2@dont-email.me>
<uoqie3$1o25r$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 25 Jan 2024 00:11:14 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="a52c38d7852fe5be8309e2b943f36561";
logging-data="2121571"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18+Sco1trRXm55SWjC4eEPy"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:ILShzhmXBE9uzKtUzVa6D6b2Vgw=
 by: Lawrence D'Oliv - Thu, 25 Jan 2024 00:11 UTC

On Wed, 24 Jan 2024 09:40:34 +0100, David Brown wrote:

> Chained strcat ...

Not that you chained it, but that you used it at all.

Re: Variadic functions

<uos929$20nr3$2@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=33055&group=comp.lang.c#33055

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.neodome.net!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.lang.c
Subject: Re: Variadic functions
Date: Thu, 25 Jan 2024 00:12:57 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 8
Message-ID: <uos929$20nr3$2@dont-email.me>
References: <uoed9m$387sh$1@dont-email.me> <uog6fs$3klco$1@dont-email.me>
<uoga8t$3l6mu$1@dont-email.me> <Kdccf9Gr26AQrYzxL@bongo-ra.co>
<pan$b84f3$321a3e4$17747387$71cad685@invalid.invalid>
<uomu5q$uj61$2@dont-email.me> <uonteh$16sb4$2@dont-email.me>
<xVQrN.249990$Wp_8.4423@fx17.iad>
<pan$514f$ac2be24d$a46eca3d$5aa9624@invalid.invalid>
<uopi9d$1fs5l$2@dont-email.me> <uoqj5b$1o34g$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 25 Jan 2024 00:12:57 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="a52c38d7852fe5be8309e2b943f36561";
logging-data="2121571"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19s1Ugo+gHlBZoGPoBe+1+4"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:GfO7gXy/CuqUPeeabH6bzJThHQo=
 by: Lawrence D'Oliv - Thu, 25 Jan 2024 00:12 UTC

On Wed, 24 Jan 2024 09:52:59 +0100, David Brown wrote:

> The common standard C str* functions are, IMHO, extraordinarily poorly
> specified, and could easily have been very much easier to use correctly
> and efficiently.

True. Which is why it is better to look a little beyond them
<https://manpages.debian.org/7/string_copying.en.html>.

Re: Variadic functions

<uosnu6$266dv$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=33078&group=comp.lang.c#33078

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: jameskuyper@alumni.caltech.edu (James Kuyper)
Newsgroups: comp.lang.c
Subject: Re: Variadic functions
Date: Wed, 24 Jan 2024 23:26:46 -0500
Organization: A noiseless patient Spider
Lines: 32
Message-ID: <uosnu6$266dv$1@dont-email.me>
References: <uoed9m$387sh$1@dont-email.me> <uog6fs$3klco$1@dont-email.me>
<uoga8t$3l6mu$1@dont-email.me> <Kdccf9Gr26AQrYzxL@bongo-ra.co>
<pan$b84f3$321a3e4$17747387$71cad685@invalid.invalid>
<uomu5q$uj61$2@dont-email.me> <uonteh$16sb4$2@dont-email.me>
<xVQrN.249990$Wp_8.4423@fx17.iad>
<pan$514f$ac2be24d$a46eca3d$5aa9624@invalid.invalid>
<uopi9d$1fs5l$2@dont-email.me> <uoqj5b$1o34g$1@dont-email.me>
<20240124113737.421@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 25 Jan 2024 04:26:46 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="30e0e2d26f1f80f067929d2085a9c556";
logging-data="2300351"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX184nLIQJoDVO9NBWa7JACdFTDEKIj74YNc="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:imVXqTUx5EnHHKqpqmhRV5RVIRc=
In-Reply-To: <20240124113737.421@kylheku.com>
Content-Language: en-US
 by: James Kuyper - Thu, 25 Jan 2024 04:26 UTC

On 1/24/24 14:50, Kaz Kylheku wrote:
....
> I always assumed strncpy was intended for the following kind of
> situation: placing a string into a non-null-terminated array intended
> for data communication or storage.
>
> struct rec {
> char first_name[15]; // null padded, not terminated
> char last_name[15]; // likewise
> // ...
> };
>
> Internally, you work with these as null terminated strings,
> copying them out to [16] buffers.
>
> These get written to disk, so the data has to be cleanly
> padded with nulls, even if a single null is enough to
> terminate short fields. You don't want random garbage
> from memory written out; it could be a security problem.

That is precisely the situation that led to me using strncpy().
Apparently, the need to pad the space with nulls is the part that most
people find odd. In addition to the security issues you mention, there's
also a problem with comparing test output files from different runs - if
they don't get nulled out, the unused parts get garbage in them, and
routines for comparing the files will find different garbage in two
files that should be identical.
The other aspect of the situation I was working with that people found
odd was that I was sometimes copying a string that might be too long for
the destination, and it was OK if I had to truncate it to fit. That was
due to the fact that the relevant string served essentially as a
comment, rather than being a critical part of the output file.

Re: Variadic functions

<uot5no$27t7l$2@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=33087&group=comp.lang.c#33087

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.brown@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Variadic functions
Date: Thu, 25 Jan 2024 09:22:16 +0100
Organization: A noiseless patient Spider
Lines: 14
Message-ID: <uot5no$27t7l$2@dont-email.me>
References: <uoed9m$387sh$1@dont-email.me> <uog6fs$3klco$1@dont-email.me>
<uoga8t$3l6mu$1@dont-email.me> <Kdccf9Gr26AQrYzxL@bongo-ra.co>
<pan$b84f3$321a3e4$17747387$71cad685@invalid.invalid>
<uomu5q$uj61$2@dont-email.me> <uonteh$16sb4$2@dont-email.me>
<xVQrN.249990$Wp_8.4423@fx17.iad> <uopc7g$1f17i$2@dont-email.me>
<uoqie3$1o25r$1@dont-email.me> <uos8v2$20nr3$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 25 Jan 2024 08:22:16 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="752f8de187c834c86aaa2ef2831da121";
logging-data="2356469"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18dFiEYv/inEczdpjoXhlTzfYGIQZFGsSg="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:IHZ21Z986ymhMHNCOqkLuDl4TEI=
Content-Language: en-GB
In-Reply-To: <uos8v2$20nr3$1@dont-email.me>
 by: David Brown - Thu, 25 Jan 2024 08:22 UTC

On 25/01/2024 01:11, Lawrence D'Oliveiro wrote:
> On Wed, 24 Jan 2024 09:40:34 +0100, David Brown wrote:
>
>> Chained strcat ...
>
> Not that you chained it, but that you used it at all.

If you think people are missing your points, perhaps you should try
expressing them rather than berating people for not reading your mind.

"strcat" is a perfectly good and useful library function. Like every
function, without exception, you need to know what it does and that it
is appropriate and correct to use it in your code.

Re: Variadic functions

<uot7cr$284a5$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=33088&group=comp.lang.c#33088

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.brown@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Variadic functions
Date: Thu, 25 Jan 2024 09:50:35 +0100
Organization: A noiseless patient Spider
Lines: 113
Message-ID: <uot7cr$284a5$1@dont-email.me>
References: <uoed9m$387sh$1@dont-email.me> <uog6fs$3klco$1@dont-email.me>
<uoga8t$3l6mu$1@dont-email.me> <Kdccf9Gr26AQrYzxL@bongo-ra.co>
<pan$b84f3$321a3e4$17747387$71cad685@invalid.invalid>
<uomu5q$uj61$2@dont-email.me> <uonteh$16sb4$2@dont-email.me>
<xVQrN.249990$Wp_8.4423@fx17.iad>
<pan$514f$ac2be24d$a46eca3d$5aa9624@invalid.invalid>
<uopi9d$1fs5l$2@dont-email.me> <uoqj5b$1o34g$1@dont-email.me>
<20240124113737.421@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 25 Jan 2024 08:50:35 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="752f8de187c834c86aaa2ef2831da121";
logging-data="2363717"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX185wmqqV5mZrRhR14Dhh816Pc5MwPHh60k="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:pCFUEnBLkEhCdEloReZU03R/3LM=
Content-Language: en-GB
In-Reply-To: <20240124113737.421@kylheku.com>
 by: David Brown - Thu, 25 Jan 2024 08:50 UTC

On 24/01/2024 20:50, Kaz Kylheku wrote:
> On 2024-01-24, David Brown <david.brown@hesbynett.no> wrote:
>> On 24/01/2024 00:31, Lawrence D'Oliveiro wrote:
>>> On Tue, 23 Jan 2024 22:28:03 -0000 (UTC), Blue-Maned_Hawk wrote:
>>>
>>>> strncat
>>>
>>> If you can figure out how to use it properly.
>>
>> strncat is nasty, as "n" is the maximum number of characters to copy,
>> not the size of the destination buffer. strncpy is nasty as it wastes
>> effort needlessly writing null characters once the string is copied, and
>> does not guarantee a terminating null.
>
> I always assumed strncpy was intended for the following kind of
> situation: placing a string into a non-null-terminated array intended
> for data communication or storage.
>
> struct rec {
> char first_name[15]; // null padded, not terminated
> char last_name[15]; // likewise
> // ...
> };
>
> Internally, you work with these as null terminated strings,
> copying them out to [16] buffers.

(I'd consider using 16 char arrays and saying that they /are/ null
terminated. (Of course you don't trust that when you read records from
files - you either check it, or force it, before using the record.)
Much of your copying can then be very efficient inlined memcpy()'s - if
you even need to copy data into and out of buffers. I understand this
is about good use-cases for strncpy, rather than about ideal
implementations of record storage. But if good uses of strncpy could be
replaced by better code that does not use it, it weakens the
justification for strncpy being defined the way it is.)

>
> These get written to disk, so the data has to be cleanly
> padded with nulls, even if a single null is enough to
> terminate short fields. You don't want random garbage
> from memory written out; it could be a security problem.

Certainly strncpy would be good for such purposes. It is quite common,
however, to use memset to first zero out such structs - that is simple,
efficient (usually more efficient than strncpy would be), and also
clears any padding.

But I am not saying that strncpy, as it is defined, has no uses - I am
saying that it would be more useful if it had been defined to copy a
string with a hard limit on the number of characters to avoid any risk
of overrun, without copying or padding unnecessarily, and with a
guaranteed zero termination:

char * string_copy(char * restrict dest,
const char * restrict src, size_t n)
{ while (n--) {
char c = *src++;
*dest++ = c;
if (!c) return dest - 1;
}
*(dest - 1) = '\0';
return dest - 1;
}

(This also returns a pointer to the terminating null, which I see as
more useful than a copy of "dest".)

>
> strncpy helps to prepare the fields:
>
> char fn[16]; // null terminated
> /* ... */
> strncpy(rec->first_name, fn, 15); // correct in all cases
>
> as well as in the other direction
>
> strncpy(fn, rec->first_name, 15); // could use strcpy

"strcpy" might be a risk if you have read the record from a file and
can't be sure it is null terminated. "strncpy" avoids that risk, but
inefficiently and unnecessarily does extra padding. I would say it is
better here to use memcpy(), then "fn[15] = '\0';".

>
> I'm using hard-coded constants for simplicity.

Sure.

>
> If fn was initialized to zeros, and always used correctly to
> store a null-terminated string, there is nothing to do;
> a null is guaranteed to always be present at fn[15].
> Otherwise we need fn[15] = 0.
>
> So I think that's the nice use case for strncpy; unfortunately, over
> most of its life, it got clumsily used for other use cases.
>

I certainly do think strncpy, as it is defined, has its uses. And in
many situations it is a better choice than strcpy, even if it is less
efficient. But I think it was a wasted opportunity - I think "strncpy"
should have been defined the way I defined "string_copy" above (though
for consistency it should return its first argument - and a matching
stpncpy function could be made that returns an end pointer).

Then a separate "strpad(char * s, size_t n)" function could be made that
takes a string pointer and ensures that everything beyond the
terminating '\0' is cleared to zero up to a total of n characters. (If
there is no zero in those n characters, the data is left unchanged.)

Re: Variadic functions

<uot9ph$28gke$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=33089&group=comp.lang.c#33089

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.1d4.us!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.brown@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Variadic functions
Date: Thu, 25 Jan 2024 10:31:29 +0100
Organization: A noiseless patient Spider
Lines: 29
Message-ID: <uot9ph$28gke$1@dont-email.me>
References: <uoed9m$387sh$1@dont-email.me> <uog6fs$3klco$1@dont-email.me>
<uoga8t$3l6mu$1@dont-email.me> <Kdccf9Gr26AQrYzxL@bongo-ra.co>
<pan$b84f3$321a3e4$17747387$71cad685@invalid.invalid>
<uomu5q$uj61$2@dont-email.me> <uonteh$16sb4$2@dont-email.me>
<xVQrN.249990$Wp_8.4423@fx17.iad>
<pan$514f$ac2be24d$a46eca3d$5aa9624@invalid.invalid>
<uopi9d$1fs5l$2@dont-email.me> <uoqj5b$1o34g$1@dont-email.me>
<20240124113737.421@kylheku.com> <87mssu5w0u.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 25 Jan 2024 09:31:29 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="752f8de187c834c86aaa2ef2831da121";
logging-data="2376334"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18NbaFeOPu9KohHFkj8qsJ4Ti2Mp51DvqM="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:LQsRiHOFrkxKW5YosXfNpVv5fGw=
In-Reply-To: <87mssu5w0u.fsf@nosuchdomain.example.com>
Content-Language: en-GB
 by: David Brown - Thu, 25 Jan 2024 09:31 UTC

On 24/01/2024 21:14, Keith Thompson wrote:

> Agreed. The name "strncpy" suggests that it's just "strcpy" with bounds
> checking, but it isn't. "strncat", on the other hand, pretty much *is*
> "strcat" with bounds checking. (And calling strncat() after setting the
> initial character of the target to '\0' actually does what some people
> assume strncpy should do.)
>
> I wrote about this a few years ago:
>
> https://github.com/Keith-S-Thompson/the-flat-trantor-society/blob/master/002-strncpy.md
>

You could add a paragraph about how strncat also does not do what people
might expect it to do. The "n" is the maximum number of characters to
copy, not including the terminating zero. The most obvious user
expectation would be that "n" is the size of the destination buffer, and
the second most obvious expectation would be that it is the maximum
number of characters copied /including/ the terminating zero.

So IMHO, strncat does /roughly/ what you expect when you have set the
first character of the target to 0 (once you remember to subtract 1 from
the size), but is otherwise surprising in its behaviour. It should have
been better.

Oh, and there should have been a "strnlen" too. It would do the same as
"memchr(s, '\0', n)", but have a better name. It's in POSIX and should
be in the C standards.

Re: Variadic functions

<uotk21$2a1tg$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=33093&group=comp.lang.c#33093

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.brown@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Variadic functions
Date: Thu, 25 Jan 2024 13:26:40 +0100
Organization: A noiseless patient Spider
Lines: 19
Message-ID: <uotk21$2a1tg$1@dont-email.me>
References: <uoed9m$387sh$1@dont-email.me> <uog6fs$3klco$1@dont-email.me>
<uoga8t$3l6mu$1@dont-email.me> <Kdccf9Gr26AQrYzxL@bongo-ra.co>
<pan$b84f3$321a3e4$17747387$71cad685@invalid.invalid>
<uomu5q$uj61$2@dont-email.me> <uonteh$16sb4$2@dont-email.me>
<xVQrN.249990$Wp_8.4423@fx17.iad>
<pan$514f$ac2be24d$a46eca3d$5aa9624@invalid.invalid>
<uopi9d$1fs5l$2@dont-email.me> <uoqj5b$1o34g$1@dont-email.me>
<uos929$20nr3$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 25 Jan 2024 12:26:41 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="752f8de187c834c86aaa2ef2831da121";
logging-data="2426800"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19j+39GEikfURCFHSQHyfkhUi8QnWdgK/w="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:paqdBi5Q8s1UYI3OKSAU16KlRqA=
Content-Language: en-GB
In-Reply-To: <uos929$20nr3$2@dont-email.me>
 by: David Brown - Thu, 25 Jan 2024 12:26 UTC

On 25/01/2024 01:12, Lawrence D'Oliveiro wrote:
> On Wed, 24 Jan 2024 09:52:59 +0100, David Brown wrote:
>
>> The common standard C str* functions are, IMHO, extraordinarily poorly
>> specified, and could easily have been very much easier to use correctly
>> and efficiently.
>
> True. Which is why it is better to look a little beyond them
> <https://manpages.debian.org/7/string_copying.en.html>.

Yes.

Unfortunately, most of these are not standard C library functions. Some
are common extensions in many libraries, others are, AFIAK, not so common.

Still, thanks for that reference. I'll have a look at them and see if
they are potentially useful to me and available with the C libraries I
usually use.

Re: Variadic functions

<uou6op$2d0m5$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=33121&group=comp.lang.c#33121

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: malcolm.arthur.mclean@gmail.com (Malcolm McLean)
Newsgroups: comp.lang.c
Subject: Re: Variadic functions
Date: Thu, 25 Jan 2024 17:46:01 +0000
Organization: A noiseless patient Spider
Lines: 22
Message-ID: <uou6op$2d0m5$1@dont-email.me>
References: <uoed9m$387sh$1@dont-email.me> <uog6fs$3klco$1@dont-email.me>
<uoga8t$3l6mu$1@dont-email.me> <Kdccf9Gr26AQrYzxL@bongo-ra.co>
<pan$b84f3$321a3e4$17747387$71cad685@invalid.invalid>
<uomu5q$uj61$2@dont-email.me> <uonteh$16sb4$2@dont-email.me>
<xVQrN.249990$Wp_8.4423@fx17.iad> <uopc7g$1f17i$2@dont-email.me>
<uoqie3$1o25r$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 25 Jan 2024 17:46:01 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b7a742cafaaee8d7e42fafde008796a0";
logging-data="2523845"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/QJ7x86iCSGmI9nda5NYkixR9jsySo7GE="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:SRIP/4hFL3fHEGrXjt4WIgIeJJk=
In-Reply-To: <uoqie3$1o25r$1@dont-email.me>
Content-Language: en-GB
 by: Malcolm McLean - Thu, 25 Jan 2024 17:46 UTC

On 24/01/2024 08:40, David Brown wrote:
>
> So when someone has used "strcat" in a Usenet post, I would assume that
> real-world code would take appropriate care to be sure the arguments
> point to valid strings, and the destinations have enough space.  It's
> not unlikely that the real code actually uses "strncat" to reduce the
> risk of failures.
>
By doing that you are replacing undefined behaviour with defined incorrect
behaviour. Now undefined behaviour will often be correct behaviour, in the
context that the program as whole is bugged. The result "segmentation fault"
is less wrong than "the answeer to the life, universe and everything is 4"
(we added an extra e to "answer" and so ran out of buffer).
Not always and sometimes wrong results are better than no results, for
example in video game. Better send 4 baddies rather than 42 than shut
down the game.

--
Check out Basic Algorithms and my other books:
https://www.lulu.com/spotlight/bgy1mm

Re: Variadic functions

<20240125101502.206@kylheku.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=33124&group=comp.lang.c#33124

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 433-929-6894@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: Variadic functions
Date: Thu, 25 Jan 2024 18:22:51 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 38
Message-ID: <20240125101502.206@kylheku.com>
References: <uoed9m$387sh$1@dont-email.me> <uog6fs$3klco$1@dont-email.me>
<uoga8t$3l6mu$1@dont-email.me> <Kdccf9Gr26AQrYzxL@bongo-ra.co>
<pan$b84f3$321a3e4$17747387$71cad685@invalid.invalid>
<uomu5q$uj61$2@dont-email.me> <uonteh$16sb4$2@dont-email.me>
<xVQrN.249990$Wp_8.4423@fx17.iad> <uopc7g$1f17i$2@dont-email.me>
<uoqie3$1o25r$1@dont-email.me> <uou6op$2d0m5$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 25 Jan 2024 18:22:51 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="64d741b4aedd1f2c9b80077e3ef47e46";
logging-data="2536529"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+bK7jyEXavzKUQZ2mptBMeqvLzRjH9xyQ="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:Xf04m7BDE32ND7VOCHJJqBJKOHY=
 by: Kaz Kylheku - Thu, 25 Jan 2024 18:22 UTC

On 2024-01-25, Malcolm McLean <malcolm.arthur.mclean@gmail.com> wrote:
> On 24/01/2024 08:40, David Brown wrote:
>>
>> So when someone has used "strcat" in a Usenet post, I would assume that
>> real-world code would take appropriate care to be sure the arguments
>> point to valid strings, and the destinations have enough space.  It's
>> not unlikely that the real code actually uses "strncat" to reduce the
>> risk of failures.
>>
> By doing that you are replacing undefined behaviour with defined incorrect
> behaviour. Now undefined behaviour will often be correct behaviour, in the
> context that the program as whole is bugged. The result "segmentation fault"
> is less wrong than "the answeer to the life, universe and everything is 4"
> (we added an extra e to "answer" and so ran out of buffer).

You're only saying that a documented extension which traps undefined
behavior is better than a silent incorrect result.

The crude memory violation checks resulting in a segmentation fault are
not even remotely close to a reliable trap for undefined behavior.

If you think that the situation when some datum does not fit into a
buffer is a grave situation, such that the program must not continue
executing, then you should use a copying function that issues a
diagnostic and aborts in that situation (like an assert failure).

I work on embedded firmware where that is the case. For string copying,
we have have functions that will abort if truncation would take place,
that return an indication that truncation took place, allowing the
caller to decide what to do, and ones that silently truncate.

Under no circumstances do you want to just overflow the buffer and hope
that a segfault will catch it.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: Variadic functions

<uou9nl$2dgi8$2@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=33126&group=comp.lang.c#33126

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!rocksolid2!news.neodome.net!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: jameskuyper@alumni.caltech.edu (James Kuyper)
Newsgroups: comp.lang.c
Subject: Re: Variadic functions
Date: Thu, 25 Jan 2024 13:36:37 -0500
Organization: A noiseless patient Spider
Lines: 23
Message-ID: <uou9nl$2dgi8$2@dont-email.me>
References: <uoed9m$387sh$1@dont-email.me> <uog6fs$3klco$1@dont-email.me>
<uoga8t$3l6mu$1@dont-email.me> <Kdccf9Gr26AQrYzxL@bongo-ra.co>
<pan$b84f3$321a3e4$17747387$71cad685@invalid.invalid>
<uomu5q$uj61$2@dont-email.me> <uonteh$16sb4$2@dont-email.me>
<xVQrN.249990$Wp_8.4423@fx17.iad> <uopc7g$1f17i$2@dont-email.me>
<uoqie3$1o25r$1@dont-email.me> <uou6op$2d0m5$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 25 Jan 2024 18:36:38 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="30e0e2d26f1f80f067929d2085a9c556";
logging-data="2540104"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18xr+G/H20MaFGYl9JpGs0yUoZ7IyuVyj0="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:aUL8hkrDGUwsm9aG3nlX/TIloeE=
In-Reply-To: <uou6op$2d0m5$1@dont-email.me>
Content-Language: en-US
 by: James Kuyper - Thu, 25 Jan 2024 18:36 UTC

On 2024-01-25, Malcolm McLean <malcolm.arthur.mclean@gmail.com> wrote:
> On 24/01/2024 08:40, David Brown wrote:
>>
>> So when someone has used "strcat" in a Usenet post, I would assume that
>> real-world code would take appropriate care to be sure the arguments
>> point to valid strings, and the destinations have enough space.  It's
>> not unlikely that the real code actually uses "strncat" to reduce the
>> risk of failures.
>>
> By doing that you are replacing undefined behaviour with defined incorrect
> behaviour. Now undefined behaviour will often be correct behaviour, in the
> context that the program as whole is bugged. The result "segmentation fault"
> is less wrong than "the answeer to the life, universe and everything is 4"
> (we added an extra e to "answer" and so ran out of buffer).

That depends entirely upon what the correct behavior was supposed to be.
In the contexts where I used strncat(), the correct behavior specified
by the requirements for the program I was writing when the input string
was too big to fit in the output field was to put as much of the input
string as possible into the output field, including the start of the
string, even if that meant not having a terminating null character, but
that it must not go past the end of the field, even if that meant
chopping off the end of the string.

Re: Variadic functions

<87h6j144u9.fsf@nosuchdomain.example.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=33129&group=comp.lang.c#33129

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S.Thompson+u@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: Variadic functions
Date: Thu, 25 Jan 2024 10:59:10 -0800
Organization: None to speak of
Lines: 32
Message-ID: <87h6j144u9.fsf@nosuchdomain.example.com>
References: <uoed9m$387sh$1@dont-email.me> <uog6fs$3klco$1@dont-email.me>
<uoga8t$3l6mu$1@dont-email.me> <Kdccf9Gr26AQrYzxL@bongo-ra.co>
<pan$b84f3$321a3e4$17747387$71cad685@invalid.invalid>
<uomu5q$uj61$2@dont-email.me> <uonteh$16sb4$2@dont-email.me>
<xVQrN.249990$Wp_8.4423@fx17.iad>
<pan$514f$ac2be24d$a46eca3d$5aa9624@invalid.invalid>
<uopi9d$1fs5l$2@dont-email.me> <uoqj5b$1o34g$1@dont-email.me>
<20240124113737.421@kylheku.com> <uot7cr$284a5$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="25cdfcf079fd064704ce92ded0c098b3";
logging-data="2550163"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/fYbSS7feeuCGZZZPuJX9v"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:Cs07rKIvBlRc0EFa5cmDycsavZU=
sha1:XGyg6mxoUeb6Eo21h+J09d3RKEU=
 by: Keith Thompson - Thu, 25 Jan 2024 18:59 UTC

David Brown <david.brown@hesbynett.no> writes:
[...]
> I certainly do think strncpy, as it is defined, has its uses. And in
> many situations it is a better choice than strcpy, even if it is less
> efficient. But I think it was a wasted opportunity - I think
> "strncpy" should have been defined the way I defined "string_copy"
> above (though for consistency it should return its first argument -
> and a matching stpncpy function could be made that returns an end
> pointer).
>
> Then a separate "strpad(char * s, size_t n)" function could be made
> that takes a string pointer and ensures that everything beyond the
> terminating '\0' is cleared to zero up to a total of n characters.
> (If there is no zero in those n characters, the data is left
> unchanged.)

To summarize, the problem with strncpy() is not its behavior, but its
name. The name suggests that it's something that it isn't, a
bound-checked version of strcpy(). It *is* useful for certain niche
cases. For example, I think early Unix filesystems stored file names in
a 14-byte field padded with zero or more null characters; that may have
been *the* primary use case for strncpy().

If I had a time machine, I might rename "strncpy()" to, say, "strpad()"
(not the same as the "strpad() you suggest above) and define a new
"strncpy()" that behaves like setting the first character of the target
to '\0' and then calling strncat().

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Medtronic
void Void(void) { Void(); } /* The recursive call of the void */

Re: Variadic functions

<87cytp44o0.fsf@nosuchdomain.example.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=33130&group=comp.lang.c#33130

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S.Thompson+u@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: Variadic functions
Date: Thu, 25 Jan 2024 11:02:55 -0800
Organization: None to speak of
Lines: 37
Message-ID: <87cytp44o0.fsf@nosuchdomain.example.com>
References: <uoed9m$387sh$1@dont-email.me> <uog6fs$3klco$1@dont-email.me>
<uoga8t$3l6mu$1@dont-email.me> <Kdccf9Gr26AQrYzxL@bongo-ra.co>
<pan$b84f3$321a3e4$17747387$71cad685@invalid.invalid>
<uomu5q$uj61$2@dont-email.me> <uonteh$16sb4$2@dont-email.me>
<xVQrN.249990$Wp_8.4423@fx17.iad>
<pan$514f$ac2be24d$a46eca3d$5aa9624@invalid.invalid>
<uopi9d$1fs5l$2@dont-email.me> <uoqj5b$1o34g$1@dont-email.me>
<20240124113737.421@kylheku.com>
<87mssu5w0u.fsf@nosuchdomain.example.com>
<uot9ph$28gke$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="25cdfcf079fd064704ce92ded0c098b3";
logging-data="2550163"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/FxZnGiBZDMCPu+PGqOIkJ"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:3I/yFRKIS89d5MMN4cgBhTyzaas=
sha1:PknBdDX2LwcKSpcuRiz7w52ysNo=
 by: Keith Thompson - Thu, 25 Jan 2024 19:02 UTC

David Brown <david.brown@hesbynett.no> writes:
> On 24/01/2024 21:14, Keith Thompson wrote:
>
>> Agreed. The name "strncpy" suggests that it's just "strcpy" with bounds
>> checking, but it isn't. "strncat", on the other hand, pretty much *is*
>> "strcat" with bounds checking. (And calling strncat() after setting the
>> initial character of the target to '\0' actually does what some people
>> assume strncpy should do.)
>> I wrote about this a few years ago:
>> https://github.com/Keith-S-Thompson/the-flat-trantor-society/blob/master/002-strncpy.md
>
> You could add a paragraph about how strncat also does not do what
> people might expect it to do. The "n" is the maximum number of
> characters to copy, not including the terminating zero. The most
> obvious user expectation would be that "n" is the size of the
> destination buffer, and the second most obvious expectation would be
> that it is the maximum number of characters copied /including/ the
> terminating zero.

Thanks, I've added that as a TODO.

When I get the proverbial Round Tuit, I'll also cite the C99 Rationale:

strncpy was initially introduced into the C library to deal
with fixed-length name fields in structures such as directory
entries. Such fields are not used in the same way as strings:
the trailing null is unnecessary for a maximum-length field,
and setting trailing bytes for shorter names to null assures
efficient field-wise comparisons. strncpy is not by origin a
"bounded strcpy," and the Committee preferred to recognize
existing practice rather than alter the function to better suit
it to such use.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Medtronic
void Void(void) { Void(); } /* The recursive call of the void */

Re: Variadic functions

<uoudaj$2e8hm$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=33135&group=comp.lang.c#33135

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!rocksolid2!news.neodome.net!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.brown@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Variadic functions
Date: Thu, 25 Jan 2024 20:37:55 +0100
Organization: A noiseless patient Spider
Lines: 39
Message-ID: <uoudaj$2e8hm$1@dont-email.me>
References: <uoed9m$387sh$1@dont-email.me> <uog6fs$3klco$1@dont-email.me>
<uoga8t$3l6mu$1@dont-email.me> <Kdccf9Gr26AQrYzxL@bongo-ra.co>
<pan$b84f3$321a3e4$17747387$71cad685@invalid.invalid>
<uomu5q$uj61$2@dont-email.me> <uonteh$16sb4$2@dont-email.me>
<xVQrN.249990$Wp_8.4423@fx17.iad> <uopc7g$1f17i$2@dont-email.me>
<uoqie3$1o25r$1@dont-email.me> <uou6op$2d0m5$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 25 Jan 2024 19:37:55 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f683ae60830b70bfa20d120c06e7e3c5";
logging-data="2564662"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19RAGeE+sQ+H3P931mQyBAoiQVEzslt2wo="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:f7PdzdvHo9kLRrbooZ8DF0uMWMg=
In-Reply-To: <uou6op$2d0m5$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Thu, 25 Jan 2024 19:37 UTC

On 25/01/2024 18:46, Malcolm McLean wrote:
> On 24/01/2024 08:40, David Brown wrote:
>>
>> So when someone has used "strcat" in a Usenet post, I would assume
>> that real-world code would take appropriate care to be sure the
>> arguments point to valid strings, and the destinations have enough
>> space.  It's not unlikely that the real code actually uses "strncat"
>> to reduce the risk of failures.
>>
> By doing that you are replacing undefined behaviour with defined incorrect
> behaviour. Now undefined behaviour will often be correct behaviour, in the
> context that the program as whole is bugged. The result "segmentation
> fault"
> is less wrong than "the answeer to the life, universe and everything is 4"
> (we added an extra e to "answer" and so ran out of buffer).
> Not always and sometimes wrong results are better than no results, for
> example in video game. Better send 4 baddies rather than 42 than shut
> down the game.
>

That is all totally dependent on the situation, and the requirements.

A buffer overrun is always wrong. It might not have major consequences,
and those consequences might even be predictable (like a segmentation
fault), though it is highly unlikely.

Cutting a string short might also be an error - in which case, if it is
a possibility, you should add checking somewhere. (It's typically quite
easy to do here - if you want to limit your strings to 20 characters,
including the null terminator, make your buffer 21 characters, clear it,
and use that for your limits for strncat, strncpy or snprintf as
appropriate. Then when you are done, check index 19 of your buffer - if
it is not a null character, your string has been truncated.)

Or cutting a string short might be perfectly correct and acceptable
behaviour.

Any generalisation here will be wrong much of the time.

Re: Variadic functions

<87sf2l2od8.fsf@nosuchdomain.example.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=33136&group=comp.lang.c#33136

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S.Thompson+u@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: Variadic functions
Date: Thu, 25 Jan 2024 11:40:19 -0800
Organization: None to speak of
Lines: 29
Message-ID: <87sf2l2od8.fsf@nosuchdomain.example.com>
References: <uoed9m$387sh$1@dont-email.me> <uog6fs$3klco$1@dont-email.me>
<uoga8t$3l6mu$1@dont-email.me> <Kdccf9Gr26AQrYzxL@bongo-ra.co>
<pan$b84f3$321a3e4$17747387$71cad685@invalid.invalid>
<uomu5q$uj61$2@dont-email.me> <uonteh$16sb4$2@dont-email.me>
<xVQrN.249990$Wp_8.4423@fx17.iad> <uopc7g$1f17i$2@dont-email.me>
<uoqie3$1o25r$1@dont-email.me> <uou6op$2d0m5$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: dont-email.me; posting-host="25cdfcf079fd064704ce92ded0c098b3";
logging-data="2562242"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18o9ThIln+WifNEgL6j4Cnm"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:W6BptAmpWgym0Epx4AGym9ajVnQ=
sha1:bQ0YFRunt4f9K4MH1g/jcrRV14U=
 by: Keith Thompson - Thu, 25 Jan 2024 19:40 UTC

Malcolm McLean <malcolm.arthur.mclean@gmail.com> writes:
> On 24/01/2024 08:40, David Brown wrote:
>> So when someone has used "strcat" in a Usenet post, I would assume
>> that real-world code would take appropriate care to be sure the
>> arguments point to valid strings, and the destinations have enough
>> space.  It's not unlikely that the real code actually uses "strncat"
>> to reduce the risk of failures.
>>
> By doing that you are replacing undefined behaviour with defined incorrect
> behaviour.

What "incorrect behaviour" are you talking about?

David's premise is that the arguments point to valid strings and the
destinations have enough space. Given that premise, strcat() works.

char s[10];
strcat(s, "hello");
puts(s);

What incorrect behavior do you see in the above code?

Of course ensuring that the arguments are valid and the destination is
big enough is sometimes easier said than done, but that's not the point.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Medtronic
void Void(void) { Void(); } /* The recursive call of the void */

Re: Variadic functions

<uouejj$2ei7i$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=33138&group=comp.lang.c#33138

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.lang.c
Subject: Re: Variadic functions
Date: Thu, 25 Jan 2024 19:59:48 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 5
Message-ID: <uouejj$2ei7i$1@dont-email.me>
References: <uoed9m$387sh$1@dont-email.me> <uog6fs$3klco$1@dont-email.me>
<uoga8t$3l6mu$1@dont-email.me> <Kdccf9Gr26AQrYzxL@bongo-ra.co>
<pan$b84f3$321a3e4$17747387$71cad685@invalid.invalid>
<uomu5q$uj61$2@dont-email.me> <uonteh$16sb4$2@dont-email.me>
<xVQrN.249990$Wp_8.4423@fx17.iad> <uopc7g$1f17i$2@dont-email.me>
<uoqie3$1o25r$1@dont-email.me> <uos8v2$20nr3$1@dont-email.me>
<uot5no$27t7l$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 25 Jan 2024 19:59:48 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="a52c38d7852fe5be8309e2b943f36561";
logging-data="2574578"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+2luIgbIL8Yc0rkvqAIMTt"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:3XA9yFIebAHGE2jbPO+U9xKjMPs=
 by: Lawrence D'Oliv - Thu, 25 Jan 2024 19:59 UTC

On Thu, 25 Jan 2024 09:22:16 +0100, David Brown wrote:

> "strcat" is a perfectly good and useful library function.

Can’t think of any situation where it is worth using.

Re: Variadic functions

<uougc3$g4o3$1@news.xmission.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=33144&group=comp.lang.c#33144

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!rocksolid2!news.neodome.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gazelle@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.lang.c
Subject: Re: Variadic functions
Date: Thu, 25 Jan 2024 20:29:55 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <uougc3$g4o3$1@news.xmission.com>
References: <uoed9m$387sh$1@dont-email.me> <uos8v2$20nr3$1@dont-email.me> <uot5no$27t7l$2@dont-email.me> <uouejj$2ei7i$1@dont-email.me>
Injection-Date: Thu, 25 Jan 2024 20:29:55 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="529155"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Thu, 25 Jan 2024 20:29 UTC

In article <uouejj$2ei7i$1@dont-email.me>,
Lawrence D'Oliveiro <ldo@nz.invalid> wrote:
>On Thu, 25 Jan 2024 09:22:16 +0100, David Brown wrote:
>
>> "strcat" is a perfectly good and useful library function.
>
>Can't think of any situation where it is worth using.

Well, that's clearly your problem, not ours.

As the kids say, it's a "you problem".

--
The randomly chosen signature file that would have appeared here is more than 4
lines long. As such, it violates one or more Usenet RFCs. In order to remain
in compliance with said RFCs, the actual sig can be found at the following URL:
http://user.xmission.com/~gazelle/Sigs/ModernXtian

Re: Variadic functions

<20240125131550.853@kylheku.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=33150&group=comp.lang.c#33150

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 433-929-6894@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: Variadic functions
Date: Thu, 25 Jan 2024 21:55:18 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 33
Message-ID: <20240125131550.853@kylheku.com>
References: <uoed9m$387sh$1@dont-email.me> <uos8v2$20nr3$1@dont-email.me>
<uot5no$27t7l$2@dont-email.me> <uouejj$2ei7i$1@dont-email.me>
<uougc3$g4o3$1@news.xmission.com>
Injection-Date: Thu, 25 Jan 2024 21:55:18 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="64d741b4aedd1f2c9b80077e3ef47e46";
logging-data="2612704"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/oHrNSFN4TE5gS2WkrqwHuHKMpAMsUe6s="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:1naZ6+QQX2MqFK/pFBF+WzrX6UM=
 by: Kaz Kylheku - Thu, 25 Jan 2024 21:55 UTC

On 2024-01-25, Kenny McCormack <gazelle@shell.xmission.com> wrote:
> In article <uouejj$2ei7i$1@dont-email.me>,
> Lawrence D'Oliveiro <ldo@nz.invalid> wrote:
>>On Thu, 25 Jan 2024 09:22:16 +0100, David Brown wrote:
>>
>>> "strcat" is a perfectly good and useful library function.
>>
>>Can't think of any situation where it is worth using.
>
> Well, that's clearly your problem, not ours.
>
> As the kids say, it's a "you problem".

In the entire TXR project:

$ git grep strcat
stream.c: strcat(num_buf, ".0");

What's that? This is when the format function, in certain circumstances,
is printing a floating-point number that has no fractional part,
and the representation has to be machine-readable to reproduce the
same object (Lisp "print-read consistency").

This strcat is justified because num_buf is 512 characters. We know
in this line of code that that it holds a printed floating-point value
in decimal, and we know that double's exponent caps out at E+308. There
cannot be an IEEE 64 bit floating-point double with anywhere near 512
digits to the left of the decimal point on all the platforms.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca


devel / comp.lang.c / Variadic functions

Pages:12345
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor