Bug 91888

Summary: Memory fail on properly compiled program
Product: [Retired] Red Hat Linux Reporter: Paul Osmialowski <newchief>
Component: gccAssignee: Jakub Jelinek <jakub>
Status: CLOSED NOTABUG QA Contact: Brian Brock <bbrock>
Severity: low Docs Contact:
Priority: medium    
Version: 9   
Target Milestone: ---   
Target Release: ---   
Hardware: i386   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2003-05-29 11:26:41 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Paul Osmialowski 2003-05-29 11:09:52 UTC
From Bugzilla Helper:
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20030225

Description of problem:
I've tried it on RH9 and RH7.1, result was the same.
I've compiled short program:
#include <stdio.h>

int main()
{
    int i;
    char * logname="logfile0.txt";
    for (i=0; i<10; i++)
    {
        printf("%s\n", logname);
        printf("%d\n", logname[7]); /* will work fine here */
        logname[7]++; /* will fail here */
        /* will also fail if I change it to something easier: logname[0]='A';
        */
    }
    return 0;
}
Memory fail error will showup after printing first two lines (printf()). After I
change char * logname="literal expression"; to char logname[]="literal
expression"; everything is fine. In my opinion, program should work properly in
both cases, that would be much safer.


Version-Release number of selected component (if applicable):


How reproducible:
Always

Steps to Reproduce:
1. compile example code given above
2. run example code


Actual Results:  Memory fail error

Expected Results:  Proper program execution


Additional info:

Comment 1 Jakub Jelinek 2003-05-29 11:26:41 UTC
Please read ISO C (e.g. ISO C99 6.4.5 says:
"The multibyte character sequence is
then used to initialize an array of static storage duration and length just sufficient to contain the sequence."
and later on:
"If the program attempts to modify such an array, the behavior is undefined."

You can use -fwritable-strings to make string literals writable in gcc, but
I'd strongly suggest you instead create a char array if you intend to modify it
(-fwritable-string will be likely removed in the future).
char logname[] = "logfile0.txt";