Bug 91888 - Memory fail on properly compiled program
Memory fail on properly compiled program
Product: Red Hat Linux
Classification: Retired
Component: gcc (Show other bugs)
i386 Linux
medium Severity low
: ---
: ---
Assigned To: Jakub Jelinek
Brian Brock
Depends On:
  Show dependency treegraph
Reported: 2003-05-29 07:09 EDT by Paul Osmialowski
Modified: 2007-04-18 12:54 EDT (History)
0 users

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Last Closed: 2003-05-29 07:26:41 EDT
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---

Attachments (Terms of Use)

  None (edit)
Description Paul Osmialowski 2003-05-29 07:09:52 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:
#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:

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 07:26:41 EDT
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.