(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
Wdangling-pointer.c
       1  /* Exercise basic C-only cases of -Wdangling-pointer.
       2     { dg-do compile }
       3     { dg-options "-O0 -Wall" }
       4     { dg-require-effective-target alloca } */
       5  
       6  typedef __SIZE_TYPE__ size_t;
       7  
       8  extern void* memchr (const void*, int, size_t);
       9  extern char* strchr (const char*, int);
      10  
      11  void sink (const void*, ...);
      12  
      13  
      14  void nowarn_compound_literal (int i)
      15  {
      16    {
      17      int *p = (int[]){ 1, 2, 3 };
      18      sink (p);
      19    }
      20    {
      21      int *q = (int[]){ 1, 2, 3 };
      22      int *p = &q[1];
      23      sink (p);
      24    }
      25    {
      26      int *p = __builtin_memchr ((int[]){ 1, 2, 3 }, 2, 3 * sizeof *p);
      27      sink (p);
      28    }
      29    {
      30      int *p = __builtin_memchr ((int[]){ i, i + 1 }, 3, 2 * sizeof *p);
      31      sink (p);
      32    }
      33  }
      34  
      35  
      36  void warn_compound_literal (int i)
      37  {
      38    int *p;
      39    {
      40      p = (int[]){ 1, 2, 3 };   // { dg-message "unnamed temporary" },
      41    }
      42    sink (p);                   // { dg-warning "using dangling pointer 'p' to an unnamed temporary" }
      43  
      44    {
      45      int *q =
      46        (int[]){ 1, 2, 3 };     // { dg-message "unnamed temporary" },
      47      p = &q[1];
      48    }
      49    sink (p);                   // { dg-warning "using dangling pointer 'p' to an unnamed temporary" }
      50    {
      51      p = (int*)memchr (
      52  	  (int[]){ 1, 2, 3 }, // { dg-message "unnamed temporary" }
      53  	  2, 3 * sizeof *p);
      54    }
      55    sink (p);                   // { dg-warning "using dangling pointer 'p' to an unnamed temporary" }
      56  
      57    {
      58      p = (int*)memchr (
      59  	  (int[]){ i, i + 1 },// { dg-message "unnamed temporary" }
      60  	  3, 2 * sizeof *p);
      61    }
      62    sink (p);                   // { dg-warning "using dangling pointer 'p' to an unnamed temporary" }
      63  }
      64  
      65  
      66  void warn_store_compound_literal (int **p)
      67  {
      68    int *q = (int[]) { 1, 2, 3 };
      69    p[0] = q;                   // { dg-warning "storing the address" }
      70  }
      71  
      72  void warn_store_vla (int n, int **p)
      73  {
      74    int a[n];
      75    p[1] = &a[1];               // { dg-warning "-Wdangling-pointer" "pr??????" { xfail *-*-* } }
      76  }