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