Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Win95 is not a virus; a virus does something. -- unknown source


devel / comp.lang.c / Re: 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

<uoulmv$2fp6j$1@dont-email.me>

  copy mid

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

  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 22:01:04 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 14
Message-ID: <uoulmv$2fp6j$1@dont-email.me>
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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 25 Jan 2024 22:01:04 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="a52c38d7852fe5be8309e2b943f36561";
logging-data="2614483"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX187awFAtlYaicmItL5dC3V0"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:pahi+gGonLIrXNAZizWdPueTqoQ=
 by: Lawrence D'Oliv - Thu, 25 Jan 2024 22:01 UTC

On Thu, 25 Jan 2024 20:29:55 -0000 (UTC), Kenny McCormack 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.

“You may say that I’m a dreamer;
But I’m not the only one.”

Re: Variadic functions

<pan$ec0b$562b777f$6399b026$ea94f9f2@invalid.invalid>

  copy mid

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

  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!bluemanedhawk.eternal-september.org!.POSTED!not-for-mail
From: bluemanedhawk@invalid.invalid (Blue-Maned_Hawk)
Newsgroups: comp.lang.c
Subject: Re: Variadic functions
Date: Thu, 25 Jan 2024 22:46:06 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 22
Message-ID: <pan$ec0b$562b777f$6399b026$ea94f9f2@invalid.invalid>
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 22:46:06 -0000 (UTC)
Injection-Info: bluemanedhawk.eternal-september.org; posting-host="3085131c6ec2e03abe51cf88e7b94f27";
logging-data="2617370"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18yocmT7KZWPNMTdNRWXjtZnqyAQG4ZXD8="
User-Agent: Pan/0.154 (Izium; 517acf4)
Cancel-Lock: sha1:PbeLHJqEHUqO2eXdV39lkWWHArk=
X-Face: Llanfair­pwllgwyngyllÃ
ƒ‚Ã
ƒƒ‚­gogery­chwyrnÃ
ƒƒ
? ?‚­drobwll­llan
Ã
? ?ƒƒ‚­tysilio­go
g
o­goch
Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAIAAADYYG7QAAACh0lEQVRYw71Z21bD
MAzzevbfkr4cHjrSXJyL044+MDa6WLEl2SkvkrZ1AbAvXO+bUGSCPYnsuIVGMpm
ZLnjX718GhAKNsp8lON2F9VrhELwIgJlBepkZjA78rVK+FkmNhEJK76UsJlz8+E
rJsjrpYouhLo/SC6qPHgakFOR8wV9+8rCfO/I/oVnmUZUp42/LW2XkLj9TCFNM9
jp5g2EmHZgpYZjCOkYU7sXVogRylJqpdggoFLG1g09Flah/7kErCxzR9HgXPYsq
0glb9cxjIz2Vsk9AmAoCSxECpD713joMKjQqLAtmMqJmXjdVvlMnMQCVITotJd1
z+fh1f1NNo+vuc1KnhWUmY7t03vydTud9BbXCtN3L2PL3bK7JCNG0GHzuZxafyB
fxevCxpm1vrwZltqw6SILCcdoCE6PGQC8wZWDA9Or7Qp5s3lAZezys0nDazs9S9
R0TjwEiksRxLkNPC1NMMWPs1bj0Ei0Yuo+JVtFLuzP1NRJ16qXWN8DhhtmS4PDg
O6mqRxs4bEJrYt087mSIow/1VzW2oFlMQuiuIy/KsUagvhdw6hSjJGlIavbLF8x
j3X47bccLcUSi0dkWh1nUZNhANT1tHKUXrNxNLbd9KPb9wDDVrKwmPQMOPQ1oy6
k5I1DwzDeRJd3jVIhDAUxq3ngzJG4CCkNXZxZVMcjefoK2J0gUY2S3rxz/RuTFx
2zHd9U+obimJXMG4edsk/2j5pTU5G1MmzbRLxkfq5EiT1GGsidvMGzi+1goGb2l
GCrN+nGnV8xj3q3JLRDVPL96vUc7Z4aJ3TN1mVqWAMJMfG+Jxh6TQqP+92iZkCU
xtglds1AB6r0aiSHKcnFck+p/c/0CbacFLQcajGcAAAAASUVORK5CYII=
 by: Blue-Maned_Hawk - Thu, 25 Jan 2024 22:46 UTC

David Brown 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.

sizeof outbuf - strlen(outbuf)

--
Blue-Maned_Hawk│shortens to
Hawk│/
blu.mɛin.dÊ°ak/
│he/him/his/himself/Mr.
blue-maned_hawk.srht.site

Re: Variadic functions

<uour9j$2gkc8$1@dont-email.me>

  copy mid

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

  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 23:36:18 +0000
Organization: A noiseless patient Spider
Lines: 49
Message-ID: <uour9j$2gkc8$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>
<87sf2l2od8.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 25 Jan 2024 23:36:19 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="8cfe1ab2d07228fe591571b8291a5d4c";
logging-data="2642312"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1855xBJIup8AJ9yKMNT8AkqEqycQzDpv0E="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:oxoXvg8XCH6vquhTGea27wpKKaM=
In-Reply-To: <87sf2l2od8.fsf@nosuchdomain.example.com>
Content-Language: en-GB
 by: Malcolm McLean - Thu, 25 Jan 2024 23:36 UTC

On 25/01/2024 19:40, Keith Thompson wrote:
> 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.
>
The proposal is to replace strcat with strncat() to improve behaviour
when the bufffer overflows. So the premise is that the program is bugged
and the overflow is unintentional, and there is no ideal behaviour.
By calling strncat you force the program to truncate the string to fit
the buffer. Which in reality might often be the best thing to do,
because many strings are intended for human reading, and a human reader
can guess what has happened and supply the missing characters. But the
results are always wrong, they are defined to be wrong.
With undefined behaviour they are undefined (by C). But the two most
likely results are either than the buffer will in fact have room and the
program will works as intended, though it won't be correct, or that the
UB will be defined by the platform as to give a message that there has
been a memory access error. Which is the correct result, given the
constraint that the program itself is incorrect.

