Bug 45821 - Missed -Wreturn-local-addr when local variable address comes from within a statement expression
Summary: Missed -Wreturn-local-addr when local variable address comes from within a st...
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: unknown
: P3 minor
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on: 60517
Blocks: Wreturn-local-addr
  Show dependency treegraph
 
Reported: 2010-09-28 17:45 UTC by Andrew Gaul
Modified: 2020-11-03 04:11 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2015-09-18 00:00:00


Attachments
example of bug (244 bytes, text/x-csrc)
2010-09-28 17:45 UTC, Andrew Gaul
Details
example of bug 2 (244 bytes, text/x-csrc)
2010-09-28 17:48 UTC, Andrew Gaul
Details
example of bug 3 (244 bytes, text/plain)
2010-09-28 17:50 UTC, Andrew Gaul
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Andrew Gaul 2010-09-28 17:45:37 UTC
Created attachment 21904 [details]
example of bug

GCC warns when returning a local variable address within a function but not within a statement expression.
Comment 1 Andrew Gaul 2010-09-28 17:48:24 UTC
Created attachment 21905 [details]
example of bug 2
Comment 2 Andrew Gaul 2010-09-28 17:50:02 UTC
Created attachment 21906 [details]
example of bug 3
Comment 3 Andrew Gaul 2010-09-28 17:51:21 UTC
The web form truncates all my attachedments at 244 bytes.  Here is the source inline:

/*
 * GCC warns about returning a local variable address within a function but not
 * within a statement expression:
 *
 * $ gcc statement_expression_return_local.c -c -Wall
 * statement_expression_return_local.c: In function ‘function_return_local’:
 * statement_expression_return_local.c:13: warning: function returns address of local variable
 */

int *function_return_local(void)
{
    int x = 0;
    return &x;
}

int *statement_expression_return_local(void)
{
    int *y = ({
        int x = 0;
        &x;
    });
    return y;
}
Comment 4 Manuel López-Ibáñez 2015-09-18 00:04:35 UTC
(In reply to Andrew Gaul from comment #3)
> int *function_return_local(void)
> {
>     int x = 0;
>     return &x;
> }
> 
> int *statement_expression_return_local(void)
> {
>     int *y = ({
>         int x = 0;
>         &x;
>     });
>     return y;
> }

We now warn at -O2:

test.c:14:10: warning: function returns address of local variable [-Wreturn-local-addr]
   return y;
          ^
test.c:11:11: note: declared here
       int x = 0;
           ^

but only because we return y. For this testcase,

int statement_expression_return_local(void)
{
    int *y = ({
        int x = 0;
        &x;
    });
    return *y;
}

we get: 

test.c:14:10: warning: ‘x’ is used uninitialized in this function [-Wuninitialized]
   return *y;
          ^

which is a bit confusing (and not the same warning).

Possibly related to 60517.
it would be good to add the testcase
Comment 5 Eric Gallager 2018-07-28 03:23:14 UTC
cc-ing diagnostic messages maintainers
Comment 6 Eric Gallager 2019-10-28 04:06:17 UTC
(In reply to Manuel López-Ibáñez from comment #4)
> (In reply to Andrew Gaul from comment #3)
> > int *function_return_local(void)
> > {
> >     int x = 0;
> >     return &x;
> > }
> > 
> > int *statement_expression_return_local(void)
> > {
> >     int *y = ({
> >         int x = 0;
> >         &x;
> >     });
> >     return y;
> > }
> 
> We now warn at -O2:
> 
> test.c:14:10: warning: function returns address of local variable
> [-Wreturn-local-addr]
>    return y;
>           ^
> test.c:11:11: note: declared here
>        int x = 0;
>            ^
> 
> but only because we return y. For this testcase,
> 
> int statement_expression_return_local(void)
> {
>     int *y = ({
>         int x = 0;
>         &x;
>     });
>     return *y;
> }
> 
> we get: 
> 
> test.c:14:10: warning: ‘x’ is used uninitialized in this function
> [-Wuninitialized]
>    return *y;
>           ^
> 
> which is a bit confusing (and not the same warning).

It'd be good to get it from the same warning, so I'm making this block the -Wreturn-local-addr meta-bug

> 
> Possibly related to 60517.
> it would be good to add the testcase
Comment 7 Philip R Brenan 2020-10-28 14:26:57 UTC
A more compact example:

#include <stdio.h>
int * aaa() {return ({int a = 2;        &a;});} // no warning unless -O2
int * bbb() {         int b = 3; return &b;}    //    warning

int main(void)
 {int *a = aaa(), *b = bbb();
  fprintf(stderr, "AAAA %d\n", *a);
  fprintf(stderr, "BBBB %d\n", *b);
  return 0;
 }
// AAAA 3
// Segmentation fault (core dumped)

Please provide the warning for line 2 (aaa) regardless of the level of optimization?