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

            Bug ID: 69433
           Summary: missing -Wreturn-local-addr assigning address of a
                    local to a static
           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: ---

Gcc issues the helpful -Wreturn-local-addr warning for the first two functions
in the test case below but not for for the second two.  It would be useful if
the same or similar warning (say something like -Wleak-local-addr) were issued
there as well.  (Note that Clang only diagnoses f0.)

$ cat z.cpp && ~/bin/gcc-5.1.0/bin/g++ -O2 -S -Wall -Wextra -Wpedantic
-o/dev/null z.cpp
const char* f0 () {
    const char a[] = "abc";
    return a;
}

const char* f1 (int i) {
    const char a[] = "abc";
    const char *q = i ? a : "def";
    return q;
}

const char* f2 () {
    const char a[] = "abc";
    static const char *s = a;
    return s;
}

static const char *s;

void f3 () {
    const char a[] = "abc";
    s = a;
}
z.cpp: In function ‘const char* f0()’:
z.cpp:2:16: warning: address of local variable ‘a’ returned
[-Wreturn-local-addr]
     const char a[] = "abc";
                ^
z.cpp: In function ‘const char* f1(int)’:
z.cpp:9:12: warning: function may return address of local variable
[-Wreturn-local-addr]
     return q;
            ^
z.cpp:7:16: note: declared here
     const char a[] = "abc";
                ^

Reply via email to