(root)/
gcc-13.2.0/
gcc/
testsuite/
c-c++-common/
Wdangling-pointer-5.c
       1  /* PR middle-end/63272 - GCC should warn when using pointer to dead scoped
       2     variable within the same function
       3     Exercise -Wdangling-pointer for escaping stores of addreses of auto
       4     variables.
       5     { dg-do compile }
       6     { dg-options "-O0 -Wall -ftrack-macro-expansion=0" }
       7     { dg-require-effective-target alloca } */
       8  
       9  void* alloca (__SIZE_TYPE__);
      10  
      11  void* sink (void*, ...);
      12  
      13  extern void *evp;
      14  
      15  void nowarn_store_extern_call (void)
      16  {
      17    int x;
      18    evp = &x;
      19    sink (0);
      20  }
      21  
      22  void nowarn_store_extern_ovrwrite (void)
      23  {
      24    int x;
      25    evp = &x;
      26    evp = 0;
      27  }
      28  
      29  void nowarn_store_extern_store (void)
      30  {
      31    int x;
      32    void **p = (void**)sink (&evp);
      33    evp = &x;
      34    *p = 0;
      35  }
      36  
      37  
      38  void warn_store_alloca (int n)
      39  {
      40    // This fails because of a bug in the warning.
      41    void *p = alloca (n);
      42    evp = p;           // { dg-warning "storing the address of local variable 'x' in 'evp1'" "pr??????" { xfail *-*-* } }
      43  }
      44  
      45  
      46  void warn_store_extern (void)
      47  {
      48    extern void *evp1;  // { dg-message "'evp1' declared here" }
      49    int x;              // { dg-message "'x' declared here" }
      50    evp1 = &x;          // { dg-warning "storing the address of local variable 'x' in 'evp1'" }
      51  }
      52  
      53  
      54  void nowarn_store_arg_call (void **vpp)
      55  {
      56    int x;
      57    *vpp = &x;
      58    sink (0);
      59  }
      60  
      61  void nowarn_store_arg_ovrwrite (void **vpp)
      62  {
      63    int x;
      64    *vpp = &x;
      65    *vpp = 0;
      66  }
      67  
      68  void nowarn_store_arg_store (void **vpp)
      69  {
      70    int x;
      71    void **p = (void**)sink (0);
      72    *vpp = &x;
      73    *p = 0;
      74  }
      75  
      76  void* nowarn_store_arg_store_arg (void **vpp1, void **vpp2)
      77  {
      78    int x;              // { dg-message "'x' declared here" }
      79    void **p = (void**)sink (0);
      80    *vpp1 = &x;         // { dg-warning "storing the address of local variable 'x' in '\\*vpp1'" }
      81    *vpp2 = 0;          // might overwrite *vpp1
      82    return p;
      83  }
      84  
      85  void warn_store_arg (void **vpp)
      86  {
      87    int x;              // { dg-message "'x' declared here" }
      88    *vpp = &x;          // { dg-warning "storing the address of local variable 'x' in '\\*vpp'" }
      89  }
      90  
      91