(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
Wstringop-overflow-44.c
       1  /* PR middle-end/97175 - ICE on an excessive strncpy bound
       2     { dg-do compile }
       3     { dg-options "-O -Wall" } */
       4  
       5  int n;
       6  
       7  char *d;
       8  
       9  void sink (void*);
      10  
      11  /* Exercise calls with a destination of unknown size.  */
      12  
      13  void f0 (const void *s)
      14  {
      15    if (n > 0) return;
      16    __builtin_memcpy (d, s, n);       // eliminated
      17  }
      18  
      19  void f1 (const void *s)
      20  {
      21    if (n > 0) return;
      22    __builtin_memmove (d, s, n);      // eliminated
      23  }
      24  
      25  void f2 (void)
      26  {
      27    if (n > 0) return;
      28    __builtin_memset (d, 0, n);       // eliminated
      29  }
      30  
      31  void f3 (const char *s)
      32  {
      33    if (n > 0) return;
      34    __builtin_strncpy (d, s, n);      // can be eliminated but isn't
      35  }
      36  
      37  void f4 (const char *s)
      38  {
      39    if (n > 0) return;
      40    *d = 0;
      41    __builtin_strncat (d, s, n);      // can be eliminated but isn't
      42  }
      43  
      44  
      45  /* Exercise the same calls but with a declared destination object.  */
      46  
      47  void g0 (const void *s)
      48  {
      49    if (n > 0) return;
      50    char a[1];
      51    __builtin_memcpy (a, s, n);       // eliminated
      52    sink (a);
      53  }
      54  
      55  void g1 (const void *s)
      56  {
      57    if (n > 0) return;
      58    char a[1];
      59    __builtin_memmove (a, s, n);      // eliminated
      60    sink (a);
      61  }
      62  
      63  void g2 (void)
      64  {
      65    if (n > 0) return;
      66    char a[1];
      67    __builtin_memset (a, 0, n);       // eliminated
      68    sink (a);
      69  }
      70  
      71  void g3 (const char *s)
      72  {
      73    if (n > 0) return;
      74    char a[1];
      75    __builtin_strncpy (a, s, n);      // can be eliminated but isn't
      76    sink (a);
      77  }
      78  
      79  void g4 (const char *s)
      80  {
      81    if (n > 0) return;
      82    char a[1];
      83    *a = 0;
      84    __builtin_strncat (a, s, n);      // can be eliminated but isn't
      85    sink (a);
      86  }
      87  
      88  
      89  void h0 (const void *s)
      90  {
      91    if (n > 0) return;
      92    d = __builtin_malloc (1);
      93    __builtin_memcpy (d, s, n);       // eliminated
      94  }
      95  
      96  void h1 (const void *s)
      97  {
      98    if (n > 0) return;
      99    d = __builtin_malloc (1);
     100    __builtin_memmove (d, s, n);      // eliminated
     101  }
     102  
     103  void h2 (void)
     104  {
     105    if (n > 0) return;
     106    d = __builtin_malloc (1);
     107    __builtin_memset (d, 0, n);       // eliminated
     108  }
     109  
     110  void h3 (const char *s)
     111  {
     112    if (n > 0) return;
     113    d = __builtin_malloc (1);
     114    __builtin_strncpy (d, s, n);      // can be eliminated but isn't
     115  }
     116  
     117  void h4 (const char *s)
     118  {
     119    if (n > 0) return;
     120    d = __builtin_malloc (1);
     121    *d = 0;
     122    __builtin_strncat (d, s, n);      // can be eliminated but isn't
     123  }
     124  
     125  /* The calls above that aren't eliminated trigger
     126       warning: specified size between INT_MAX and SIZE_MAX exceed maximum
     127                object size PTRDIFF_MAX
     128    { dg-prune-output "-Wstringop-overflow" }
     129    { dg-prune-output "-Wstringop-overread" } */