| 1 |
// RUN: %clang_cc1 -triple x86_64-apple-macosx10.14.0 %s -verify |
| 2 |
// RUN: %clang_cc1 -triple x86_64-apple-macosx10.14.0 %s -verify -DUSE_BUILTINS |
| 3 |
// RUN: %clang_cc1 -xc++ -triple x86_64-apple-macosx10.14.0 %s -verify |
| 4 |
// RUN: %clang_cc1 -xc++ -triple x86_64-apple-macosx10.14.0 %s -verify -DUSE_BUILTINS |
| 5 |
|
| 6 |
// RUN: %clang_cc1 -triple x86_64-apple-macosx10.14.0 %s -verify -fexperimental-new-constant-interpreter |
| 7 |
// RUN: %clang_cc1 -triple x86_64-apple-macosx10.14.0 %s -verify -DUSE_BUILTINS -fexperimental-new-constant-interpreter |
| 8 |
// RUN: %clang_cc1 -xc++ -triple x86_64-apple-macosx10.14.0 %s -verify -fexperimental-new-constant-interpreter |
| 9 |
// RUN: %clang_cc1 -xc++ -triple x86_64-apple-macosx10.14.0 %s -verify -DUSE_BUILTINS -fexperimental-new-constant-interpreter |
| 10 |
|
| 11 |
typedef unsigned long size_t; |
| 12 |
|
| 13 |
#ifdef __cplusplus |
| 14 |
extern "C" { |
| 15 |
#endif |
| 16 |
|
| 17 |
extern int sprintf(char *str, const char *format, ...); |
| 18 |
|
| 19 |
#if defined(USE_BUILTINS) |
| 20 |
#define memcpy(x,y,z) __builtin_memcpy(x,y,z) |
| 21 |
#else |
| 22 |
void *memcpy(void *dst, const void *src, size_t c); |
| 23 |
#endif |
| 24 |
|
| 25 |
#ifdef __cplusplus |
| 26 |
} |
| 27 |
#endif |
| 28 |
|
| 29 |
void call_memcpy(void) { |
| 30 |
char dst[10]; |
| 31 |
char src[20]; |
| 32 |
memcpy(dst, src, 20); // expected-warning {{memcpy' will always overflow; destination buffer has size 10, but size argument is 20}} |
| 33 |
|
| 34 |
if (sizeof(dst) == sizeof(src)) |
| 35 |
memcpy(dst, src, 20); // no warning, unreachable |
| 36 |
} |
| 37 |
|
| 38 |
void call_memcpy_type(void) { |
| 39 |
struct pair { |
| 40 |
int first; |
| 41 |
int second; |
| 42 |
}; |
| 43 |
struct pair p; |
| 44 |
char buf[20]; |
| 45 |
memcpy(&p.first, buf, 20); // expected-warning {{memcpy' will always overflow; destination buffer has size 8, but size argument is 20}} |
| 46 |
} |
| 47 |
|
| 48 |
void call_strncat(void) { |
| 49 |
char s1[10], s2[20]; |
| 50 |
__builtin_strncat(s2, s1, 20); |
| 51 |
__builtin_strncat(s1, s2, 20); // expected-warning {{'strncat' size argument is too large; destination buffer has size 10, but size argument is 20}} |
| 52 |
} |
| 53 |
|
| 54 |
void call_strncpy(void) { |
| 55 |
char s1[10], s2[20]; |
| 56 |
__builtin_strncpy(s2, s1, 20); |
| 57 |
__builtin_strncpy(s1, s2, 20); // expected-warning {{'strncpy' size argument is too large; destination buffer has size 10, but size argument is 20}} |
| 58 |
} |
| 59 |
|
| 60 |
void call_stpncpy(void) { |
| 61 |
char s1[10], s2[20]; |
| 62 |
__builtin_stpncpy(s2, s1, 20); |
| 63 |
__builtin_stpncpy(s1, s2, 20); // expected-warning {{'stpncpy' size argument is too large; destination buffer has size 10, but size argument is 20}} |
| 64 |
} |
| 65 |
|
| 66 |
void call_strcpy(void) { |
| 67 |
const char *const src = "abcd"; |
| 68 |
char dst[4]; |
| 69 |
__builtin_strcpy(dst, src); // expected-warning {{'strcpy' will always overflow; destination buffer has size 4, but the source string has length 5 (including NUL byte)}} |
| 70 |
} |
| 71 |
|
| 72 |
void call_strcpy_nowarn(void) { |
| 73 |
const char *const src = "abcd"; |
| 74 |
char dst[5]; |
| 75 |
// We should not get a warning here. |
| 76 |
__builtin_strcpy(dst, src); |
| 77 |
} |
| 78 |
|
| 79 |
void call_strcat(void) { |
| 80 |
const char *const src = "abcd"; |
| 81 |
char dst1[5]; |
| 82 |
char dst2[4]; |
| 83 |
__builtin_strcat(dst1, src); |
| 84 |
__builtin_strcat(dst2, src); // expected-warning {{'strcat' will always overflow; destination buffer has size 4, but the source string has length 5 (including NUL byte)}} |
| 85 |
} |
| 86 |
|
| 87 |
void call_stpcpy(void) { |
| 88 |
const char *const src = "abcd"; |
| 89 |
char dst1[5]; |
| 90 |
char dst2[4]; |
| 91 |
__builtin_stpcpy(dst1, src); |
| 92 |
__builtin_stpcpy(dst2, src); // expected-warning {{'stpcpy' will always overflow; destination buffer has size 4, but the source string has length 5 (including NUL byte)}} |
| 93 |
} |
| 94 |
|
| 95 |
void call_memmove(void) { |
| 96 |
char s1[10], s2[20]; |
| 97 |
__builtin_memmove(s2, s1, 20); |
| 98 |
__builtin_memmove(s1, s2, 20); // expected-warning {{'memmove' will always overflow; destination buffer has size 10, but size argument is 20}} |
| 99 |
} |
| 100 |
|
| 101 |
void call_memset(void) { |
| 102 |
char buf[10]; |
| 103 |
__builtin_memset(buf, 0xff, 10); |
| 104 |
__builtin_memset(buf, 0xff, 11); // expected-warning {{'memset' will always overflow; destination buffer has size 10, but size argument is 11}} |
| 105 |
} |
| 106 |
|
| 107 |
void call_snprintf(double d, int n) { |
| 108 |
char buf[10]; |
| 109 |
__builtin_snprintf(buf, 10, "merp"); |
| 110 |
__builtin_snprintf(buf, 11, "merp"); // expected-warning {{'snprintf' size argument is too large; destination buffer has size 10, but size argument is 11}} |
| 111 |
__builtin_snprintf(buf, 0, "merp"); |
| 112 |
__builtin_snprintf(buf, 3, "merp"); // expected-warning {{'snprintf' will always be truncated; specified size is 3, but format string expands to at least 5}} |
| 113 |
__builtin_snprintf(buf, 4, "merp"); // expected-warning {{'snprintf' will always be truncated; specified size is 4, but format string expands to at least 5}} |
| 114 |
__builtin_snprintf(buf, 5, "merp"); |
| 115 |
__builtin_snprintf(buf, 1, "%.1000g", d); // expected-warning {{'snprintf' will always be truncated; specified size is 1, but format string expands to at least 2}} |
| 116 |
__builtin_snprintf(buf, 5, "%.1000g", d); |
| 117 |
__builtin_snprintf(buf, 5, "%.1000G", d); |
| 118 |
__builtin_snprintf(buf, 10, " %#08x", n); |
| 119 |
__builtin_snprintf(buf, 2, "%#x", n); |
| 120 |
__builtin_snprintf(buf, 2, "%#X", n); |
| 121 |
__builtin_snprintf(buf, 2, "%#o", n); |
| 122 |
__builtin_snprintf(buf, 1, "%#x", n); // expected-warning {{'snprintf' will always be truncated; specified size is 1, but format string expands to at least 2}} |
| 123 |
__builtin_snprintf(buf, 1, "%#X", n); // expected-warning {{'snprintf' will always be truncated; specified size is 1, but format string expands to at least 2}} |
| 124 |
__builtin_snprintf(buf, 1, "%#o", n); // expected-warning {{'snprintf' will always be truncated; specified size is 1, but format string expands to at least 2}} |
| 125 |
} |
| 126 |
|
| 127 |
void call_vsnprintf(void) { |
| 128 |
char buf[10]; |
| 129 |
__builtin_va_list list; |
| 130 |
__builtin_vsnprintf(buf, 10, "merp", list); |
| 131 |
__builtin_vsnprintf(buf, 11, "merp", list); // expected-warning {{'vsnprintf' size argument is too large; destination buffer has size 10, but size argument is 11}} |
| 132 |
__builtin_vsnprintf(buf, 0, "merp", list); |
| 133 |
__builtin_vsnprintf(buf, 3, "merp", list); // expected-warning {{'vsnprintf' will always be truncated; specified size is 3, but format string expands to at least 5}} |
| 134 |
__builtin_vsnprintf(buf, 4, "merp", list); // expected-warning {{'vsnprintf' will always be truncated; specified size is 4, but format string expands to at least 5}} |
| 135 |
__builtin_vsnprintf(buf, 5, "merp", list); |
| 136 |
__builtin_vsnprintf(buf, 1, "%.1000g", list); // expected-warning {{'vsnprintf' will always be truncated; specified size is 1, but format string expands to at least 2}} |
| 137 |
__builtin_vsnprintf(buf, 5, "%.1000g", list); |
| 138 |
__builtin_vsnprintf(buf, 5, "%.1000G", list); |
| 139 |
__builtin_vsnprintf(buf, 10, " %#08x", list); |
| 140 |
__builtin_vsnprintf(buf, 2, "%#x", list); |
| 141 |
__builtin_vsnprintf(buf, 2, "%#X", list); |
| 142 |
__builtin_vsnprintf(buf, 2, "%#o", list); |
| 143 |
__builtin_vsnprintf(buf, 1, "%#x", list); // expected-warning {{'vsnprintf' will always be truncated; specified size is 1, but format string expands to at least 2}} |
| 144 |
__builtin_vsnprintf(buf, 1, "%#X", list); // expected-warning {{'vsnprintf' will always be truncated; specified size is 1, but format string expands to at least 2}} |
| 145 |
__builtin_vsnprintf(buf, 1, "%#o", list); // expected-warning {{'vsnprintf' will always be truncated; specified size is 1, but format string expands to at least 2}} |
| 146 |
} |
| 147 |
|
| 148 |
void call_sprintf_chk(char *buf) { |
| 149 |
__builtin___sprintf_chk(buf, 1, 6, "hell\n"); |
| 150 |
__builtin___sprintf_chk(buf, 1, 5, "hell\n"); // expected-warning {{'sprintf' will always overflow; destination buffer has size 5, but format string expands to at least 6}} |
| 151 |
__builtin___sprintf_chk(buf, 1, 6, "hell\0 boy"); // expected-warning {{format string contains '\0' within the string body}} |
| 152 |
__builtin___sprintf_chk(buf, 1, 2, "hell\0 boy"); // expected-warning {{format string contains '\0' within the string body}} \ |
| 153 |
// expected-warning {{'sprintf' will always overflow; destination buffer has size 2, but format string expands to at least 5}} |
| 154 |
__builtin___sprintf_chk(buf, 1, 6, "hello"); |
| 155 |
__builtin___sprintf_chk(buf, 1, 5, "hello"); // expected-warning {{'sprintf' will always overflow; destination buffer has size 5, but format string expands to at least 6}} |
| 156 |
__builtin___sprintf_chk(buf, 1, 2, "%c", '9'); |
| 157 |
__builtin___sprintf_chk(buf, 1, 1, "%c", '9'); // expected-warning {{'sprintf' will always overflow; destination buffer has size 1, but format string expands to at least 2}} |
| 158 |
__builtin___sprintf_chk(buf, 1, 2, "%d", 9); |
| 159 |
__builtin___sprintf_chk(buf, 1, 1, "%d", 9); // expected-warning {{'sprintf' will always overflow; destination buffer has size 1, but format string expands to at least 2}} |
| 160 |
__builtin___sprintf_chk(buf, 1, 2, "%i", 9); |
| 161 |
__builtin___sprintf_chk(buf, 1, 1, "%i", 9); // expected-warning {{'sprintf' will always overflow; destination buffer has size 1, but format string expands to at least 2}} |
| 162 |
__builtin___sprintf_chk(buf, 1, 2, "%o", 9); |
| 163 |
__builtin___sprintf_chk(buf, 1, 1, "%o", 9); // expected-warning {{'sprintf' will always overflow; destination buffer has size 1, but format string expands to at least 2}} |
| 164 |
__builtin___sprintf_chk(buf, 1, 2, "%u", 9); |
| 165 |
__builtin___sprintf_chk(buf, 1, 1, "%u", 9); // expected-warning {{'sprintf' will always overflow; destination buffer has size 1, but format string expands to at least 2}} |
| 166 |
__builtin___sprintf_chk(buf, 1, 2, "%x", 9); |
| 167 |
__builtin___sprintf_chk(buf, 1, 1, "%x", 9); // expected-warning {{'sprintf' will always overflow; destination buffer has size 1, but format string expands to at least 2}} |
| 168 |
__builtin___sprintf_chk(buf, 1, 2, "%X", 9); |
| 169 |
__builtin___sprintf_chk(buf, 1, 1, "%X", 9); // expected-warning {{'sprintf' will always overflow; destination buffer has size 1, but format string expands to at least 2}} |
| 170 |
__builtin___sprintf_chk(buf, 1, 2, "%hhd", (char)9); |
| 171 |
__builtin___sprintf_chk(buf, 1, 1, "%hhd", (char)9); // expected-warning {{'sprintf' will always overflow; destination buffer has size 1, but format string expands to at least 2}} |
| 172 |
__builtin___sprintf_chk(buf, 1, 2, "%hd", (short)9); |
| 173 |
__builtin___sprintf_chk(buf, 1, 1, "%hd", (short)9); // expected-warning {{'sprintf' will always overflow; destination buffer has size 1, but format string expands to at least 2}} |
| 174 |
__builtin___sprintf_chk(buf, 1, 2, "%ld", 9l); |
| 175 |
__builtin___sprintf_chk(buf, 1, 1, "%ld", 9l); // expected-warning {{'sprintf' will always overflow; destination buffer has size 1, but format string expands to at least 2}} |
| 176 |
__builtin___sprintf_chk(buf, 1, 2, "%lld", 9ll); |
| 177 |
__builtin___sprintf_chk(buf, 1, 1, "%lld", 9ll); // expected-warning {{'sprintf' will always overflow; destination buffer has size 1, but format string expands to at least 2}} |
| 178 |
__builtin___sprintf_chk(buf, 1, 2, "%%"); |
| 179 |
__builtin___sprintf_chk(buf, 1, 1, "%%"); // expected-warning {{'sprintf' will always overflow; destination buffer has size 1, but format string expands to at least 2}} |
| 180 |
__builtin___sprintf_chk(buf, 1, 4, "%#x", 9); |
| 181 |
__builtin___sprintf_chk(buf, 1, 3, "%#x", 9); |
| 182 |
__builtin___sprintf_chk(buf, 1, 4, "%p", (void *)9); |
| 183 |
__builtin___sprintf_chk(buf, 1, 3, "%p", (void *)9); // expected-warning {{'sprintf' will always overflow; destination buffer has size 3, but format string expands to at least 4}} |
| 184 |
__builtin___sprintf_chk(buf, 1, 3, "%+d", 9); |
| 185 |
__builtin___sprintf_chk(buf, 1, 2, "%+d", 9); // expected-warning {{'sprintf' will always overflow; destination buffer has size 2, but format string expands to at least 3}} |
| 186 |
__builtin___sprintf_chk(buf, 1, 3, "% i", 9); |
| 187 |
__builtin___sprintf_chk(buf, 1, 2, "% i", 9); // expected-warning {{'sprintf' will always overflow; destination buffer has size 2, but format string expands to at least 3}} |
| 188 |
__builtin___sprintf_chk(buf, 1, 6, "%5d", 9); |
| 189 |
__builtin___sprintf_chk(buf, 1, 5, "%5d", 9); // expected-warning {{'sprintf' will always overflow; destination buffer has size 5, but format string expands to at least 6}} |
| 190 |
__builtin___sprintf_chk(buf, 1, 9, "%f", 9.f); |
| 191 |
__builtin___sprintf_chk(buf, 1, 8, "%f", 9.f); // expected-warning {{'sprintf' will always overflow; destination buffer has size 8, but format string expands to at least 9}} |
| 192 |
__builtin___sprintf_chk(buf, 1, 9, "%Lf", (long double)9.); |
| 193 |
__builtin___sprintf_chk(buf, 1, 8, "%Lf", (long double)9.); // expected-warning {{'sprintf' will always overflow; destination buffer has size 8, but format string expands to at least 9}} |
| 194 |
__builtin___sprintf_chk(buf, 1, 10, "%+f", 9.f); |
| 195 |
__builtin___sprintf_chk(buf, 1, 9, "%+f", 9.f); // expected-warning {{'sprintf' will always overflow; destination buffer has size 9, but format string expands to at least 10}} |
| 196 |
__builtin___sprintf_chk(buf, 1, 12, "%e", 9.f); |
| 197 |
__builtin___sprintf_chk(buf, 1, 11, "%e", 9.f); // expected-warning {{'sprintf' will always overflow; destination buffer has size 11, but format string expands to at least 12}} |
| 198 |
} |
| 199 |
|
| 200 |
void call_sprintf(void) { |
| 201 |
char buf[6]; |
| 202 |
sprintf(buf, "hell\0 boy"); // expected-warning {{format string contains '\0' within the string body}} |
| 203 |
sprintf(buf, "hello b\0y"); // expected-warning {{format string contains '\0' within the string body}} \ |
| 204 |
// expected-warning {{'sprintf' will always overflow; destination buffer has size 6, but format string expands to at least 8}} |
| 205 |
sprintf(buf, "hello"); |
| 206 |
sprintf(buf, "hello!"); // expected-warning {{'sprintf' will always overflow; destination buffer has size 6, but format string expands to at least 7}} |
| 207 |
sprintf(buf, "1234%%"); |
| 208 |
sprintf(buf, "12345%%"); // expected-warning {{'sprintf' will always overflow; destination buffer has size 6, but format string expands to at least 7}} |
| 209 |
sprintf(buf, "1234%c", '9'); |
| 210 |
sprintf(buf, "12345%c", '9'); // expected-warning {{'sprintf' will always overflow; destination buffer has size 6, but format string expands to at least 7}} |
| 211 |
sprintf(buf, "1234%d", 9); |
| 212 |
sprintf(buf, "12345%d", 9); // expected-warning {{'sprintf' will always overflow; destination buffer has size 6, but format string expands to at least 7}} |
| 213 |
sprintf(buf, "1234%lld", 9ll); |
| 214 |
sprintf(buf, "12345%lld", 9ll); // expected-warning {{'sprintf' will always overflow; destination buffer has size 6, but format string expands to at least 7}} |
| 215 |
sprintf(buf, "12%#x", 9); |
| 216 |
sprintf(buf, "123%#x", 9); |
| 217 |
sprintf(buf, "12%p", (void *)9); |
| 218 |
sprintf(buf, "123%p", (void *)9); // expected-warning {{'sprintf' will always overflow; destination buffer has size 6, but format string expands to at least 7}} |
| 219 |
sprintf(buf, "123%+d", 9); |
| 220 |
sprintf(buf, "1234%+d", 9); // expected-warning {{'sprintf' will always overflow; destination buffer has size 6, but format string expands to at least 7}} |
| 221 |
sprintf(buf, "123% i", 9); |
| 222 |
sprintf(buf, "1234% i", 9); // expected-warning {{'sprintf' will always overflow; destination buffer has size 6, but format string expands to at least 7}} |
| 223 |
sprintf(buf, "%5d", 9); |
| 224 |
sprintf(buf, "1%5d", 9); // expected-warning {{'sprintf' will always overflow; destination buffer has size 6, but format string expands to at least 7}} |
| 225 |
sprintf(buf, "%.3f", 9.f); |
| 226 |
sprintf(buf, "5%.3f", 9.f); // expected-warning {{'sprintf' will always overflow; destination buffer has size 6, but format string expands to at least 7}} |
| 227 |
sprintf(buf, "%+.2f", 9.f); |
| 228 |
sprintf(buf, "%+.3f", 9.f); // expected-warning {{'sprintf' will always overflow; destination buffer has size 6, but format string expands to at least 7}} |
| 229 |
sprintf(buf, "%.0e", 9.f); |
| 230 |
sprintf(buf, "5%.1e", 9.f); // expected-warning {{'sprintf' will always overflow; destination buffer has size 6, but format string expands to at least 8}} |
| 231 |
} |
| 232 |
|
| 233 |
#ifdef __cplusplus |
| 234 |
template <class> struct S { |
| 235 |
void mf() const { |
| 236 |
__builtin_memset(const_cast<char *>(mv), 0, 0); |
| 237 |
} |
| 238 |
|
| 239 |
char mv[10]; |
| 240 |
}; |
| 241 |
|
| 242 |
template <int A, int B> |
| 243 |
void call_memcpy_dep() { |
| 244 |
char bufferA[A]; |
| 245 |
char bufferB[B]; |
| 246 |
memcpy(bufferA, bufferB, 10); // expected-warning{{'memcpy' will always overflow; destination buffer has size 9, but size argument is 10}} |
| 247 |
} |
| 248 |
|
| 249 |
void call_call_memcpy() { |
| 250 |
call_memcpy_dep<10, 9>(); |
| 251 |
call_memcpy_dep<9, 10>(); // expected-note {{in instantiation of function template specialization 'call_memcpy_dep<9, 10>' requested here}} |
| 252 |
} |
| 253 |
#endif |