(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
Wdangling-pointer-3.c
       1  /* PR tree-optimization/104715 - false dangling pointer with strstr
       2     Vertify that using pointers that have become dangling after they were
       3     passed to and returned from strstr is diagnosed.
       4    { dg-do compile }
       5    { dg-options "-Wall" } */
       6  
       7  extern char* strstr (const char*, const char*);
       8  
       9  void sink (const void*);
      10  
      11  void nowarn_strstr_static (const char *s)
      12  {
      13    char *t1;
      14  
      15    {
      16      static const char a[] = "abc";
      17      t1 = strstr (a, s);
      18      sink (t1);
      19    }
      20  
      21    sink (t1);
      22  }
      23  
      24  
      25  void nowarn_strstr_lit (const char *s)
      26  {
      27    char *t2;
      28  
      29    {
      30      t2 = strstr ("def", s);
      31      sink (t2);
      32    }
      33  
      34    sink (t2);
      35  }
      36  
      37  
      38  void warn_strstr_comp_lit (const char *s)
      39  {
      40    char *t3;
      41  
      42    {
      43      const char *a =
      44        (char[]){ '1', '\0' };  // { dg-message "unnamed temporary defined here" }
      45      t3 = strstr (a, s);
      46      sink (t3);
      47    }
      48  
      49    sink (t3);            // { dg-warning "using dangling pointer 't3' to an unnamed temporary" }
      50  }
      51  
      52  
      53  void warn_strstr_arg (const char *s)
      54  {
      55    char *t4;
      56  
      57    {
      58      char a[] = "1";     // { dg-message "'a' declared here" }
      59      t4 = strstr (a, s);
      60      sink (t4);
      61    }
      62  
      63    sink (t4);            // { dg-warning "using dangling pointer 't4' to 'a'" }
      64  }
      65  
      66  
      67  void warn_strstr_arg_plus_cst (const char *s)
      68  {
      69    char *t5;
      70  
      71    {
      72      char a[] = "12";    // { dg-message "'a' declared here" }
      73      t5 = strstr (a + 1, s);
      74      sink (t5);
      75    }
      76  
      77    sink (t5);            // { dg-warning "using dangling pointer 't5' to 'a'" }
      78  }
      79  
      80  
      81  void warn_strstr_arg_plus_var (const char *s, int i)
      82  {
      83    char *t6;
      84  
      85    {
      86      char a[] = "123";   // { dg-message "'a' declared here" }
      87      t6 = strstr (a + i, s);
      88      sink (t6++);
      89    }
      90  
      91    sink (t6);            // { dg-warning "using dangling pointer 't6' to 'a'" }
      92  }