Debian Codegraph @

libstdc++-v3/testsuite/std/format/string.cc

raw
1 // { dg-do run { target c++20 } }
2
3 #include <format>
4 #include <testsuite_hooks.h>
5
6 template<typename... Args>
7 bool
8 is_format_string_for(const char* str, Args&&... args)
9 {
10 try {
11 (void) std::vformat(str, std::make_format_args(args...));
12 return true;
13 } catch (const std::format_error&) {
14 return false;
15 }
16 }
17
18 template<typename... Args>
19 bool
20 is_format_string_for(const wchar_t* str, Args&&... args)
21 {
22 try {
23 (void) std::vformat(str, std::make_wformat_args(args...));
24 return true;
25 } catch (const std::format_error&) {
26 return false;
27 }
28 }
29
30 void
31 test_no_args()
32 {
33 VERIFY( is_format_string_for("") );
34 VERIFY( is_format_string_for("chars") );
35 VERIFY( is_format_string_for(" The Great Escape {{}} ") );
36
37 VERIFY( ! is_format_string_for("{") );
38 VERIFY( ! is_format_string_for("}") );
39 VERIFY( ! is_format_string_for("}{") );
40 VERIFY( ! is_format_string_for("{{}") );
41 VERIFY( ! is_format_string_for("{{{") );
42 VERIFY( ! is_format_string_for("{{{{{") );
43 }
44
45 void
46 test_indexing()
47 {
48 VERIFY( is_format_string_for("{} to {}", "a", "b") ); // automatic indexing
49 VERIFY( is_format_string_for("{1} to {0}", "a", "b") ); // manual indexing
50 VERIFY( ! is_format_string_for("{0} to {}", "a", "b") ); // mixed indexing
51 VERIFY( ! is_format_string_for("{} to {1}", "a", "b") ); // mixed indexing
52
53 VERIFY( is_format_string_for("{} {} {}", 1, 2, 3) );
54 VERIFY( is_format_string_for("{} {} {}", 1, 2, 3, 4) );
55 VERIFY( is_format_string_for("{0} {1} {2}", 1, 2, 3, 4) );
56 VERIFY( is_format_string_for("{1} {2} {3}", 1, 2, 3, 4) );
57 VERIFY( is_format_string_for("{3} {3} {3}", 1, 2, 3, 4) );
58
59 VERIFY( ! is_format_string_for("{2}", 1, 2) );
60
61 VERIFY( ! is_format_string_for("{0} {}", 1) );
62 VERIFY( ! is_format_string_for("{} {0}", 1) );
63 }
64
65 #if __cpp_lib_format_ranges
66 constexpr bool escaped_strings_supported = true;
67 #else
68 constexpr bool escaped_strings_supported = false;
69 #endif
70
71 void
72 test_format_spec()
73 {
74 VERIFY( is_format_string_for("{:}", 1) );
75 VERIFY( is_format_string_for("{0:}", 1) );
76 VERIFY( is_format_string_for("{2:}", 1, 2, 3) );
77 VERIFY( is_format_string_for("{0:s} {0:}", "str") );
78 VERIFY( is_format_string_for("{0:} {0:c}", 'c') );
79 VERIFY( is_format_string_for("{0:p} {0:}", nullptr) );
80 VERIFY( is_format_string_for("{:d} {:+d}", true, true) );
81 VERIFY( is_format_string_for("{:0<-#03Ld}", 1) );
82 VERIFY( is_format_string_for("{1:0<-#03.4Lf}", 1, 2.3) );
83 VERIFY( is_format_string_for("{1:3.3f}", 1, 2.3) );
84 VERIFY( is_format_string_for("{:#d}", 'c') );
85 VERIFY( is_format_string_for("{:#d}", true) );
86 VERIFY( is_format_string_for("{0:s} {0:?}", "str") == escaped_strings_supported );
87 VERIFY( is_format_string_for("{0:} {0:?}", 'c') == escaped_strings_supported );
88
89 // Invalid sign options.
90 VERIFY( ! is_format_string_for("{:+}", "str") );
91 VERIFY( ! is_format_string_for("{:+s}", "str") );
92 VERIFY( ! is_format_string_for("{:+}", 'c') );
93 VERIFY( ! is_format_string_for("{:+c}", 'c') );
94 VERIFY( ! is_format_string_for("{:+p}", nullptr) );
95 VERIFY( ! is_format_string_for("{:+}", true) );
96 VERIFY( ! is_format_string_for("{:+s}", true) );
97 VERIFY( ! is_format_string_for("{:+?}", "str") );
98 VERIFY( ! is_format_string_for("{:+?}", 'c') );
99
100 // Invalid alternate forms.
101 VERIFY( ! is_format_string_for("{:#}", "str") );
102 VERIFY( ! is_format_string_for("{:#s}", "str") );
103 VERIFY( ! is_format_string_for("{:#}", 'c') );
104 VERIFY( ! is_format_string_for("{:#c}", 'c') );
105 VERIFY( ! is_format_string_for("{:#}", true) );
106 VERIFY( ! is_format_string_for("{:#s}", true) );
107 VERIFY( ! is_format_string_for("{:#}", nullptr) );
108 VERIFY( ! is_format_string_for("{:#p}", nullptr) );
109 VERIFY( ! is_format_string_for("{:#?}", "str") );
110 VERIFY( ! is_format_string_for("{:#?}", 'c') );
111
112 // The 0 option is not valid for charT and bool.
113 VERIFY( ! is_format_string_for("{:0c}", 'c') );
114 VERIFY( ! is_format_string_for("{:0s}", true) );
115
116 // Dynamic width arg must be a standar integer type.
117 VERIFY( ! is_format_string_for("{:{}d}", 1, 1.5) );
118 VERIFY( ! is_format_string_for("{:{}d}", 1, true) );
119 VERIFY( ! is_format_string_for("{:{}d}", 1, "str") );
120 VERIFY( ! is_format_string_for("{:{}d}", 1, nullptr) );
121 #ifdef __SIZEOF_INT128__
122 VERIFY( ! is_format_string_for("{:{}d}", 1, static_cast<__int128>(1)) );
123 #endif
124
125 // Precision only valid for string and floating-point types.
126 VERIFY( ! is_format_string_for("{:.3d}", 1) );
127 VERIFY( ! is_format_string_for("{:3.3d}", 1) );
128 VERIFY( is_format_string_for("{:3.3s}", "str") );
129 VERIFY( ! is_format_string_for("{:3.3s}", 'c') );
130 VERIFY( ! is_format_string_for("{:3.3p}", nullptr) );
131
132 // Dynamic precision arg must be a standard integer type.
133 VERIFY( ! is_format_string_for("{:.{}f}", 1.0, 1.5) );
134 VERIFY( ! is_format_string_for("{:.{}f}", 1.0, true) );
135 VERIFY( ! is_format_string_for("{:.{}f}", 1.0, "str") );
136 VERIFY( ! is_format_string_for("{:.{}f}", 1.0, nullptr) );
137 #ifdef __SIZEOF_INT128__
138 VERIFY( ! is_format_string_for("{:{}f}", 1.0, static_cast<unsigned __int128>(1)) );
139 #endif
140
141 // Invalid presentation types for integers.
142 VERIFY( ! is_format_string_for("{:f}", 1) );
143 VERIFY( ! is_format_string_for("{:s}", 1) );
144 VERIFY( ! is_format_string_for("{:g}", 1) );
145 VERIFY( ! is_format_string_for("{:E}", 1) );
146 VERIFY( ! is_format_string_for("{:D}", 1) );
147
148 // Invalid presentation types for floating-point types.
149 VERIFY( ! is_format_string_for("{:d}", 1.2) );
150 VERIFY( ! is_format_string_for("{:b}", 1.2) );
151 VERIFY( ! is_format_string_for("{:x}", 1.2) );
152 VERIFY( ! is_format_string_for("{:s}", 1.2) );
153
154 // Invalid presentation types for strings.
155 VERIFY( ! is_format_string_for("{:S}", "str") );
156 VERIFY( ! is_format_string_for("{:d}", "str") );
157
158 // Maximum integer value supported for widths and precisions is USHRT_MAX.
159 VERIFY( is_format_string_for("{:65535}", 1) );
160 VERIFY( is_format_string_for(L"{:65535}", 1) );
161 VERIFY( ! is_format_string_for("{:65536}", 1) );
162 VERIFY( ! is_format_string_for(L"{:65536}", 1) );
163 VERIFY( ! is_format_string_for("{:9999999}", 1) );
164 VERIFY( ! is_format_string_for(L"{:9999999}", 1) );
165 }
166
167 void
168 test_pr110862()
169 {
170 try {
171 int i = 1;
172 // PR libstdc++/110862 out-of-bounds read on invalid format string
173 (void) std::vformat("{0:{0}", std::make_format_args(i));
174 VERIFY( false );
175 } catch (const std::format_error& e) {
176 std::string_view what = e.what();
177 VERIFY( what.find("unmatched '{'") != what.npos );
178 }
179 }
180
181 void
182 test_pr110974()
183 {
184 try {
185 double d = 1.0;
186 int i = 1;
187 // PR libstdc++/110974 out of bounds read on invalid format string "{:{}."
188 std::string_view fmt{"{:{}.0", 5}; // "0" is not part of the format string.
189 (void) std::vformat(fmt, std::make_format_args(d, i));
190 VERIFY( false );
191 } catch (const std::format_error& e) {
192 std::string_view what = e.what();
193 // GCC 13.2 throws "invalid width or precision in format-spec" after
194 // trying to parse the "0" past-the-end of the format string.
195 // There should be an exception before even trying that:
196 VERIFY( what.find("missing precision after '.'") != what.npos );
197 }
198 }
199
200 int main()
201 {
202 test_no_args();
203 test_indexing();
204 test_format_spec();
205 test_pr110862();
206 test_pr110974();
207 }