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
7 { dg-options "-O2 -Wall -fdump-tree-optimized" } */
9 typedef __SIZE_TYPE__
size_t;
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*);
16 extern void keep_call_on_line (int);
17 extern void elim_call_on_line (int);
19 void sink (void*, ...);
21 struct __attribute__ ((packed
)) S
23 char a4
[4], b4
[4], ax
[];
28 void test_extern_decl_memcpy (void)
32 /* Set strlen (P->A4) to [3, PTRDIFF - 2]. */
33 memcpy (p
->a4
, "123", 3);
34 int i
= snprintf (0, 0, "%s", p
->a4
);
36 keep_call_on_line (__LINE__
);
39 void test_extern_decl_strcpy_3 (void)
43 /* Set strlen (P->A4) to 3. */
44 strcpy (p
->a4
, "123");
45 int i
= snprintf (0, 0, "%s", p
->a4
);
47 elim_call_on_line (__LINE__
);
50 void test_extern_decl_strcpy_X (const char *s
)
54 /* Set strlen (P->A4) to [0, PTRDIFF_MAX - 2]. */
56 int i
= snprintf (0, 0, "%s", p
->a4
);
58 keep_call_on_line (__LINE__
);
61 size_t test_extern_decl_strlen (void)
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
);
69 keep_call_on_line (__LINE__
);
76 /* Store and read SS to prevent optimizers from assuming it's unchanged. */
78 extern void set_ss (struct S
*p
)
80 if (ss
.a4
[(unsigned char)*p
->a4
])
81 __builtin_memcpy (&ss
, p
, sizeof ss
);
85 void test_static_decl_memcpy (void)
89 /* Set strlen (P->A4) to [3, PTRDIFF - 2]. */
90 memcpy (p
->a4
, "123", 3);
91 int i
= snprintf (0, 0, "%s", p
->a4
);
93 keep_call_on_line (__LINE__
);
96 void test_static_decl_strcpy_3 (void)
100 /* Set strlen (P->A4) to 3. */
101 strcpy (p
->a4
, "123");
102 int i
= snprintf (0, 0, "%s", p
->a4
);
104 elim_call_on_line (__LINE__
);
107 void test_static_decl_strcpy_X (const char *s
)
111 /* Set strlen (P->A4) to [0, PTRDIFF_MAX - 2]. */
113 int i
= snprintf (0, 0, "%s", p
->a4
);
115 keep_call_on_line (__LINE__
);
118 size_t test_static_decl_strlen (void)
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
);
126 keep_call_on_line (__LINE__
);
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" } } */