Red Hat Bugzilla – Bug 91888
Memory fail on properly compiled program
Last modified: 2007-04-18 12:54:08 EDT
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:
char * logname="logfile0.txt";
for (i=0; i<10; i++)
printf("%d\n", logname); /* will work fine here */
logname++; /* will fail here */
/* will also fail if I change it to something easier: logname='A';
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):
Steps to Reproduce:
1. compile example code given above
2. run example code
Actual Results: Memory fail error
Expected Results: Proper program execution
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";