Occasionally, as in my example, string truncation can be catastrophic,
because it changes the string to a reasonable but incorrect meaning.

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

Re: Variadic functions

<EQCsN.250663$7sbb.34488@fx16.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.chmurka.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx16.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> <uopc7g$1f17i$2@dont-email.me> <uoqie3$1o25r$1@dont-email.me> <uou6op$2d0m5$1@dont-email.me> <87sf2l2od8.fsf@nosuchdomain.example.com> <uour9j$2gkc8$1@dont-email.me>
Lines: 53
Message-ID: <EQCsN.250663$7sbb.34488@fx16.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Fri, 26 Jan 2024 00:15:32 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Fri, 26 Jan 2024 00:15:32 GMT
X-Received-Bytes: 3565
 by: Scott Lurndal - Fri, 26 Jan 2024 00:15 UTC

Malcolm McLean <malcolm.arthur.mclean@gmail.com> writes:
>On 25/01/2024 19:40, Keith Thompson wrote:
>> 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.
>>
>The proposal is to replace strcat with strncat() to improve behaviour
>when the bufffer overflows. So the premise is that the program is bugged
>and the overflow is unintentional, and there is no ideal behaviour.
>By calling strncat you force the program to truncate the string to fit
>the buffer. Which in reality might often be the best thing to do,
>because many strings are intended for human reading, and a human reader
>can guess what has happened and supply the missing characters. But the
>results are always wrong, they are defined to be wrong.
>With undefined behaviour they are undefined (by C). But the two most
>likely results are either than the buffer will in fact have room and the
>program will works as intended, though it won't be correct, or that the
>UB will be defined by the platform as to give a message that there has
>been a memory access error.

Actually, it's highly _unlikely_ that an implementation would "give a
message that there has been a memory access error", unless the buffer access
crossed a page boundary and hit a not-present page. The majority
of cases, strcat would just corrupt whatever followed the buffer,
which often would be on the stack, where said corruption could lead
to exploits even leaving aside the UB whenever the corrupt data
is used.

strcat should not be used. strncat isn't a suitable substitute.

snprintf does work quite well as a substitute for strcat.

Re: Variadic functions

<20240125162416.84@kylheku.com>

  copy mid

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

  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: 433-929-6894@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: Variadic functions
Date: Fri, 26 Jan 2024 00:25:21 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <20240125162416.84@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>
<87sf2l2od8.fsf@nosuchdomain.example.com> <uour9j$2gkc8$1@dont-email.me>
Injection-Date: Fri, 26 Jan 2024 00:25:21 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="96ca4eb75b4703d7855e1ee0c9e2d747";
logging-data="2654202"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18wvZ+Y/8V4rvTrI8fjoMmbfE9H7z8sBPI="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:st6ruomRjLi29OyR0KPTWVtJNsI=
 by: Kaz Kylheku - Fri, 26 Jan 2024 00:25 UTC

On 2024-01-25, Malcolm McLean <malcolm.arthur.mclean@gmail.com> wrote:
> The proposal is to replace strcat with strncat() to improve behaviour
> when the bufffer overflows. So the premise is that the program is bugged
> and the overflow is unintentional, and there is no ideal behaviour.
> By calling strncat you force the program to truncate the string to fit
> the buffer. Which in reality might often be the best thing to do,
> because many strings are intended for human reading, and a human reader
> can guess what has happened and supply the missing characters. But the
> results are always wrong, they are defined to be wrong.

Yes; you have to pick one behavior for the function and ... USE
SOMETHING ELSE for the other behaviors. Wow, eh?

(Or have ioctl-like codes to select a behavior in one function,
which is really like multipel functions in one.)

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

Re: Variadic functions

<uov56c$2hrf9$1@dont-email.me>

  copy mid

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

  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: malcolm.arthur.mclean@gmail.com (Malcolm McLean)
Newsgroups: comp.lang.c
Subject: Re: Variadic functions
Date: Fri, 26 Jan 2024 02:25:16 +0000
Organization: A noiseless patient Spider
Lines: 34
Message-ID: <uov56c$2hrf9$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>
<87sf2l2od8.fsf@nosuchdomain.example.com> <uour9j$2gkc8$1@dont-email.me>
<20240125162416.84@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 26 Jan 2024 02:25:16 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="158c40369e186346709d93477c442c64";
logging-data="2682345"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Wo3eONQhNHuu+zDh5GsDLGRDm5KRdBCI="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:yVptK2aYr2O3+Xh4conRf0wz5Fs=
In-Reply-To: <20240125162416.84@kylheku.com>
Content-Language: en-GB
 by: Malcolm McLean - Fri, 26 Jan 2024 02:25 UTC

On 26/01/2024 00:25, Kaz Kylheku wrote:
> On 2024-01-25, Malcolm McLean <malcolm.arthur.mclean@gmail.com> wrote:
>> The proposal is to replace strcat with strncat() to improve behaviour
>> when the bufffer overflows. So the premise is that the program is bugged
>> and the overflow is unintentional, and there is no ideal behaviour.
>> By calling strncat you force the program to truncate the string to fit
>> the buffer. Which in reality might often be the best thing to do,
>> because many strings are intended for human reading, and a human reader
>> can guess what has happened and supply the missing characters. But the
>> results are always wrong, they are defined to be wrong.
>
> Yes; you have to pick one behavior for the function and ... USE
> SOMETHING ELSE for the other behaviors. Wow, eh?
>
> (Or have ioctl-like codes to select a behavior in one function,
> which is really like multipel functions in one.)
>
You don't seem to understand that David Brown's premise was that the
program was bugged. And that's entirely reasonable. Programs do ship
with bugs. The question is whether there are any strategies we can use
to limit the damage.

And strncat rather than strcat is one such strategy. And David Brown is
probably right that, in many cases, it will be a good strategy. But I
was pointing out that this won't always hold.

