Because of a mistaken preprocessor macro I ended up compiling a program like
this:

static void foo() {};

void *bar() {
  extern void foo();
  return foo;
}

int main() {
  return bar()==foo;
}

Of course this was not what I intended to do (I don't even know whether it's
legal C) but gcc 3.4.4 compiled and linked it with no warnings (even given
-Wall).  Now, here's the weird thing: it seems that, when compiling with -m32
or without -fPIC, bar() returns the address of the static function foo()!
However, when compiling with -m64 and -fPIC, bar() returns some seemingly
random value. In neither case is there any compiler or linker warning.

What should happen instead is surely one of the following:

1) gcc should emit a warning or error about redeclaring foo() from static to
extern linkage, or...

2) gcc should emit a warning about the undefined behavior of taking the address
of foo() in the context of bar(), or...

3) gcc should consistently generate code for bar() to get the address of some
global symbol "foo", not the static function foo() (in the case of the program
above, ultimately causing a linker error).


-- 
           Summary: strange behavior with no warning at -Wall when
                    redeclaring static function extern
           Product: gcc
           Version: 3.4.4
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: josh dot parsons at stonebow dot otago dot ac dot nz
 GCC build triplet: x86_64-pc-linux-gnu
  GCC host triplet: x86_64-pc-linux-gnu
GCC target triplet: x86_64-pc-linux-gnu


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

  • [Bug c/26271] New: s... josh dot parsons at stonebow dot otago dot ac dot nz

Reply via email to