[Bug c++/31775] New: static object mangling conflicts with extern object

geoffk at gcc dot gnu dot org gcc-bugzilla@gcc.gnu.org
Tue May 1 18:53:00 GMT 2007


In [basic.link] paragraph 6, there's an example that shows that (unlike in C)
it is permissible to define an object 'static' in a namespace scope and then
have another object which is 'extern', and reference both in the same
translation unit.

The compiler optimises that example so that there's no way to see the incorrect
behaviour, but a slightly modified version is:

extern "C" void abort();
static int i;
int *p = &i;
int main()
{ 
  int i;
  { 
    extern int i;
    i = 1;
    *p = 2;
    if (i == 2)
      abort ();
  }
  return 0;
}

I believe this should fail to compile with a link error, because the extern
version of 'i' is never defined.  On Darwin, what this does (apparently) is
crash with a bus error trying to write to the first instruction of main, which
is probably a linker bug; I expect that on other OSs it will call abort().

The basic problem is that 'static int i' needs to be a different name in the
assembly than 'extern int i'.


-- 
           Summary: static object mangling conflicts with extern object
           Product: gcc
           Version: 4.3.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: geoffk at gcc dot gnu dot org


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



More information about the Gcc-bugs mailing list