But there is no way of making an incorrect program correct without
correcting the error. It's not so much a case of picking a behaviour, as
choosing what is likely to be the least damaging wrong behaviour.

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

Re: strings (was Re: Variadic functions)

<uovdju$2mo85$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.samoylyk.net!newsfeed.xs3.de!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: strings (was Re: Variadic functions)
Date: Fri, 26 Jan 2024 04:49:03 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 13
Message-ID: <uovdju$2mo85$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>
<87sf2l2od8.fsf@nosuchdomain.example.com> <uour9j$2gkc8$1@dont-email.me>
<EQCsN.250663$7sbb.34488@fx16.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 26 Jan 2024 04:49:03 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e3e30b5c11bd53f646c3b65395233311";
logging-data="2842885"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19A1/bx9yvFUiWVSUV65mdZ"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:bXCJ+utoCUiMJz92vLh7tOTOt3I=
 by: Lawrence D'Oliv - Fri, 26 Jan 2024 04:49 UTC

On Fri, 26 Jan 2024 00:15:32 GMT, Scott Lurndal wrote:

> strcat should not be used. strncat isn't a suitable substitute.

The BSDs tried to come up with some alternatives, e.g. strlcat(3).

See also the code for stpecpy here
<https://manpages.debian.org/7/string_copying.en.html>.

> snprintf does work quite well as a substitute for strcat.

I’m assuming you’re not suggesting passing an arbitrary string (e.g. user-
entered) for the format ...

Re: Variadic functions

<up0i59$2tm6a$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.chmurka.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: Fri, 26 Jan 2024 16:12:40 +0100
Organization: A noiseless patient Spider
Lines: 77
Message-ID: <up0i59$2tm6a$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>
<87sf2l2od8.fsf@nosuchdomain.example.com> <uour9j$2gkc8$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 26 Jan 2024 15:12:41 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="5275502989ed94a21b07f0e0ca598467";
logging-data="3070154"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+G1eaDYGApZ7EgTLfB5LY1bD7GIIcsHjY="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:0jRQ8aj+Z15YWOKeKPF4Ksm8Sbw=
Content-Language: en-GB
In-Reply-To: <uour9j$2gkc8$1@dont-email.me>
 by: David Brown - Fri, 26 Jan 2024 15:12 UTC

On 26/01/2024 00:36, Malcolm McLean wrote:
> On 25/01/2024 19:40, Keith Thompson wrote:
>> 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.
>>
> The proposal is to replace strcat with strncat() to improve behaviour
> when the bufffer overflows.

Yes.

> So the premise is that the program is bugged
> and the overflow is unintentional,

Yes.

> and there is no ideal behaviour.

Incorrect.

You started out okay, but then drew an unwarranted conclusion from your
premises.

Buffer overflow means the original "strcat" program had a bug if it was
expected to deal correctly with longer strings - longer strings caused
UB, and that is never correct execution.

Let's say the program is trying to concatenate two strings into a 10
character buffer. There are, roughly, three was to specify this :

1. It should give the concatenation for any two strings whose total
length does not exceed 9 characters.

2. It should give the concatenation for any two strings.

3. It should up to the first 9 characters of the hypothetical unlimited
length concatenation of any two strings.

If the specification is 1, then the "strcat" version is fine - it is
ideal behaviour. Garbage in, garbage out - programs are only required
to be used according to their specifications.

If the specification is 2, then it is impossible to write a program with
the required behaviour.

If the specification is 3, then strncat will give the ideal behaviour.

The "ideal behaviour" is dependent on the specification for the program,
and that has not been given here. Without the specification or
requirements, it makes no sense to talk about whether there is or is not
an "ideal behaviour", whether it is possible or not, or whether any
given program fulfils it.

Re: Variadic functions

<up0ikl$2tm6a$2@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.chmurka.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: Fri, 26 Jan 2024 16:20:53 +0100
Organization: A noiseless patient Spider
Lines: 26
Message-ID: <up0ikl$2tm6a$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>
<87sf2l2od8.fsf@nosuchdomain.example.com> <uour9j$2gkc8$1@dont-email.me>
<EQCsN.250663$7sbb.34488@fx16.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 26 Jan 2024 15:20:53 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="5275502989ed94a21b07f0e0ca598467";
logging-data="3070154"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/hIUpofCssLdM+wTni6Hlyd4VOHTFPcnk="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:zMlF6SixQg4YwTSC2gsZod74Hgo=
In-Reply-To: <EQCsN.250663$7sbb.34488@fx16.iad>
Content-Language: en-GB
 by: David Brown - Fri, 26 Jan 2024 15:20 UTC

On 26/01/2024 01:15, Scott Lurndal wrote:

> strcat should not be used. strncat isn't a suitable substitute.
>

Either can be used, if you know exactly what they do and they are
suitable for the task.

> snprintf does work quite well as a substitute for strcat.
>

It is a completely useless substitute in some situations - it has
massively greater overhead, rendering it unusable in situations where
efficiency is important.

There is no single "right" answer here, nor are there "always wrong"
answers. "stcat should not be used" is as wrong a generalisation as
"strcat is always fine". Understand the functions, and use them
appropriately - just like any other programming task.

I am slightly surprised to be writing this in reply to a post by you -
you are not exactly a newbie to programming, or to understanding that
sometimes efficiency is critical, or to understanding that it's safe to
use a function if and only if you have ensured it is safe to use that
function.

Re: Variadic functions

<up0int$2tm6a$3@dont-email.me>

  copy mid

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

  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: Fri, 26 Jan 2024 16:22:37 +0100
Organization: A noiseless patient Spider
Lines: 33
Message-ID: <up0int$2tm6a$3@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> <uot7cr$284a5$1@dont-email.me>
<87h6j144u9.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 26 Jan 2024 15:22:37 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="5275502989ed94a21b07f0e0ca598467";
logging-data="3070154"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+jaRwQ/5uymWh0WCnYBREF2W80PD9nr6k="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:pUDKWypllQM3iajCxQ3GO4VqNE4=
Content-Language: en-GB
In-Reply-To: <87h6j144u9.fsf@nosuchdomain.example.com>
 by: David Brown - Fri, 26 Jan 2024 15:22 UTC

