Bug 91888 - Memory fail on properly compiled program
Summary: Memory fail on properly compiled program
Keywords:
Status: CLOSED NOTABUG
Alias: None
Product: Red Hat Linux
Classification: Retired
Component: gcc
Version: 9
Hardware: i386
OS: Linux
medium
low
Target Milestone: ---
Assignee: Jakub Jelinek
QA Contact: Brian Brock
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2003-05-29 11:09 UTC by Paul Osmialowski
Modified: 2007-04-18 16:54 UTC (History)
0 users

Fixed In Version:
Clone Of:
Environment:
Last Closed: 2003-05-29 11:26:41 UTC
Embargoed:


Attachments (Terms of Use)

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";


Note You need to log in before you can comment on or make changes to this bug.