https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70301

            Bug ID: 70301
           Summary: missing diagnostic on taking the address of a
                    temporary
           Product: gcc
           Version: 6.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

The following attempts to store the address of the temporary member array
returned from function f() in a couple of pointer variables.  Since the
lifetime of the temporary returned from the function ends at the end of the
complete expression the saved pointer is invalid and the code should be
diagnosed.

$ cat t.c && /build/gcc-trunk-bootstrap/gcc/xgcc -B
/build/gcc-trunk-bootstrap/gcc -S -Wall -Wextra -Wpedantic -xc++ t.c
struct S { int a [1]; };

struct S f (void);

void g (void)
{
    int* p = f ().a;
    int *q = &f ().a [0];

    (void)p;
    (void)q;
}

Clang diagnoses one of the two expressions above:

warning: pointer is initialized by a
      temporary array, which will be destroyed at the end of the
full-expression
      [-Waddress-of-array-temporary]
    int* p = f ().a;
             ^~~~~~
1 warning generated.

Reply via email to