On 25/01/2024 19:59, Keith Thompson wrote:
> 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().
>

I would go for a longer name - perhaps strncpy_padded(). Other than
that, I agree with you.

Re: Variadic functions

<up0j3g$2tm6a$4@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.hispagatos.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: Fri, 26 Jan 2024 16:28:48 +0100
Organization: A noiseless patient Spider
Lines: 38
Message-ID: <up0j3g$2tm6a$4@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>
<pan$ec0b$562b777f$6399b026$ea94f9f2@invalid.invalid>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 26 Jan 2024 15:28:48 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="5275502989ed94a21b07f0e0ca598467";
logging-data="3070154"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19IZrf/IUtV02LihPcj1cjTEC2Lwa/PwN8="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:D/+ehjAqjb+0lfhttj3XbDfkPpo=
Content-Language: en-GB
In-Reply-To: <pan$ec0b$562b777f$6399b026$ea94f9f2@invalid.invalid>
 by: David Brown - Fri, 26 Jan 2024 15:28 UTC

On 25/01/2024 23:46, Blue-Maned_Hawk wrote:
> David Brown 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.
>
> sizeof outbuf - strlen(outbuf)
>

sizeof outbuf - strlen(outbuf) - 1

strncat adds a \0 to the end, after copying up to n characters.

And you have to check this to make sure it is not less than zero -
remembering that all the types here are "size_t" and thus unsigned -
before deciding if you can call strncat at all.

As I said, nasty.

And for a naïve use, you are scanning through the string currently
"outbuf" to find its current length, then calling strncat on it which
will re-scan the same string again to find the end point for starting
the copy. And it will uselessly return the pointer to "outbuf", so that
if you want to find the length of the combined string (perhaps for
concatenating another string), you have to re-scan the whole thing again.

Nasty and inefficient.

Re: Variadic functions

<up24qi$393mt$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!paganini.bofh.team!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: Sat, 27 Jan 2024 05:37:21 +0000
Organization: A noiseless patient Spider
Lines: 105
Message-ID: <up24qi$393mt$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>
<87sf2l2od8.fsf@nosuchdomain.example.com> <uour9j$2gkc8$1@dont-email.me>
<up0i59$2tm6a$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 27 Jan 2024 05:37:23 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="df6d7b3d59a4ae5b2ee79a0cc303e2d6";
logging-data="3444445"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Q7i1yK04vfeQ8u4YE/7g0sLCUvKbCYa8="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:BNH2Ul5d/m3jSLsiC/tvXmmeTuo=
Content-Language: en-GB
In-Reply-To: <up0i59$2tm6a$1@dont-email.me>
 by: Malcolm McLean - Sat, 27 Jan 2024 05:37 UTC

On 26/01/2024 15:12, David Brown wrote:
> On 26/01/2024 00:36, Malcolm McLean wrote:
>> On 25/01/2024 19:40, Keith Thompson wrote:
>>> 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.
>>>
>> The proposal is to replace strcat with strncat() to improve behaviour
>> when the bufffer overflows.
>
> Yes.
>
>> So the premise is that the program is bugged and the overflow is
>> unintentional,
>
> Yes.
>
>> and there is no ideal behaviour.
>
> Incorrect.
>
> You started out okay, but then drew an unwarranted conclusion from your
> premises.
>
> Buffer overflow means the original "strcat" program had a bug if it was
> expected to deal correctly with longer strings - longer strings caused
> UB, and that is never correct execution.
>
> Let's say the program is trying to concatenate two strings into a 10
> character buffer.  There are, roughly, three was to specify this :
>
> 1. It should give the concatenation for any two strings whose total
> length does not exceed 9 characters.
>
> 2. It should give the concatenation for any two strings.
>
> 3. It should up to the first 9 characters of the hypothetical unlimited
> length concatenation of any two strings.
>
> If the specification is 1, then the "strcat" version is fine - it is
> ideal behaviour.  Garbage in, garbage out - programs are only required
> to be used according to their specifications.
>
> If the specification is 2, then it is impossible to write a program with
> the required behaviour.
>
> If the specification is 3, then strncat will give the ideal behaviour.
>
>
> The "ideal behaviour" is dependent on the specification for the program,
> and that has not been given here.  Without the specification or
> requirements, it makes no sense to talk about whether there is or is not
> an "ideal behaviour", whether it is possible or not, or whether any
> given program fulfils it.
>
>
No. The program is trying to concatentate "strcat " and "strncat" into a
ten byte buffer, and the specification is that the buffer shall hold
"strcat strncat". But since that is more than ten bytes that is
impossible, the specification cannot be met and therefore the program is
bugged.
Now of course you can say "we can only deal with ten characters,
threefore the specification is that the program should output "strcat
st". The user will figure it out." Which he may well do and that would
be fair enough. And now you have no bug.

But often that wouldn't be acceptable as plan. The program is bugged and
it will need to be fixed. So the question is, what is the least bad
thing we can do. And instead of specifications, we have stratgems. Throw
out the contents of the buffer and report some sort of error message.
May or may not be acceptable. Truncate and hope that the truncated
message is not too catastrophic. Or push the whole message past the end
of the buffer, cross your fingers, and hope. And there's quite a bit to
be said for the last strategy.

A bugged program is incorrect and cannot be made correct by declaring
defined but wrong behaviour to be correct, unless it is genuinely the
case that, in the act of so declaring, we make it correct.

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

Re: Variadic functions

<up34s6$3dpki$1@dont-email.me>

  copy mid

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

  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: Sat, 27 Jan 2024 15:44:22 +0100
