public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/35384]  New: Variables declared as 'static char * avar = "some string";' cannot be modified
@ 2008-02-26 18:57 Quinlan at ACM dot org
  2008-02-26 19:00 ` [Bug c/35384] " Quinlan at ACM dot org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Quinlan at ACM dot org @ 2008-02-26 18:57 UTC (permalink / raw)
  To: gcc-bugs

A variable is declared similar to this:
  static char * avar = "some string";
Then an attempt to change the value like this will throw a SIGSEGV:
  avar[4] = '_';
The problem occurs when the variable is declared locally in a function, and
when the variable is declared globally. Adding a cast to the assignment (as
suggested in another bug solution I found during my search) does not alter the
behavior:
  avar[4] = (unsigned char)'_';

Changing the way the variable is declared does work around the problem:
  static char * avar = NULL; // Local or global
   ...
  if(avar == NULL) avar = "some string"; // In a function
   ...
  avar[4] = '_'; // Now this will work

I am seeing this problem using Linux Kernel 2.6.21.5 and gcc version 4.1.2.
I am using the gcc build from the Slackware 12.0 distribution.


-- 
           Summary: Variables declared as 'static char * avar = "some
                    string";' cannot be modified
           Product: gcc
           Version: 4.1.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: Quinlan at ACM dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35384


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c/35384] Variables declared as 'static char * avar = "some string";' cannot be modified
  2008-02-26 18:57 [Bug c/35384] New: Variables declared as 'static char * avar = "some string";' cannot be modified Quinlan at ACM dot org
@ 2008-02-26 19:00 ` Quinlan at ACM dot org
  2008-02-26 19:00 ` pinskia at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Quinlan at ACM dot org @ 2008-02-26 19:00 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from Quinlan at ACM dot org  2008-02-26 18:59 -------
I am building with compiler flags set to:
    -c -DLINUX -gdwarf-2 -g3 -g -O -DDEBUG


-- 

Quinlan at ACM dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|Variables declared as       |Variables declared as
                   |'static char * avar = "some |'static char * avar = "some
                   |string";' cannot be modified|string";' cannot be modified


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35384


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c/35384] Variables declared as 'static char * avar = "some string";' cannot be modified
  2008-02-26 18:57 [Bug c/35384] New: Variables declared as 'static char * avar = "some string";' cannot be modified Quinlan at ACM dot org
  2008-02-26 19:00 ` [Bug c/35384] " Quinlan at ACM dot org
@ 2008-02-26 19:00 ` pinskia at gcc dot gnu dot org
  2008-02-26 20:10 ` Quinlan at ACM dot org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2008-02-26 19:00 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from pinskia at gcc dot gnu dot org  2008-02-26 19:00 -------
"some string" is a string literal so it is constant.  What you are trying to do
is change the string literal which is undefined behavior so you are getting a
runtime seg fault.


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35384


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c/35384] Variables declared as 'static char * avar = "some string";' cannot be modified
  2008-02-26 18:57 [Bug c/35384] New: Variables declared as 'static char * avar = "some string";' cannot be modified Quinlan at ACM dot org
  2008-02-26 19:00 ` [Bug c/35384] " Quinlan at ACM dot org
  2008-02-26 19:00 ` pinskia at gcc dot gnu dot org
@ 2008-02-26 20:10 ` Quinlan at ACM dot org
  2008-02-26 21:06 ` Quinlan at ACM dot org
  2008-02-26 21:10 ` pinskia at gcc dot gnu dot org
  4 siblings, 0 replies; 6+ messages in thread
From: Quinlan at ACM dot org @ 2008-02-26 20:10 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from Quinlan at ACM dot org  2008-02-26 20:10 -------
I appreciate your answer, however shouldn't this declaration:
  static char * avar = "some string";
be identical to this:
  static char * avar = NULL;
  avar = "some string";

Yet when the application executes:
  avar[4] = '_';
with the first declaration I get SIGSEGV while with the second declaration the
fifth character is changed. Why? Does the behavior of the second declaration
result from a bug and if so should I not be using this approach as it is likely
to stop working with the next gcc upgrade? In both instance avar is a pointer
to a constant string!

Note that I encountered this problem in code that was written about 10 years
ago, and that works with early gcc versions and with Microsoft's compilers.

To avoid making a change to a constant variable do I really need to replace a
simple line of code like: 
  static char * avar = NULL;
with this much more complicated approach:
  static char * orig_var = "some string";
  static char * avar = NULL;
  if(avar == NULL) { avar = (char *)calloc(12,1); memcpy(avar,orig_var,12);}
  .... // Now I can safely modify characters in avar
  free(avar); // Sometime before the application exits


-- 

Quinlan at ACM dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |UNCONFIRMED
         Resolution|INVALID                     |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35384


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c/35384] Variables declared as 'static char * avar = "some string";' cannot be modified
  2008-02-26 18:57 [Bug c/35384] New: Variables declared as 'static char * avar = "some string";' cannot be modified Quinlan at ACM dot org
                   ` (2 preceding siblings ...)
  2008-02-26 20:10 ` Quinlan at ACM dot org
@ 2008-02-26 21:06 ` Quinlan at ACM dot org
  2008-02-26 21:10 ` pinskia at gcc dot gnu dot org
  4 siblings, 0 replies; 6+ messages in thread
From: Quinlan at ACM dot org @ 2008-02-26 21:06 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from Quinlan at ACM dot org  2008-02-26 21:05 -------
If the problem is that the char pointer is pointing to a constant value in:
  static char * avar = "some string";
then perhaps initializing with an array of one string as in this line work:
  static char * avar = {"some string"};
That approach doesn't work. Converting avar from a pointer to an array like
this however does work:
  static char avar[] = {"some string"};
This provides a simple workaround that we will use for now. The big problem
remaining is: There are inconsistent behaviors exhibited in these tests. The
previous approach works, but it still is allowing a constant value to be
modified. Is this a bug that may be fixed in the future and break our
applications, or can we expect the C standard be upgraded at some point to
provide for this type of behavior?


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35384


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c/35384] Variables declared as 'static char * avar = "some string";' cannot be modified
  2008-02-26 18:57 [Bug c/35384] New: Variables declared as 'static char * avar = "some string";' cannot be modified Quinlan at ACM dot org
                   ` (3 preceding siblings ...)
  2008-02-26 21:06 ` Quinlan at ACM dot org
@ 2008-02-26 21:10 ` pinskia at gcc dot gnu dot org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2008-02-26 21:10 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from pinskia at gcc dot gnu dot org  2008-02-26 21:09 -------
>Converting avar from a pointer to an array like
this however does work:
  static char avar[] = {"some string"};

This is an array value which is initialized via a string literal.  The array is
writable.

The behavior is still undefined for writing in string literals.


-- Pinski


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35384


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2008-02-26 21:10 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-02-26 18:57 [Bug c/35384] New: Variables declared as 'static char * avar = "some string";' cannot be modified Quinlan at ACM dot org
2008-02-26 19:00 ` [Bug c/35384] " Quinlan at ACM dot org
2008-02-26 19:00 ` pinskia at gcc dot gnu dot org
2008-02-26 20:10 ` Quinlan at ACM dot org
2008-02-26 21:06 ` Quinlan at ACM dot org
2008-02-26 21:10 ` pinskia at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).