Path: csiph.com!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch
Newsgroups: comp.lang.c
Subject: Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?
Date: Mon, 12 Aug 2024 16:05:29 -0700
Organization: A noiseless patient Spider
Lines: 56
Message-ID: <86jzgl1gw6.fsf@linuxsc.com>
References: <20240801174026.00002cda@yahoo.com> <87zfpvfdk4.fsf@nosuchdomain.example.com> <86ttfp2zpf.fsf@linuxsc.com> <871q2tiffa.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Date: Tue, 13 Aug 2024 01:05:30 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="7741d78d7fa92162b25738c81b1a0845"; logging-data="3686921"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX188CBPSvdtoi015VbUW5g07rRt648OGt+0="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:6vTbZw6m7SDBX9bqSbxHzVpHqEI= sha1:FVU1aSWTsTR7gNzo8O6v5NTCAdk=
Xref: csiph.com comp.lang.c:387531
Keith Thompson writes:
> Tim Rentsch writes:
>
>> Keith Thompson writes:
>>
>>> candycanearter07
>>> writes:
>>>
>>>> David Brown wrote at 17:56 this Thursday (GMT):
>>>
>>> [...]
>>>
>>>>> gcc has the option "-Wwrite-strings" that makes string literals in
>>>>> C have "const char" array type, and thus give errors when you try
>>>>> to assign to a non-const char * pointer. But the option has to be
>>>>> specified explicitly (it is not in -Wall) because it changes the
>>>>> meaning of the code and can cause compatibility issues with
>>>>> existing correct code.
>>>>
>>>> -Wwrite-strings is included in -Wpedantic.
>>>
>>> No it isn't, nor is it included in -Wall -- and it wouldn't make
>>> sense to do so.
>>>
>>> The -Wpedantic option is intended to produce all required
>>> diagnostics for the specified C standard. -Wwrite-strings
>>> gives string literals the type `const char[LENGTH]`, which
>>> enables useful diagnostics but is *non-conforming*.
>>
>> As long as the -Wwrite-strings diagnostics are only warnings the
>> result is still conforming.
>
> It's not just about diagnostics. This program:
>
> #include
> int main(void) {
> puts(_Generic("hello",
> char*: "char*",
> const char*: "const char*",
> default: "?"));
> }
>
> must print "char*" in a conforming implementation. With
> (gcc|clang) -Wwrite-strings, it prints "const char*".
Good point. I hadn't considered such cases.
> And something as simple as:
>
> char *p = "hello";
>
> is rejected with a fatal error with "-Wwrite-strings -pedantic-errors".
That violates the "As long as the -Wwrite-strings diagnostics are
only warnings" condition.