Organization: A noiseless patient Spider
Lines: 106
Message-ID: <up34s6$3dpki$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>
<87sf2l2od8.fsf@nosuchdomain.example.com> <uour9j$2gkc8$1@dont-email.me>
<up0i59$2tm6a$1@dont-email.me> <up24qi$393mt$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 27 Jan 2024 14:44:23 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="2eeb3bda41f88f1d87610b9eee2a7cea";
logging-data="3597970"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+MtVOE9+fKEh1TnlUh6JOU3I49PqdhIlw="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:27NJ5EcFGsqXM/y704F22zHfvIs=
In-Reply-To: <up24qi$393mt$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Sat, 27 Jan 2024 14:44 UTC

On 27/01/2024 06:37, Malcolm McLean wrote:
> On 26/01/2024 15:12, David Brown wrote:
>> On 26/01/2024 00:36, Malcolm McLean wrote:
>>> On 25/01/2024 19:40, Keith Thompson wrote:
>>>> 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.
>>>>
>>> The proposal is to replace strcat with strncat() to improve behaviour
>>> when the bufffer overflows.
>>
>> Yes.
>>
>>> So the premise is that the program is bugged and the overflow is
>>> unintentional,
>>
>> Yes.
>>
>>> and there is no ideal behaviour.
>>
>> Incorrect.
>>
>> You started out okay, but then drew an unwarranted conclusion from
>> your premises.
>>
>> Buffer overflow means the original "strcat" program had a bug if it
>> was expected to deal correctly with longer strings - longer strings
>> caused UB, and that is never correct execution.
>>
>> Let's say the program is trying to concatenate two strings into a 10
>> character buffer.  There are, roughly, three was to specify this :
>>
>> 1. It should give the concatenation for any two strings whose total
>> length does not exceed 9 characters.
>>
>> 2. It should give the concatenation for any two strings.
>>
>> 3. It should up to the first 9 characters of the hypothetical
>> unlimited length concatenation of any two strings.
>>
>> If the specification is 1, then the "strcat" version is fine - it is
>> ideal behaviour.  Garbage in, garbage out - programs are only required
>> to be used according to their specifications.
>>
>> If the specification is 2, then it is impossible to write a program
>> with the required behaviour.
>>
>> If the specification is 3, then strncat will give the ideal behaviour.
>>
>>
>> The "ideal behaviour" is dependent on the specification for the
>> program, and that has not been given here.  Without the specification
>> or requirements, it makes no sense to talk about whether there is or
>> is not an "ideal behaviour", whether it is possible or not, or whether
>> any given program fulfils it.
>>
>>
> No. The program is trying to concatentate "strcat " and "strncat" into a
> ten byte buffer, and the specification is that the buffer shall hold
> "strcat strncat".

Says who?

Why do /you/ get to decide details of what the program is supposed to
do? You can't just pick a specification after the fact as an attempt to
justify your claims that the program is buggy!

> But since that is more than ten bytes that is
> impossible, the specification cannot be met and therefore the program is
> bugged.

No. I the specification is impossible to meet, then the specification
is at fault, not the program.

Whether a program with strcat, or a program with strncat, is correct or
not depends on the specifications for the program. The specifications
were never given. So all we can do is look at what might or might not
be correct for various plausible choices of specifications. We cannot
conclude that a particular implementation is buggy, nor can we conclude
that it is correct.

Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)

<up3q66$3hbk7$5@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.niel.me!news.gegeweb.eu!gegeweb.org!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: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)
Date: Sat, 27 Jan 2024 20:48:06 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 9
Message-ID: <up3q66$3hbk7$5@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>
<87sf2l2od8.fsf@nosuchdomain.example.com> <uour9j$2gkc8$1@dont-email.me>
<up0i59$2tm6a$1@dont-email.me> <up24qi$393mt$1@dont-email.me>
<up34s6$3dpki$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 27 Jan 2024 20:48:06 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="a9befbf67dfd56f15a347bec2826e976";
logging-data="3714695"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+NIvtn54NaWA8h0TQEG/Gt"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:ReTyTFQXOl99bQZatrh+RU9suy8=
 by: Lawrence D'Oliv - Sat, 27 Jan 2024 20:48 UTC

On Sat, 27 Jan 2024 15:44:22 +0100, David Brown wrote:

> Whether a program with strcat, or a program with strncat, is correct or
> not depends on the specifications for the program.

Can a program that uses strcat be “correct”? In theory, yes. In practice,
code that uses such misbegotten functions has a high probability of having
associated security vulnerabilities like buffer overflows in it. That’s
why we avoid same.

Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)

<up3rcc$3hjng$2@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!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: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)
Date: Sat, 27 Jan 2024 13:08:28 -0800
Organization: A noiseless patient Spider
Lines: 22
Message-ID: <up3rcc$3hjng$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>
<87sf2l2od8.fsf@nosuchdomain.example.com> <uour9j$2gkc8$1@dont-email.me>
<up0i59$2tm6a$1@dont-email.me> <up24qi$393mt$1@dont-email.me>
<up34s6$3dpki$1@dont-email.me> <up3q66$3hbk7$5@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 27 Jan 2024 21:08:28 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="4de68a687a399c61b3fcd74392800b9b";
logging-data="3722992"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Uby0gnuOo8ITQiIPbwA9waPCEFjqg6+E="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:qPprzjGo5Vk4yGKPyJK1q8IR7Gk=
Content-Language: en-US
In-Reply-To: <up3q66$3hbk7$5@dont-email.me>
 by: Chris M. Thomasson - Sat, 27 Jan 2024 21:08 UTC

On 1/27/2024 12:48 PM, Lawrence D'Oliveiro wrote:
> On Sat, 27 Jan 2024 15:44:22 +0100, David Brown wrote:
>
>> Whether a program with strcat, or a program with strncat, is correct or
>> not depends on the specifications for the program.
>
> Can a program that uses strcat be “correct”?

Indeed! For sure.

> In theory, yes. In practice,
> code that uses such misbegotten functions has a high probability of having
> associated security vulnerabilities like buffer overflows in it. That’s
> why we avoid same.

Well, if you cannot use it correctly, perhaps, well, don't use it until
you learn how?

Patient: it hurts when I do that...

Humm...

Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)

<up3rqa$3hnls$1@dont-email.me>

  copy mid

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

  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: bc@freeuk.com (bart)
Newsgroups: comp.lang.c
Subject: Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)
Date: Sat, 27 Jan 2024 21:15:54 +0000
Organization: A noiseless patient Spider
Lines: 18
Message-ID: <up3rqa$3hnls$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>
<87sf2l2od8.fsf@nosuchdomain.example.com> <uour9j$2gkc8$1@dont-email.me>
<up0i59$2tm6a$1@dont-email.me> <up24qi$393mt$1@dont-email.me>
<up34s6$3dpki$1@dont-email.me> <up3q66$3hbk7$5@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 27 Jan 2024 21:15:54 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="57a0e44d94d7e92b3900d2671e8a2518";
logging-data="3727036"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18aUAm+RV2mf+Fjj9S50N/JqgAcdY2lmbA="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:xjzLQtBhQOwjabg5my9TL7remvU=
Content-Language: en-GB
In-Reply-To: <up3q66$3hbk7$5@dont-email.me>
 by: bart - Sat, 27 Jan 2024 21:15 UTC

On 27/01/2024 20:48, Lawrence D'Oliveiro wrote:
> On Sat, 27 Jan 2024 15:44:22 +0100, David Brown wrote:
>
>> Whether a program with strcat, or a program with strncat, is correct or
>> not depends on the specifications for the program.
>
> Can a program that uses strcat be “correct”? In theory, yes. In practice,
> code that uses such misbegotten functions has a high probability of having
> associated security vulnerabilities like buffer overflows in it. That’s
> why we avoid same.

So how would you write such a function in a safe manner?

At some point you will need to trust the parameters that the caller
provides. Then that is no different to an implementation of strcat.

BTW you seem to like Python. Cpython uses strcpy() quite a bit, and also
strcat(). Does that make CPython unsafe?

Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)

<up415i$j6sp$1@news.xmission.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.swapon.de!csiph.com!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gazelle@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.lang.c
Subject: Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)
Date: Sat, 27 Jan 2024 22:47:14 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <up415i$j6sp$1@news.xmission.com>
References: <uoed9m$387sh$1@dont-email.me> <up24qi$393mt$1@dont-email.me> <up34s6$3dpki$1@dont-email.me> <up3q66$3hbk7$5@dont-email.me>
Injection-Date: Sat, 27 Jan 2024 22:47:14 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="629657"; 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 - Sat, 27 Jan 2024 22:47 UTC

In article <up3q66$3hbk7$5@dont-email.me>,
Lawrence D'Oliveiro <ldo@nz.invalid> wrote:
>On Sat, 27 Jan 2024 15:44:22 +0100, David Brown wrote:
>
>> Whether a program with strcat, or a program with strncat, is correct or
>> not depends on the specifications for the program.
>
>Can a program that uses strcat be correct? In theory, yes. In practice,
>code that uses such misbegotten functions has a high probability of having
>associated security vulnerabilities like buffer overflows in it. Thats
>why we avoid same.

I'm very happy for you.

--
"Only a genius could lose a billion dollars running a casino."
"You know what they say: the house always loses."
"When life gives you lemons, don't pay taxes."
"Grab 'em by the p***y!"

Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)

<up44aq$3io8t$4@dont-email.me>

  copy mid

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

  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: ldo@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.lang.c
Subject: Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)
Date: Sat, 27 Jan 2024 23:41:14 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 14
Message-ID: <up44aq$3io8t$4@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>
<87sf2l2od8.fsf@nosuchdomain.example.com> <uour9j$2gkc8$1@dont-email.me>
<up0i59$2tm6a$1@dont-email.me> <up24qi$393mt$1@dont-email.me>
<up34s6$3dpki$1@dont-email.me> <up3q66$3hbk7$5@dont-email.me>
<up3rqa$3hnls$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 27 Jan 2024 23:41:14 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="aa3bd129a7fc4eefdc68e712f3fbcc7a";
logging-data="3760413"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+BECFzc+bRE4CkimPik9JK"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:H275+fKInBHLHSYO57bfVclP8lA=
 by: Lawrence D'Oliv - Sat, 27 Jan 2024 23:41 UTC

On Sat, 27 Jan 2024 21:15:54 +0000, bart wrote:

> On 27/01/2024 20:48, Lawrence D'Oliveiro wrote:
>
>> Can a program that uses strcat be “correct”? In theory, yes. In practice,
>> code that uses such misbegotten functions has a high probability of having
>> associated security vulnerabilities like buffer overflows in it. That’s
>> why we avoid same.
>
> So how would you write such a function in a safe manner?

Don’t. Use an alternative function instead.

<https://manpages.debian.org/7/string_copying.en.html>

Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)

<up44qm$3j2ov$1@dont-email.me>

  copy mid

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

  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: bc@freeuk.com (bart)
Newsgroups: comp.lang.c
Subject: Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)
Date: Sat, 27 Jan 2024 23:49:42 +0000
Organization: A noiseless patient Spider
Lines: 24
Message-ID: <up44qm$3j2ov$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>
<87sf2l2od8.fsf@nosuchdomain.example.com> <uour9j$2gkc8$1@dont-email.me>
<up0i59$2tm6a$1@dont-email.me> <up24qi$393mt$1@dont-email.me>
<up34s6$3dpki$1@dont-email.me> <up3q66$3hbk7$5@dont-email.me>
<up3rqa$3hnls$1@dont-email.me> <up44aq$3io8t$4@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 27 Jan 2024 23:49:42 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="045dc6465ca7ac2f93242225a5137cab";
logging-data="3771167"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18/7d6RamEasDWzTN2XB/qcNFuftqUMntc="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:HufyBuR3QF7mtb4+vpGyTCss96w=
In-Reply-To: <up44aq$3io8t$4@dont-email.me>
Content-Language: en-GB
 by: bart - Sat, 27 Jan 2024 23:49 UTC

