[Bug c/63272] GCC should warn when using pointer to dead scoped variable within the same function

dcb314 at hotmail dot com gcc-bugzilla@gcc.gnu.org
Fri Oct 30 10:06:33 GMT 2020


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

David Binderman <dcb314 at hotmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dcb314 at hotmail dot com

--- Comment #4 from David Binderman <dcb314 at hotmail dot com> ---
I can confirm that gcc version 11, some six years later, doesn't implement
this warning.

Fortunately, the cppcheck static analyser does.

Example code:

// Dangling pointer

void g( int *);

void f()
{
        int a;
        int * p = 0;

        {
                int b;

                p = &b;
        }
        g( p);
}

$ /home/dcb/cppcheck/trunk/cppcheck oct30a.cc
oct30a.cc:16:5: error: Using pointer to local variable 'b' that is out of
scope. [invalidLifetime]
 g( p);
    ^
oct30a.cc:14:7: note: Address of variable taken here.
  p = &b;
      ^
oct30a.cc:12:7: note: Variable created here.
  int b;
      ^
oct30a.cc:16:5: note: Using pointer to local variable 'b' that is out of scope.
 g( p);
    ^


More information about the Gcc-bugs mailing list