| 1 |
// RUN: %clang_cc1 -std=c23 -triple x86_64 -verify -fsyntax-only -Wfortify-source -Wno-string-plus-int %s |
| 2 |
// RUN: %clang_cc1 -std=c23 -triple x86_64 -verify -fsyntax-only -Wfortify-source -Wno-string-plus-int -fexperimental-new-constant-interpreter %s |
| 3 |
|
| 4 |
void test(char *c) { |
| 5 |
// We test offsets 0 to 4. |
| 6 |
// 0: a no op of course |
| 7 |
// 1,2: these result in a different length string than the buffer size |
| 8 |
// 3: the last position: this hits ptr+object_size being a valid pointer, |
| 9 |
// but not dereferencable |
| 10 |
// 4: completely invalid pointer |
| 11 |
__builtin_strcat(c, "42" + 0); |
| 12 |
__builtin_strcat(c, "42" + 1); |
| 13 |
__builtin_strcat(c, "42" + 2); |
| 14 |
__builtin_strcat(c, "42" + 3); |
| 15 |
__builtin_strcat(c, "42" + 4); |
| 16 |
_Static_assert(__builtin_strlen("42" + 0) == 2); |
| 17 |
|
| 18 |
// A test without a null terminator, this captures incorrect size computation |
| 19 |
// and incorrectly specifying the buffer size to strlen. |
| 20 |
char buffer[1]; |
| 21 |
static const char test_buffer[] = {'4','2'}; |
| 22 |
__builtin_strcpy(buffer, test_buffer + 0); |
| 23 |
__builtin_strcpy(buffer, test_buffer + 1); |
| 24 |
// Note: these show that we will not issue a fortify warning when the source |
| 25 |
// buffer is not null terminated. |
| 26 |
__builtin_strcpy(buffer, test_buffer + 2); |
| 27 |
__builtin_strcpy(buffer, test_buffer + 3); |
| 28 |
|
| 29 |
// Verifying strlen computes from the correct starting point. |
| 30 |
_Static_assert(__builtin_strlen("42" + 1) == 1); |
| 31 |
_Static_assert(__builtin_strlen("42" + 2) == 0); |
| 32 |
_Static_assert(__builtin_strlen("42" + 3)); |
| 33 |
// expected-error@-1 {{static assertion expression is not an integral constant expression}} |
| 34 |
_Static_assert(__builtin_strlen("42" + 4)); |
| 35 |
// expected-error@-1 {{static assertion expression is not an integral constant expression}} |
| 36 |
// expected-note@-2 {{cannot refer to element 4 of array of 3 elements in a constant expression}} |
| 37 |
} |