Fix ICE in substring-handling building 502.gcc_r (PR 87562)
[official-gcc.git] / gcc / testsuite / c-c++-common / Wsizeof-pointer-memaccess3.c
blob97598c423464bd89d86ac56d1f4a4c42e654aa76
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" } */
5 #define bos(ptr) __builtin_object_size (ptr, 1)
6 #define bos0(ptr) __builtin_object_size (ptr, 0)
8 #define memset(dst, val, sz) \
9 (FUNC (memset, dst, val, sz, bos (dst)), sink ((dst)))
11 #define memcpy(dst, src, sz) \
12 (FUNC (memcpy, dst, src, sz, bos (dst)), sink ((dst)))
14 #define memmove(dst, src, sz) \
15 (FUNC (memmove, dst, src, sz, bos (dst)), sink ((dst)))
17 #define mempcpy(dst, src, sz) \
18 (FUNC (mempcpy, dst, src, sz, bos (dst)), sink ((dst)))
20 #define strncpy(dst, src, sz) \
21 (FUNC (strncpy, dst, src, sz, bos (dst)), sink (dst))
23 #define strncat(dst, src, sz) \
24 (FUNC (strncat, dst, src, sz, bos (dst)), sink (dst))
26 #define stpncpy(dst, src, sz) \
27 (FUNC (stpncpy, dst, src, sz, bos (dst)), sink (dst))
29 void sink (void*);
31 #define S10 "123456789"
32 extern char a10[10];
34 void test_string_literal (char *dst)
36 #define FUNC(f, d, s, n, x) __builtin_ ## f (d, s, n)
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);
45 memset (dst, 0, sizeof S10);
47 stpncpy (dst, S10, sizeof S10); /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" } */
49 strncpy (dst, S10, sizeof S10); /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" } */
51 strncat (dst, S10, sizeof S10); /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" } */
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 *-*-* } } */
60 strncpy (dst, S10, sizeof S10 - 1); /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" "" { xfail *-*-* } } */
62 strncat (dst, S10, sizeof S10 - 1); /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" "" { xfail *-*-* } } */
66 void test_char_array (char *dst)
68 memcpy (dst, a10, sizeof a10);
69 mempcpy (dst, a10, sizeof a10);
70 memmove (dst, a10, sizeof a10);
72 memset (dst, 0, sizeof a10);
74 stpncpy (dst, a10, sizeof a10); /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" } */
76 strncpy (dst, a10, sizeof a10); /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" } */
78 strncat (dst, a10, sizeof a10); /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" } */
80 stpncpy (dst, a10, sizeof a10 - 1); /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" "" { xfail *-*-* } } */
82 strncpy (dst, a10, sizeof a10 - 1); /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" "" { xfail *-*-* } } */
84 strncat (dst, a10, sizeof a10 - 1); /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" "" { xfail *-*-* } } */
88 #undef FUNC
89 #define FUNC(f, d, s, n, os) __builtin___ ## f ## _chk (d, s, n, os)
91 void test_char_array_chk (char *dst)
93 memcpy (dst, S10, sizeof S10);
94 mempcpy (dst, S10, sizeof S10);
95 memmove (dst, S10, sizeof S10);
97 memset (dst, 0, sizeof S10);
99 stpncpy (dst, S10, sizeof S10); /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" } */
101 strncpy (dst, S10, sizeof S10); /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" } */
103 strncat (dst, S10, sizeof S10); /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" } */
105 stpncpy (dst, S10, sizeof S10 - 1); /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" "" { xfail *-*-* } } */
107 strncpy (dst, S10, sizeof S10 - 1); /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" "" { xfail *-*-* } } */
109 strncat (dst, S10, sizeof S10 - 1); /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" "" { xfail *-*-* } } */
113 void test_string_literal_chk (char *dst)
115 memcpy (dst, a10, sizeof a10);
116 mempcpy (dst, a10, sizeof a10);
117 memmove (dst, a10, sizeof a10);
119 memset (dst, 0, sizeof a10);
121 stpncpy (dst, a10, sizeof a10); /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" } */
123 strncpy (dst, a10, sizeof a10); /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" } */
125 strncat (dst, a10, sizeof a10); /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" } */
127 stpncpy (dst, a10, sizeof a10 - 1); /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" "" { xfail *-*-* } } */
129 strncpy (dst, a10, sizeof a10 - 1); /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" "" { xfail *-*-* } } */
131 strncat (dst, a10, sizeof a10 - 1); /* { dg-warning "\\\[-Wsizeof-pointer-memaccess]" "" { xfail *-*-* } } */