Hello,

I don't know whether it is a bug or feature and I searched through the mailing 
lists without success therefor I write my question this way:

If you have a global variable inside a cpp file and create a library out of 
that, the symbol name for that global variable does in no way take the 
variable type into account. A user of that variable can "make" it any type 
with its extern declaration and thus produce subtle errors. An example:

lib.cpp:
------------------------
int maximum;
int minimum;

static bool init ( )
{
  maximum = 2;
  minimum = -7;
}

static bool initialized = init ( );
-----------------------

Create a library out of that lib.cpp file. Then compile the following main.cpp 
and link it against the library:

main.cpp:
---------------------------
extern double maximum;
extern int    minimum;

void main (int, char**)
{
  // Assume you are on a machine where the sizeof (int) is 4 bytes
  // and the sizeof (double) is 8 bytes.
  assert (minimum == -7);
  {
    maximum = 2342343242343.3;
  }
  assert (minimum == -7);

  return 0;
}
---------------------------

The main.o will perfectly link with the library although main.o needs a double 
variable named maximum and the lib only offers an int variable named maximum. 
Because the symbol name does in no way reflect the variable type, everything 
links fine but in fact the variable minimum gets scrambled in this example 
because maximum is accessed as if it is a double variable thus overwriting 4 
additional bytes (in the case the 4 bytes of the variable minimum). The 
assertion will show that.

I tested that on Windows with Visual C++ as well and there main.obj won't link 
because the variable type is part of the symbol name and everthing is fine.

I think it would be very very important for the binary interface (ELF here, 
or?) to have that feature as well. What do you think?

Regards,

Wolfgang Roemer

Reply via email to