| 1 |
/* PR tree-optimization/104119 - unexpected -Wformat-overflow after strlen |
| 2 |
in ILP32 since Ranger integration |
| 3 |
Verify that unlike -Wformat-overflow the sprintf optimization doesn't |
| 4 |
assume the length of a string isn't bounded by the size of the array |
| 5 |
member it's stored in. |
| 6 |
{ dg-do compile } |
| 7 |
{ dg-options "-O2 -Wall -fdump-tree-optimized" } */ |
| 8 |
|
| 9 |
typedef __SIZE_TYPE__ size_t; |
| 10 |
|
| 11 |
void* memcpy (void*, const void*, size_t); |
| 12 |
int snprintf (char*, size_t, const char*, ...); |
| 13 |
char* strcpy (char*, const char*); |
| 14 |
size_t strlen (const char*); |
| 15 |
|
| 16 |
extern void keep_call_on_line (int); |
| 17 |
extern void elim_call_on_line (int); |
| 18 |
|
| 19 |
void sink (void*, ...); |
| 20 |
|
| 21 |
struct __attribute__ ((packed)) S |
| 22 |
{ |
| 23 |
char a4[4], b4[4], ax[]; |
| 24 |
}; |
| 25 |
|
| 26 |
extern struct S es; |
| 27 |
|
| 28 |
void test_extern_decl_memcpy (void) |
| 29 |
{ |
| 30 |
struct S *p = &es; |
| 31 |
|
| 32 |
/* Set strlen (P->A4) to [3, PTRDIFF - 2]. */ |
| 33 |
memcpy (p->a4, "123", 3); |
| 34 |
int i = snprintf (0, 0, "%s", p->a4); |
| 35 |
if (i > 4) |
| 36 |
keep_call_on_line (__LINE__); |
| 37 |
} |
| 38 |
|
| 39 |
void test_extern_decl_strcpy_3 (void) |
| 40 |
{ |
| 41 |
struct S *p = &es; |
| 42 |
|
| 43 |
/* Set strlen (P->A4) to 3. */ |
| 44 |
strcpy (p->a4, "123"); |
| 45 |
int i = snprintf (0, 0, "%s", p->a4); |
| 46 |
if (i > 4) |
| 47 |
elim_call_on_line (__LINE__); |
| 48 |
} |
| 49 |
|
| 50 |
void test_extern_decl_strcpy_X (const char *s) |
| 51 |
{ |
| 52 |
struct S *p = &es; |
| 53 |
|
| 54 |
/* Set strlen (P->A4) to [0, PTRDIFF_MAX - 2]. */ |
| 55 |
strcpy (p->a4, s); |
| 56 |
int i = snprintf (0, 0, "%s", p->a4); |
| 57 |
if (i > 4) |
| 58 |
keep_call_on_line (__LINE__); |
| 59 |
} |
| 60 |
|
| 61 |
size_t test_extern_decl_strlen (void) |
| 62 |
{ |
| 63 |
struct S *p = &es; |
| 64 |
|
| 65 |
/* Set strlen (P->A4) to [0, PTRDIFF - 2]. */ |
| 66 |
size_t n = strlen (p->a4); |
| 67 |
int i = snprintf (0, 0, "%s", p->a4); |
| 68 |
if (i > 4) |
| 69 |
keep_call_on_line (__LINE__); |
| 70 |
return n; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
static struct S ss; |
| 75 |
|
| 76 |
/* Store and read SS to prevent optimizers from assuming it's unchanged. */ |
| 77 |
|
| 78 |
extern void set_ss (struct S *p) |
| 79 |
{ |
| 80 |
if (ss.a4[(unsigned char)*p->a4]) |
| 81 |
__builtin_memcpy (&ss, p, sizeof ss); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
void test_static_decl_memcpy (void) |
| 86 |
{ |
| 87 |
struct S *p = &ss; |
| 88 |
|
| 89 |
/* Set strlen (P->A4) to [3, PTRDIFF - 2]. */ |
| 90 |
memcpy (p->a4, "123", 3); |
| 91 |
int i = snprintf (0, 0, "%s", p->a4); |
| 92 |
if (i > 4) |
| 93 |
keep_call_on_line (__LINE__); |
| 94 |
} |
| 95 |
|
| 96 |
void test_static_decl_strcpy_3 (void) |
| 97 |
{ |
| 98 |
struct S *p = &ss; |
| 99 |
|
| 100 |
/* Set strlen (P->A4) to 3. */ |
| 101 |
strcpy (p->a4, "123"); |
| 102 |
int i = snprintf (0, 0, "%s", p->a4); |
| 103 |
if (i > 4) |
| 104 |
elim_call_on_line (__LINE__); |
| 105 |
} |
| 106 |
|
| 107 |
void test_static_decl_strcpy_X (const char *s) |
| 108 |
{ |
| 109 |
struct S *p = &ss; |
| 110 |
|
| 111 |
/* Set strlen (P->A4) to [0, PTRDIFF_MAX - 2]. */ |
| 112 |
strcpy (p->a4, s); |
| 113 |
int i = snprintf (0, 0, "%s", p->a4); |
| 114 |
if (i > 4) |
| 115 |
keep_call_on_line (__LINE__); |
| 116 |
} |
| 117 |
|
| 118 |
size_t test_static_decl_strlen (void) |
| 119 |
{ |
| 120 |
struct S *p = &ss; |
| 121 |
|
| 122 |
/* Set strlen (P->A4) to [0, PTRDIFF - 2]. */ |
| 123 |
size_t n = strlen (p->a4); |
| 124 |
int i = snprintf (0, 0, "%s", p->a4); |
| 125 |
if (i > 4) |
| 126 |
keep_call_on_line (__LINE__); |
| 127 |
return n; |
| 128 |
} |
| 129 |
|
| 130 |
/* { dg-final { scan-tree-dump-times "keep_call_on_line" 6 "optimized" } } |
| 131 |
{ dg-final { scan-tree-dump-not "elim_call_on_line" "optimized" } } */ |