On 27/01/2024 23:41, Lawrence D'Oliveiro wrote:
> On Sat, 27 Jan 2024 21:15:54 +0000, bart wrote:
>
>> On 27/01/2024 20:48, Lawrence D'Oliveiro wrote:
>>
>>> Can a program that uses strcat be “correct”? In theory, yes. In practice,
>>> code that uses such misbegotten functions has a high probability of having
>>> associated security vulnerabilities like buffer overflows in it. That’s
>>> why we avoid same.
>>
>> So how would you write such a function in a safe manner?
>
> Don’t. Use an alternative function instead.
>
> <https://manpages.debian.org/7/string_copying.en.html>

Which one from that page? It shows strcat and strlcat.

The latter contains a size. But you have to trust the size is correct.

This is little different from trusting the caller to not provide a
source string that is longer than the remaining capacity of the destination.

At some point, you have to trust something.

Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)

<up461r$3j7tk$1@dont-email.me>

  copy mid

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

  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: ldo@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.lang.c
Subject: Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)
Date: Sun, 28 Jan 2024 00:10:35 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 5
Message-ID: <up461r$3j7tk$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>
<87sf2l2od8.fsf@nosuchdomain.example.com> <uour9j$2gkc8$1@dont-email.me>
<up0i59$2tm6a$1@dont-email.me> <up24qi$393mt$1@dont-email.me>
<up34s6$3dpki$1@dont-email.me> <up3q66$3hbk7$5@dont-email.me>
<up3rqa$3hnls$1@dont-email.me> <up44aq$3io8t$4@dont-email.me>
<up44qm$3j2ov$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 28 Jan 2024 00:10:35 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="aa3bd129a7fc4eefdc68e712f3fbcc7a";
logging-data="3776436"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/9M6nrxtP+MCErEEfcvNKA"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:Uy85oNaj9dnMfibFu1seM7RxIo8=
 by: Lawrence D'Oliv - Sun, 28 Jan 2024 00:10 UTC

On Sat, 27 Jan 2024 23:49:42 +0000, bart wrote:

> The latter contains a size. But you have to trust the size is correct.

The point being, you can specify one.

Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)

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

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.nntp4.net!news.hispagatos.org!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: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)
Date: Sat, 27 Jan 2024 17:05:27 -0800
Organization: None to speak of
Lines: 28
Message-ID: <87o7d61d48.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>
<87sf2l2od8.fsf@nosuchdomain.example.com>
<uour9j$2gkc8$1@dont-email.me> <up0i59$2tm6a$1@dont-email.me>
<up24qi$393mt$1@dont-email.me> <up34s6$3dpki$1@dont-email.me>
<up3q66$3hbk7$5@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="8434623738c8ca97fb8c62709c865b07";
logging-data="3789905"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19T8RMOfv50ygug1vGL7OAV"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:gLSz6nLf0Z5QFfxnpj6EKEZoBx0=
sha1:OXUpWg3TikOX2o4bID/Iv/r36q4=
 by: Keith Thompson - Sun, 28 Jan 2024 01:05 UTC

Lawrence D'Oliveiro <ldo@nz.invalid> writes:
> On Sat, 27 Jan 2024 15:44:22 +0100, David Brown wrote:
>> Whether a program with strcat, or a program with strncat, is correct or
>> not depends on the specifications for the program.
>
> Can a program that uses strcat be “correct”? In theory, yes. In practice,
> code that uses such misbegotten functions has a high probability of having
> associated security vulnerabilities like buffer overflows in it. That’s
> why we avoid same.

In theory, yes. In practice, also yes.

char s[20];
strcpy(s, "foo");
strcat(s, "bar");

That's perfectly safe. Yes, you can introduce a buffer overflow if you
modify the code carelessly. So don't do that. And yes, strcpy() and
strcat() certainly can be used unsafely. Nobody has denied that.

Similarly, adding two int value can cause undefined behavior if the
result overflows. Do you suggest using only bound-checking addition
operators (I think C23 is adding them to the standard library)?

--
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: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)

<up49tp$3jopf$3@dont-email.me>

  copy mid

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

  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: ldo@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.lang.c
Subject: Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)
Date: Sun, 28 Jan 2024 01:16:42 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 7
Message-ID: <up49tp$3jopf$3@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>
<87sf2l2od8.fsf@nosuchdomain.example.com> <uour9j$2gkc8$1@dont-email.me>
<up0i59$2tm6a$1@dont-email.me> <up24qi$393mt$1@dont-email.me>
<up34s6$3dpki$1@dont-email.me> <up3q66$3hbk7$5@dont-email.me>
<87o7d61d48.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 28 Jan 2024 01:16:42 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="aa3bd129a7fc4eefdc68e712f3fbcc7a";
logging-data="3793711"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/GFGVar1rG3sgcJNne2SZQ"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:ajDqepXlbj5h6reKH9M2CX9ffKs=
 by: Lawrence D'Oliv - Sun, 28 Jan 2024 01:16 UTC

On Sat, 27 Jan 2024 17:05:27 -0800, Keith Thompson wrote:

> Yes, you can introduce a buffer overflow if you
> modify the code carelessly. So don't do that.

Adding an explicit buffer size provides an additional check. It helps
reduce the incidence of such “carelessness”. Not completely, but it helps.

Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)

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

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.1d4.us!usenet.goja.nl.eu.org!paganini.bofh.team!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: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)
Date: Sat, 27 Jan 2024 18:00:08 -0800
Organization: None to speak of
Lines: 32
Message-ID: <87y1caz07r.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>
<87sf2l2od8.fsf@nosuchdomain.example.com>
<uour9j$2gkc8$1@dont-email.me> <up0i59$2tm6a$1@dont-email.me>
<up24qi$393mt$1@dont-email.me> <up34s6$3dpki$1@dont-email.me>
<up3q66$3hbk7$5@dont-email.me>
<87o7d61d48.fsf@nosuchdomain.example.com>
<up49tp$3jopf$3@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="8434623738c8ca97fb8c62709c865b07";
logging-data="3795946"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18zR0hnTEgNgX+3cdlDRCrr"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:RObnNNKhlDENwjUCTJ1DvWHW4lM=
sha1:KAEFdSIXsoG06oJHYqu9jcnTado=
 by: Keith Thompson - Sun, 28 Jan 2024 02:00 UTC

