Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

"You are WRONG, you ol' brass-breasted fascist poop!" -- Bloom County


devel / comp.lang.c / What Is Wrong With This Function?

SubjectAuthor
* What Is Wrong With This Function?Lawrence D'Oliveiro
+- Re: What Is Wrong With This Function?Ben Bacarisse
`* Re: What Is Wrong With This Function?Barry Schwarz
 `* Re: What Is Wrong With This Function?Keith Thompson
  `- Re: What Is Wrong With This Function?James Kuyper

1
What Is Wrong With This Function?

<urdvk2$1f1dt$1@dont-email.me>

  copy mid

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

  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: What Is Wrong With This Function?
Date: Sat, 24 Feb 2024 23:54:42 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 12
Message-ID: <urdvk2$1f1dt$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 24 Feb 2024 23:54:42 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="5d3a24d1dc8cf78704609d9d7dcc21b3";
logging-data="1541565"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+8Fp7PuYN0d2bGDRQ48Qs3"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:1l1WRy7S2m/BfvhRmwUDGwvlSUE=
 by: Lawrence D'Oliv - Sat, 24 Feb 2024 23:54 UTC

I think it was Dennis Ritchie who pointed out what a fudge this was:

char *strstr(const char *haystack, const char *needle);

ANSI C introduced the “const” modifier, and wanted to use it for the
arguments to strstr. But if the result is a pointer into the
“haystack” string, then if that is const, then so should the result
be. But that means it can only be assigned to a variable that also has
the “const” modifier.

Really, it should have become two functions, one where both
“haystack” and the result are const, and one where neither is.

Re: What Is Wrong With This Function?

<87frxh9xlt.fsf@bsb.me.uk>

  copy mid

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

  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: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: What Is Wrong With This Function?
Date: Sun, 25 Feb 2024 00:49:34 +0000
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <87frxh9xlt.fsf@bsb.me.uk>
References: <urdvk2$1f1dt$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="e770f46cfdd6e611e431e3de15c787fc";
logging-data="1562189"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+GKlt9uen6fJ7xZyrkeTPxVipruyrHn2A="
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:xd9BXmC0kggeb2dmuqtiVRl3Vu0=
sha1:CW1FtczG2vgkfmWGgFZqjurK4TE=
X-BSB-Auth: 1.f05e10373e53010ecf24.20240225004934GMT.87frxh9xlt.fsf@bsb.me.uk
 by: Ben Bacarisse - Sun, 25 Feb 2024 00:49 UTC

Lawrence D'Oliveiro <ldo@nz.invalid> writes:

> I think it was Dennis Ritchie who pointed out what a fudge this was:
>
> char *strstr(const char *haystack, const char *needle);
>
> ANSI C introduced the “const” modifier, and wanted to use it for the
> arguments to strstr. But if the result is a pointer into the
> “haystack” string, then if that is const, then so should the result
> be. But that means it can only be assigned to a variable that also has
> the “const” modifier.
>
> Really, it should have become two functions, one where both
> “haystack” and the result are const, and one where neither is.

Now (in C23) it has become a generic function that preserves const
correctness in its first argument.

--
Ben.

Re: What Is Wrong With This Function?

<jr8ltipei88tcrbtl8gdvntsvv7kusq8mb@4ax.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!dotsrc.org!filter.dotsrc.org!news.dotsrc.org!not-for-mail
From: schwa...@delq.com (Barry Schwarz)
Newsgroups: comp.lang.c
Subject: Re: What Is Wrong With This Function?
Date: Sat, 24 Feb 2024 18:23:45 -0800
Message-ID: <jr8ltipei88tcrbtl8gdvntsvv7kusq8mb@4ax.com>
References: <urdvk2$1f1dt$1@dont-email.me>
X-Newsreader: Forte Agent 4.2/32.1118
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
Lines: 28
Organization: SunSITE.dk - Supporting Open source
NNTP-Posting-Host: 03737a6b.news.sunsite.dk
X-Trace: 1708827830 news.sunsite.dk 710 schwarzb@q.com/97.126.73.152:59316
X-Complaints-To: staff@sunsite.dk
 by: Barry Schwarz - Sun, 25 Feb 2024 02:23 UTC

On Sat, 24 Feb 2024 23:54:42 -0000 (UTC), Lawrence D'Oliveiro
<ldo@nz.invalid> wrote:

>I think it was Dennis Ritchie who pointed out what a fudge this was:
>
> char *strstr(const char *haystack, const char *needle);
>
>ANSI C introduced the “const” modifier, and wanted to use it for the
>arguments to strstr. But if the result is a pointer into the
>“haystack” string, then if that is const, then so should the result
>be. But that means it can only be assigned to a variable that also has
>the “const” modifier.
>
>Really, it should have become two functions, one where both
>“haystack” and the result are const, and one where neither is.

It seems to me that the const modifier in this context does not mean
no one can ever modify the string. It is a guarantee that the
function will not modify the string. Once the function returns, all
bets are off.

While I don't recall having the need myself, I see nothing wrong with
calling strstr to find a substring and then doing whatever I want to
the haystack, such as terminating it either before of after the
substring by inserting a '\0'.

--
Remove del for email

Re: What Is Wrong With This Function?

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

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!newsfeed.endofthelinebbs.com!news.hispagatos.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: What Is Wrong With This Function?
Date: Sat, 24 Feb 2024 19:10:01 -0800
Organization: None to speak of
Lines: 61
Message-ID: <87plwlw86u.fsf@nosuchdomain.example.com>
References: <urdvk2$1f1dt$1@dont-email.me>
<jr8ltipei88tcrbtl8gdvntsvv7kusq8mb@4ax.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: dont-email.me; posting-host="bb66de1b9b107dde749bb8d6bc16acf3";
logging-data="1732476"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/VxZKVZ2D9wBbF6m89eiFW"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:S/fXmpQN0nyA0S+i3aYMmcxuCLg=
sha1:QKvDogP947JNNJhdBB6afVNekbM=
 by: Keith Thompson - Sun, 25 Feb 2024 03:10 UTC

Barry Schwarz <schwarzb@delq.com> writes:
> On Sat, 24 Feb 2024 23:54:42 -0000 (UTC), Lawrence D'Oliveiro
> <ldo@nz.invalid> wrote:
>>I think it was Dennis Ritchie who pointed out what a fudge this was:
>>
>> char *strstr(const char *haystack, const char *needle);
>>
>>ANSI C introduced the “const” modifier, and wanted to use it for the
>>arguments to strstr. But if the result is a pointer into the
>>“haystack” string, then if that is const, then so should the result
>>be. But that means it can only be assigned to a variable that also has
>>the “const” modifier.
>>
>>Really, it should have become two functions, one where both
>>“haystack” and the result are const, and one where neither is.
>
> It seems to me that the const modifier in this context does not mean
> no one can ever modify the string. It is a guarantee that the
> function will not modify the string. Once the function returns, all
> bets are off.
>
> While I don't recall having the need myself, I see nothing wrong with
> calling strstr to find a substring and then doing whatever I want to
> the haystack, such as terminating it either before of after the
> substring by inserting a '\0'.

That's fine if haystack is modifiable. The problem is that when the
haystack argument points to a non-modifiable string, such as a string
literal, the return value purports to allow modifying it.

An example:

#include <string.h>
#include <stdio.h>
int main(void) {
const char *message = "hello, world";
char *s = strstr(message, "world");
puts(s);
s[0] = 'W';
puts(s);
}

The first puts(s) correctly prints "world". The assignment to s[0] has
undefined behavior (seg fault on my system).

One solution would have been to define two functions with different
names:

const char *strstr_c(const char *haystack, const char *needle);
char *strstr ( char *haystack, const char *needle);

C++ has <cstring>, which is mostly a copy of C's <string.h>, but it has
two overloaded strstr() functions.

C23 makes strstr() a generic function, so the return value has the type
of the first argument.

--
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: What Is Wrong With This Function?

<urekss$1mmc0$1@dont-email.me>

  copy mid

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

  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: jameskuy...@alumni.caltech.edu (James Kuyper)
Newsgroups: comp.lang.c
Subject: Re: What Is Wrong With This Function?
Date: Sun, 25 Feb 2024 00:57:47 -0500
Organization: A noiseless patient Spider
Lines: 10
Message-ID: <urekss$1mmc0$1@dont-email.me>
References: <urdvk2$1f1dt$1@dont-email.me>
<jr8ltipei88tcrbtl8gdvntsvv7kusq8mb@4ax.com>
<87plwlw86u.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 25 Feb 2024 05:57:48 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="d8412534fb33f2e761a25e4c01619e6a";
logging-data="1792384"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18XuXvWYKb7PrPll2a6AdlA9AiWUe0XNYQ="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:VrefaHjY2lPOKA++OdqS/6ql/GU=
In-Reply-To: <87plwlw86u.fsf@nosuchdomain.example.com>
Content-Language: en-US
 by: James Kuyper - Sun, 25 Feb 2024 05:57 UTC

On 2/24/24 22:10, Keith Thompson wrote:
....
> C23 makes strstr() a generic function, so the return value has the type
> of the first argument.

Nice! I see this also applies to memchr, strchr, strpbrk, and strrchr.
However, I think they missed and opportunity to generalize. Those
functions are generic only with respect to the type qualifiers. I think
it would have been even better if they had been made generic over the
character type, allowing wchar_t, char16_T, and char32_t.

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor