(root)/
gcc-13.2.0/
gcc/
testsuite/
c-c++-common/
Wsizeof-pointer-memaccess3.c
       1  /* Test -Wsizeof-pointer-memaccess warnings.  */
       2  /* { dg-do compile } */
       3  /* { dg-options "-Wsizeof-pointer-memaccess -Wno-stringop-overflow -Wno-stringop-truncation -ftrack-macro-expansion=0" } */
       4  
       5  #define bos(ptr) __builtin_object_size (ptr, 1)
       6  #define bos0(ptr) __builtin_object_size (ptr, 0)
       7  
       8  #define memset(dst, val, sz) \
       9    (FUNC (memset, dst, val, sz, bos (dst)), sink ((dst)))
      10  
      11  #define memcpy(dst, src, sz) \
      12    (FUNC (memcpy, dst, src, sz, bos (dst)), sink ((dst)))
      13  
      14  #define memmove(dst, src, sz) \
      15    (FUNC (memmove, dst, src, sz, bos (dst)), sink ((dst)))
      16  
      17  #define mempcpy(dst, src, sz) \
      18    (FUNC (mempcpy, dst, src, sz, bos (dst)), sink ((dst)))
      19  
      20  #define strncpy(dst, src, sz)				\
      21    (FUNC (strncpy, dst, src, sz, bos (dst)), sink (dst))
      22  
      23  #define strncat(dst, src, sz) \
      24    (FUNC (strncat, dst, src, sz, bos (dst)), sink (dst))
      25  
      26  #define stpncpy(dst, src, sz) \
      27    (FUNC (stpncpy, dst, src, sz, bos (dst)), sink (dst))
      28  
      29  void sink (void*);
      30  
      31  #define S10 "123456789"
      32  extern char a10[10];
      33  
      34  void test_string_literal (char *dst)
      35  {
      36  #define FUNC(f, d, s, n, x) __builtin_ ## f (d, s, n)
      37  
      38    /* It's common to call memcpy and other raw memory functions with
      39       size drerived from the source argument.  Verify that no warning
      40       is ussued for such calls.  */
      41    memcpy (dst, S10, sizeof S10);
      42    mempcpy (dst, S10, sizeof S10);
      43    memmove (dst, S10, sizeof S10);
      44  
      45    memset (dst, 0, sizeof S10);
      46  
      47    stpncpy (dst, S10, sizeof S10);   /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" } */
      48  
      49    strncpy (dst, S10, sizeof S10);   /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" } */
      50  
      51    strncat (dst, S10, sizeof S10);   /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" } */
      52  
      53    /* Unlike in the cases above, even though the calls below are likely
      54       wrong, it's not easy to detect that  the expression (sizeof X - 1)
      55       involves sizeof of the source, so no warning is issued here, as
      56       helpful as one might be.  Whether -Wstringop-truncation is issued
      57       is tested elsewhere.  */
      58    stpncpy (dst, S10, sizeof S10 - 1);   /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" "" { xfail *-*-* } } */
      59  
      60    strncpy (dst, S10, sizeof S10 - 1);   /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" "" { xfail *-*-* } } */
      61  
      62    strncat (dst, S10, sizeof S10 - 1);   /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" "" { xfail *-*-* } } */
      63  }
      64  
      65  
      66  void test_char_array (char *dst)
      67  {
      68    memcpy (dst, a10, sizeof a10);
      69    mempcpy (dst, a10, sizeof a10);
      70    memmove (dst, a10, sizeof a10);
      71  
      72    memset (dst, 0, sizeof a10);
      73  
      74    stpncpy (dst, a10, sizeof a10);   /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" } */
      75  
      76    strncpy (dst, a10, sizeof a10);   /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" } */
      77  
      78    strncat (dst, a10, sizeof a10);   /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" } */
      79  
      80    stpncpy (dst, a10, sizeof a10 - 1);   /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" "" { xfail *-*-* } } */
      81  
      82    strncpy (dst, a10, sizeof a10 - 1);   /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" "" { xfail *-*-* } } */
      83  
      84    strncat (dst, a10, sizeof a10 - 1);   /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" "" { xfail *-*-* } } */
      85  }
      86  
      87  
      88  #undef FUNC
      89  #define FUNC(f, d, s, n, os) __builtin___ ## f ## _chk (d, s, n, os)
      90  
      91  void test_char_array_chk (char *dst)
      92  {
      93    memcpy (dst, S10, sizeof S10);
      94    mempcpy (dst, S10, sizeof S10);
      95    memmove (dst, S10, sizeof S10);
      96  
      97    memset (dst, 0, sizeof S10);
      98  
      99    stpncpy (dst, S10, sizeof S10);   /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" } */
     100  
     101    strncpy (dst, S10, sizeof S10);   /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" } */
     102  
     103    strncat (dst, S10, sizeof S10);   /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" } */
     104  
     105    stpncpy (dst, S10, sizeof S10 - 1);   /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" "" { xfail *-*-* } } */
     106  
     107    strncpy (dst, S10, sizeof S10 - 1);   /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" "" { xfail *-*-* } } */
     108  
     109    strncat (dst, S10, sizeof S10 - 1);   /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" "" { xfail *-*-* } } */
     110  }
     111  
     112  
     113  void test_string_literal_chk (char *dst)
     114  {
     115    memcpy (dst, a10, sizeof a10);
     116    mempcpy (dst, a10, sizeof a10);
     117    memmove (dst, a10, sizeof a10);
     118  
     119    memset (dst, 0, sizeof a10);
     120  
     121    stpncpy (dst, a10, sizeof a10);   /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" } */
     122  
     123    strncpy (dst, a10, sizeof a10);   /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" } */
     124  
     125    strncat (dst, a10, sizeof a10);   /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" } */
     126  
     127    stpncpy (dst, a10, sizeof a10 - 1);   /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" "" { xfail *-*-* } } */
     128  
     129    strncpy (dst, a10, sizeof a10 - 1);   /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" "" { xfail *-*-* } } */
     130  
     131    strncat (dst, a10, sizeof a10 - 1);   /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" "" { xfail *-*-* } } */
     132  }