Lawrence D'Oliveiro <ldo@nz.invalid> writes:
> On Sat, 27 Jan 2024 17:05:27 -0800, Keith Thompson wrote:
>> Yes, you can introduce a buffer overflow if you
>> modify the code carelessly. So don't do that.
>
> Adding an explicit buffer size provides an additional check. It helps
> reduce the incidence of such “carelessness”. Not completely, but it helps.

Sure. It also requires extra work, and introduce more opportunities for
errors if you specify the size incorrectly.

There are languages that handle this kind of thing differently, where
the size is inherently associated with each array or string object or
value, and therefore the programmer doesn't have to specify it at all.
C is not one of those languages.

You seemed to be implying that strcpy() and strcat() cannot ever be used
safely, and should not ever be used at all. It was that extreme
statement that I was trying to refute. Of course using strcpy() or
strcat() with user-provided input without checking:

char buf[80]
strcpy(buf, argv[1]);
strcat(buf, argv[2]);

is dangerous. Just as 1+1 is perfectly safe, but n+1 is potentially
dangerous if n might be equal to INT_MAX.

--
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: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)

<up4d3l$3k6su$1@dont-email.me>

  copy mid

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

  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: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)
Date: Sun, 28 Jan 2024 02:11:02 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 14
Message-ID: <up4d3l$3k6su$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>
<87sf2l2od8.fsf@nosuchdomain.example.com> <uour9j$2gkc8$1@dont-email.me>
<up0i59$2tm6a$1@dont-email.me> <up24qi$393mt$1@dont-email.me>
<up34s6$3dpki$1@dont-email.me> <up3q66$3hbk7$5@dont-email.me>
<87o7d61d48.fsf@nosuchdomain.example.com> <up49tp$3jopf$3@dont-email.me>
<87y1caz07r.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 28 Jan 2024 02:11:02 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="aa3bd129a7fc4eefdc68e712f3fbcc7a";
logging-data="3808158"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX186rnVQ2fBB/qatTUdlVkAr"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:riCYGZJQyVGcCt1ux7evQRWEf8o=
 by: Lawrence D'Oliv - Sun, 28 Jan 2024 02:11 UTC

On Sat, 27 Jan 2024 18:00:08 -0800, Keith Thompson wrote:

> Lawrence D'Oliveiro <ldo@nz.invalid> writes:
>> On Sat, 27 Jan 2024 17:05:27 -0800, Keith Thompson wrote:
>>> Yes, you can introduce a buffer overflow if you modify the code
>>> carelessly. So don't do that.
>>
>> Adding an explicit buffer size provides an additional check. It helps
>> reduce the incidence of such “carelessness”. Not completely, but it
>> helps.
>
> Sure. It also requires extra work ...

Macros can help with that.

Re: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)

<up5dqf$3t99q$1@dont-email.me>

  copy mid

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

  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: Bad String Functions; Bad Bad Bad (was Re: Variadic functions)
Date: Sun, 28 Jan 2024 12:29:18 +0100
Organization: A noiseless patient Spider
Lines: 44
Message-ID: <up5dqf$3t99q$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>
<87sf2l2od8.fsf@nosuchdomain.example.com> <uour9j$2gkc8$1@dont-email.me>
<up0i59$2tm6a$1@dont-email.me> <up24qi$393mt$1@dont-email.me>
<up34s6$3dpki$1@dont-email.me> <up3q66$3hbk7$5@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 28 Jan 2024 11:29:19 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f1cb5a483529a30a7360981bcf31d1cc";
logging-data="4105530"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1832zbV3Hz8Gn7diABQ9rc4AAbjljnKhWk="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:x6gp7WlyQPPSHak2Atg7hRyYdy8=
In-Reply-To: <up3q66$3hbk7$5@dont-email.me>
Content-Language: en-GB
 by: David Brown - Sun, 28 Jan 2024 11:29 UTC

On 27/01/2024 21:48, Lawrence D'Oliveiro wrote:
> On Sat, 27 Jan 2024 15:44:22 +0100, David Brown wrote:
>
>> Whether a program with strcat, or a program with strncat, is correct or
>> not depends on the specifications for the program.
>
> Can a program that uses strcat be “correct”? In theory, yes. In practice,
> code that uses such misbegotten functions has a high probability of having
> associated security vulnerabilities like buffer overflows in it. That’s
> why we avoid same.

In theory, yes. In practice, yes.

The principle is the same for every program. If you have unknown input
from the outside, sanitize it so that you know enough about it to be
able to use it the way you want. If you know it is safe to use it in a
particular way, you can use it that way without introducing bugs.

Any program, function, or set of functions needs a specification (even
if it is informal). This gives the restrictions on the inputs for
anything using the code, and says what the output will be as long as
these restrictions are fulfilled. (It might also specify some error
detection for "bad" inputs, but that's really just an extension to the
specification of inputs and outputs.)

Sometimes a function will be specified for all possible values of its
input types. Sometimes it won't. (And sometimes details are hidden by
the language and implementation - the object code for a function
typically gets its inputs in registers or stack entries, and they may be
able to hold bit patterns that don't match any value of the logical type.)

The implementation of a function can, and should, assume that the input
values are within specification. Any incorrect input falls under
"garbage in, garbage out" - a principle that has been established in the
computing world since the first mechanical computers, and which extends
throughout countless aspects of life.

If the function uses strcat in a way that is safe and correct for inputs
as specified for the function, then it is safe and correct to use strcat.

If you have an alternative way to implement the function, that could be
just as good. You might have a way to do it more efficiently. But that
does not mean strcat is bad.


devel / comp.lang.c / Re: Variadic functions

Pages:12345
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor