Deutsch English Français Italiano |
<877cckgplx.fsf@nosuchdomain.example.com> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!feeds.phibee-telecom.net!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson <Keith.S.Thompson+u@gmail.com> 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: Tue, 13 Aug 2024 13:00:26 -0700 Organization: None to speak of Lines: 77 Message-ID: <877cckgplx.fsf@nosuchdomain.example.com> References: <IoGcndcJ1Zm83zb7nZ2dnZfqnPWdnZ2d@brightview.co.uk> <20240801174026.00002cda@yahoo.com> <v8gi7i$29iu1$1@dont-email.me> <slrnvaorkl.34j6.candycanearter07@candydeb.host.invalid> <87zfpvfdk4.fsf@nosuchdomain.example.com> <86ttfp2zpf.fsf@linuxsc.com> <871q2tiffa.fsf@nosuchdomain.example.com> <86jzgl1gw6.fsf@linuxsc.com> <v9fes9$3rtc7$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Tue, 13 Aug 2024 22:00:27 +0200 (CEST) Injection-Info: dont-email.me; posting-host="75b586a96d8f13b1a18286173fed3ce1"; logging-data="43115"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+SmGDA5gAGuXia/2Iyg7qf" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:94d7yAws7QH/p8H9CxJaAaD0S9s= sha1:4+zXBpMspRfASbJrF0QUPcpUF7g= Bytes: 4733 David Brown <david.brown@hesbynett.no> writes: > On 13/08/2024 01:05, Tim Rentsch wrote: >> Keith Thompson <Keith.S.Thompson+u@gmail.com> writes: >>> Tim Rentsch <tr.17687@z991.linuxsc.com> writes: >>>> Keith Thompson <Keith.S.Thompson+u@gmail.com> writes: >>>>> candycanearter07 <candycanearter07@candycanearter07.nomail.afraid> >>>>> writes: >>>>>> David Brown <david.brown@hesbynett.no> 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 <stdio.h> >>> 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. > > Indeed. > > I personally think it is nice to have an option to make string > literals "const" in C, even though it is non-conforming. I also think > it is very useful to have a warning on attempts to write to string > literals. But I think gcc has made a mistake here by conflating the > two. I'd rather see the warning being enabled by default (or at least > in -Wall), while the "make string literals const" option should > require an explicit flag and be a "-f" flag rather than a "-W" flag. > The current situation seems to be a quick-and-dirty way to get the > warning. > > Other people may have different opinions, of course :-) I agree. An alternative way to implement "-Wwrite-strings" might have been to invent a new attribute that can be applied to string literal objects. With the current "-Wwrite-strings", gcc marks string literal objects as const, with all the non-conforming consequences that implies. Instead, they could have added an attribute like say, "unwritable" that triggers warnings but no other changes in semantics and no fatal errors (unless you use -Werror, but then you're literally asking for it). -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com void Void(void) { Void(); } /* The recursive call of the void */