When returning local function pointer, it doesn't produce any warnings. For example: int *h() { int z; return &z; } int (*apply (int (*f) (const void *, const void *), void *a))(const void *) { int zozo(const void * b) { return f(a,b); }; return &zozo; } Compile with -O2 -Wall, it produces a warning for h: ttt.c: In function 'h': ttt.c:17: warning: function returns address of local variable But not for `apply' (and it produces invalid code, which is normal). GCC Version: Using built-in specs. Configured with: ../configure --prefix=/global/morpork/gcc-4.0-20050130 Thread model: posix gcc version 4.0.0 20050130 (experimental)
Confirmed.
(In reply to Patrick Pelissier from comment #0) > When returning local function pointer, it doesn't produce any warnings. > For example: > > int *h() { > int z; > return &z; > } > > int (*apply (int (*f) (const void *, const void *), void *a))(const void *) > { > int zozo(const void * b) { return f(a,b); }; > return &zozo; > } > > Compile with -O2 -Wall, it produces a warning for h: > > ttt.c: In function 'h': > ttt.c:17: warning: function returns address of local variable > With a newer gcc, this warning is now: $ /usr/local/bin/gcc -c -O2 -Wall 19972.c 19972.c: In function ‘h’: 19972.c:3:9: warning: function returns address of local variable [-Wreturn-local-addr] return &z; ^~ Retitling this bug to better describe situation.
I have tested with GCC 7.1.0 the code of #0 (forget the function h, this was only a reference) and the status is the same as described in #0. For the following code, int (*apply (int (*f) (const void *, const void *), void *a))(const void *) { int g(const void * b) { return f(a,b); }; return &g; } I get no warning for returning the nested function 'g'.