(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
analyzer/
write-to-string-literal-1.c
       1  #include <string.h>
       2  
       3  /* PR analyzer/95007.  */
       4  
       5  void test_1 (void)
       6  {
       7    char *s = "foo";
       8    s[0] = 'g'; /* { dg-warning "write to string literal" } */
       9  }
      10  
      11  /* PR c/83347.  */
      12  
      13  void test_2 (void)
      14  {
      15    memcpy ("abc", "def", 3); /* { dg-warning "write to string literal" } */
      16  }
      17  
      18  static char * __attribute__((noinline))
      19  called_by_test_3 (void)
      20  {
      21    return (char *)"foo";
      22  }
      23  
      24  void test_3 (void)
      25  {
      26    char *s = called_by_test_3 ();
      27    s[1] = 'a'; /* { dg-warning "write to string literal" } */
      28  }
      29  
      30  static char * __attribute__((noinline))
      31  called_by_test_4 (int flag)
      32  {
      33    if (flag)
      34      return (char *)"foo";
      35    else
      36      return (char *)"bar";
      37  }
      38  
      39  void test_4 (void)
      40  {
      41    char *s = called_by_test_4 (0);
      42    s[1] = 'z'; /* { dg-warning "write to string literal" } */
      43  }
      44  
      45  static char * __attribute__((noinline))
      46  called_by_test_5 (int flag)
      47  {
      48    if (flag)
      49      return (char *)"foo";
      50    else
      51      return (char *)"bar";
      52  }
      53  
      54  void test_5 (int flag)
      55  {
      56    char *s = called_by_test_5 (flag);
      57    s[1] = 'z'; /* We miss this one, unless we disable state merging.  */
      58  }