annotate gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf-8.c @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* PR tree-optimization/77671 - missing -Wformat-overflow warning
kono
parents:
diff changeset
2 on sprintf overflow with "%s"
kono
parents:
diff changeset
3
kono
parents:
diff changeset
4 Negative test verifying that sprintf family calls that must not
kono
parents:
diff changeset
5 be transformed into calls to other functions (such as memcpy)
kono
parents:
diff changeset
6 are preserved.
kono
parents:
diff changeset
7
kono
parents:
diff changeset
8 { dg-compile }
kono
parents:
diff changeset
9 { dg-options "-O2 -Wformat -Wno-format-truncation -Wno-format-zero-length -fdump-tree-optimized" } */
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 void sink (char*, ...);
kono
parents:
diff changeset
12
kono
parents:
diff changeset
13 extern char buffer[];
kono
parents:
diff changeset
14
kono
parents:
diff changeset
15 /* String exactly 4100 characters long (plus the terminating NUL). */
kono
parents:
diff changeset
16 extern const char s4100[4101];
kono
parents:
diff changeset
17
kono
parents:
diff changeset
18 void test_sprintf (const char *s)
kono
parents:
diff changeset
19 {
kono
parents:
diff changeset
20 /* Macros to test the function call while eignoring and using
kono
parents:
diff changeset
21 the return value, respectively. */
kono
parents:
diff changeset
22 #define IGN(...) __builtin_sprintf (buffer, __VA_ARGS__), sink (buffer)
kono
parents:
diff changeset
23 #define USE(...) sink (buffer, __builtin_sprintf (buffer, __VA_ARGS__))
kono
parents:
diff changeset
24
kono
parents:
diff changeset
25 /* All of the following calls to sprintf must be emitted (and not
kono
parents:
diff changeset
26 transformed into memcpy, strcpy, or similar). */
kono
parents:
diff changeset
27
kono
parents:
diff changeset
28 /* Verify that a sprintf call with output in excess of the maximum
kono
parents:
diff changeset
29 of 4095 bytes is not transformed into memcpy/strcpy when its
kono
parents:
diff changeset
30 return value is used (the call may fail with EOVERFLOW but
kono
parents:
diff changeset
31 the error is only detectable when the function's negative return
kono
parents:
diff changeset
32 value indicates errno is valid ). */
kono
parents:
diff changeset
33 USE (s4100);
kono
parents:
diff changeset
34
kono
parents:
diff changeset
35 USE ("%s", s4100);
kono
parents:
diff changeset
36
kono
parents:
diff changeset
37 /* Same as above but with string of unknown length (which could
kono
parents:
diff changeset
38 be in excess of 4K long). */
kono
parents:
diff changeset
39 USE (s);
kono
parents:
diff changeset
40 USE ("%s", s);
kono
parents:
diff changeset
41 }
kono
parents:
diff changeset
42
kono
parents:
diff changeset
43
kono
parents:
diff changeset
44 void test_snprintf (void)
kono
parents:
diff changeset
45 {
kono
parents:
diff changeset
46 #undef IGN
kono
parents:
diff changeset
47 #define IGN(N, ...) __builtin_snprintf (buffer, N, __VA_ARGS__); sink (buffer)
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 /* All of the following calls to sprintf must be emitted (and not
kono
parents:
diff changeset
50 transformed into memcpy, strcpy, or similar). */
kono
parents:
diff changeset
51
kono
parents:
diff changeset
52 /* Truncated output could be optimized into strncpy (not done yet). */
kono
parents:
diff changeset
53 IGN (1, "123");
kono
parents:
diff changeset
54 IGN (1, s4100);
kono
parents:
diff changeset
55
kono
parents:
diff changeset
56 IGN (1, "%s", "123");
kono
parents:
diff changeset
57 IGN (1, "%s", s4100);
kono
parents:
diff changeset
58
kono
parents:
diff changeset
59 /* Output in excess of the maximum of 4095 bytes. */
kono
parents:
diff changeset
60 IGN (4097, s4100);
kono
parents:
diff changeset
61
kono
parents:
diff changeset
62 IGN (4097, "%s", s4100);
kono
parents:
diff changeset
63 }
kono
parents:
diff changeset
64
kono
parents:
diff changeset
65
kono
parents:
diff changeset
66 void test_vsprintf (__builtin_va_list va)
kono
parents:
diff changeset
67 {
kono
parents:
diff changeset
68 #undef IGN
kono
parents:
diff changeset
69 #define IGN(fmt) __builtin_vsprintf (buffer, fmt, va); sink (buffer)
kono
parents:
diff changeset
70
kono
parents:
diff changeset
71 /* All of the following calls to vsprintf must be emitted (and not
kono
parents:
diff changeset
72 transformed into memcpy, strcpy, or similar). */
kono
parents:
diff changeset
73
kono
parents:
diff changeset
74 /* Output in excess of the maximum of 4095 bytes. */
kono
parents:
diff changeset
75 IGN (s4100);
kono
parents:
diff changeset
76 }
kono
parents:
diff changeset
77
kono
parents:
diff changeset
78
kono
parents:
diff changeset
79 void test_vsnprintf (__builtin_va_list va)
kono
parents:
diff changeset
80 {
kono
parents:
diff changeset
81 #undef IGN
kono
parents:
diff changeset
82 #define IGN(N, fmt) __builtin_vsnprintf (buffer, N, fmt, va); sink (buffer)
kono
parents:
diff changeset
83
kono
parents:
diff changeset
84 /* All of the following calls to vsnprintf must be emitted (and not
kono
parents:
diff changeset
85 transformed into memcpy, strcpy, or similar). */
kono
parents:
diff changeset
86
kono
parents:
diff changeset
87 /* Truncated output could be optimized into strncpy (not done yet). */
kono
parents:
diff changeset
88 IGN (1, "123");
kono
parents:
diff changeset
89 IGN (1, s4100);
kono
parents:
diff changeset
90
kono
parents:
diff changeset
91 /* Output in excess of the maximum of 4095 bytes. */
kono
parents:
diff changeset
92 IGN (4097, s4100);
kono
parents:
diff changeset
93 }
kono
parents:
diff changeset
94
kono
parents:
diff changeset
95 /* { dg-final { scan-tree-dump-times "builtin_sprintf" 4 "optimized" } }
kono
parents:
diff changeset
96 { dg-final { scan-tree-dump-times "builtin_snprintf" 6 "optimized" } }
kono
parents:
diff changeset
97 { dg-final { scan-tree-dump-times "builtin_vsprintf" 1 "optimized" } }
kono
parents:
diff changeset
98 { dg-final { scan-tree-dump-times "builtin_vsnprintf" 3 "optimized" } } */
kono
parents:
diff changeset
99
kono
parents:
diff changeset
100 #define S10 "0123456789"
kono
parents:
diff changeset
101 #define S100 S10 S10 S10 S10 S10 S10 S10 S10 S10 S10
kono
parents:
diff changeset
102 #define S1000 S100 S100 S100 S100 S100 S100 S100 S100 S100 S100
kono
parents:
diff changeset
103
kono
parents:
diff changeset
104 const char s4100[4101] = S1000 S1000 S1000 S1000 S100;