| 1 |
/* PR tree-optimization/83075 - Invalid strncpy optimization */ |
| 2 |
/* { dg-do run } */ |
| 3 |
/* { dg-options "-O2 -Wstringop-truncation" } */ |
| 4 |
|
| 5 |
typedef __SIZE_TYPE__ size_t; |
| 6 |
|
| 7 |
__attribute__((noipa)) size_t |
| 8 |
foo (char *p, char *q, size_t *r) |
| 9 |
{ |
| 10 |
size_t n0 = __builtin_strlen (p); |
| 11 |
__builtin_strncpy (q, p, n0); /* { dg-warning "\\\[-Wstringop-truncation" } */ |
| 12 |
size_t n1 = __builtin_strlen (p); |
| 13 |
*r = n0; |
| 14 |
return n1; |
| 15 |
} |
| 16 |
|
| 17 |
int |
| 18 |
main () |
| 19 |
{ |
| 20 |
char a[8] = ""; |
| 21 |
__builtin_strcpy (a, "123"); |
| 22 |
size_t n0 = __builtin_strlen (a); |
| 23 |
__builtin_strncpy (a + 3, a, n0); /* { dg-warning "specified bound depends on the length" } */ |
| 24 |
size_t n1 = __builtin_strlen (a); |
| 25 |
if (n1 == n0) |
| 26 |
__builtin_abort (); |
| 27 |
a[6] = '7'; |
| 28 |
__builtin_strcpy (a, "456"); |
| 29 |
size_t n2; |
| 30 |
if (foo (a, a + 3, &n2) != 7 || n2 != 3) |
| 31 |
__builtin_abort (); |
| 32 |
if (__builtin_memcmp (a, "4564567", sizeof "4564567")) |
| 33 |
__builtin_abort (); |
| 34 |
return 0; |
| 35 |
} |