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: relearning C: why does an in-place change to a char* segfault?
Date: Tue, 13 Aug 2024 17:43:09 -0700
Organization: A noiseless patient Spider
Lines: 33
Message-ID: <868qx0szmq.fsf@linuxsc.com>
References: <20240801114615.906@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Date: Wed, 14 Aug 2024 02:43:10 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="088cfa383a3af87f6acebe452dc28057"; logging-data="158184"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18kNRMy2waS11kImn2xdqlTFZGsePTJrkg="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:WM4OQA5cMfx7vf92yrFd8Rmo5NI= sha1:BppFhVv49LuPhPQNL/4rXTg5Cqw=
Xref: csiph.com comp.lang.c:387556
Kaz Kylheku <643-408-1753@kylheku.com> writes:
> On 2024-08-01, Mark Summerfield wrote:
>
>> This program segfaults at the commented line:
>>
>> #include
>> #include
>>
>> void uppercase_ascii(char *s) {
>> while (*s) {
>> *s = toupper(*s); // SEGFAULT
>> s++;
>> }
>> }
>>
>> int main() {
>> char* text = "this is a test";
>
> The "this is a test" object is a literal. It is part of the
> program's image. When you try to change it, you're making your
> program self-modifying.
>
> The ISO C language standard doesn't require implementations to
> support self-modifying programs; the behavior is left undefined.
>
> It could work in some documented, reliable way, in a given
> implementation.
>
> It's the same with any other constant in the program. [...]
That is wrong both technically and practically. And obviously
so.