Bug 19972 - -Wreturn-local-addr misses return of local (nested) function pointer
Summary: -Wreturn-local-addr misses return of local (nested) function pointer
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.0.0
: P2 minor
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: Wreturn-local-addr
  Show dependency treegraph
 
Reported: 2005-02-15 08:37 UTC by Patrick Pelissier
Modified: 2021-10-27 19:51 UTC (History)
3 users (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu
Known to work:
Known to fail:
Last reconfirmed: 2017-07-24 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Patrick Pelissier 2005-02-15 08:37:38 UTC
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)
Comment 1 Andrew Pinski 2005-02-15 14:14:02 UTC
Confirmed.
Comment 2 Eric Gallager 2017-07-24 21:01:10 UTC
(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.
Comment 3 Patrick Pelissier 2017-07-25 17:26:48 UTC
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'.