]>
Commit | Line | Data |
---|---|---|
aeee4812 | 1 | /* Copyright (C) 2016-2023 Free Software Foundation, Inc. |
88d0c3f0 MS |
2 | Contributed by Martin Sebor <msebor@redhat.com>. |
3 | ||
4 | This file is part of GCC. | |
5 | ||
6 | GCC is free software; you can redistribute it and/or modify it under | |
7 | the terms of the GNU General Public License as published by the Free | |
8 | Software Foundation; either version 3, or (at your option) any later | |
9 | version. | |
10 | ||
11 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 | for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GCC; see the file COPYING3. If not see | |
18 | <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | /* This file implements the printf-return-value pass. The pass does | |
21 | two things: 1) it analyzes calls to formatted output functions like | |
22 | sprintf looking for possible buffer overflows and calls to bounded | |
23 | functions like snprintf for early truncation (and under the control | |
24 | of the -Wformat-length option issues warnings), and 2) under the | |
25 | control of the -fprintf-return-value option it folds the return | |
26 | value of safe calls into constants, making it possible to eliminate | |
27 | code that depends on the value of those constants. | |
28 | ||
29 | For all functions (bounded or not) the pass uses the size of the | |
30 | destination object. That means that it will diagnose calls to | |
31 | snprintf not on the basis of the size specified by the function's | |
d2ae6d5c | 32 | second argument but rather on the basis of the size the first |
88d0c3f0 MS |
33 | argument points to (if possible). For bound-checking built-ins |
34 | like __builtin___snprintf_chk the pass uses the size typically | |
35 | determined by __builtin_object_size and passed to the built-in | |
36 | by the Glibc inline wrapper. | |
37 | ||
38 | The pass handles all forms standard sprintf format directives, | |
39 | including character, integer, floating point, pointer, and strings, | |
5fe39ec0 | 40 | with the standard C flags, widths, and precisions. For integers |
88d0c3f0 | 41 | and strings it computes the length of output itself. For floating |
d2ae6d5c | 42 | point it uses MPFR to format known constants with up and down |
88d0c3f0 MS |
43 | rounding and uses the resulting range of output lengths. For |
44 | strings it uses the length of string literals and the sizes of | |
45 | character arrays that a character pointer may point to as a bound | |
46 | on the longest string. */ | |
47 | ||
48 | #include "config.h" | |
49 | #include "system.h" | |
50 | #include "coretypes.h" | |
51 | #include "backend.h" | |
52 | #include "tree.h" | |
53 | #include "gimple.h" | |
54 | #include "tree-pass.h" | |
55 | #include "ssa.h" | |
ba206889 | 56 | #include "gimple-iterator.h" |
88d0c3f0 MS |
57 | #include "gimple-fold.h" |
58 | #include "gimple-pretty-print.h" | |
59 | #include "diagnostic-core.h" | |
60 | #include "fold-const.h" | |
88d0c3f0 MS |
61 | #include "tree-ssa.h" |
62 | #include "tree-object-size.h" | |
88d0c3f0 | 63 | #include "tree-cfg.h" |
d7b0fcaa | 64 | #include "tree-ssa-propagate.h" |
88d0c3f0 MS |
65 | #include "calls.h" |
66 | #include "cfgloop.h" | |
965779b4 AH |
67 | #include "tree-scalar-evolution.h" |
68 | #include "tree-ssa-loop.h" | |
88d0c3f0 | 69 | #include "intl.h" |
d127ae9f | 70 | #include "langhooks.h" |
88d0c3f0 | 71 | |
91e3ec29 | 72 | #include "attribs.h" |
88d0c3f0 | 73 | #include "builtins.h" |
2a837de2 | 74 | #include "pointer-query.h" |
88d0c3f0 MS |
75 | #include "stor-layout.h" |
76 | ||
77 | #include "realmpfr.h" | |
78 | #include "target.h" | |
88d0c3f0 MS |
79 | |
80 | #include "cpplib.h" | |
81 | #include "input.h" | |
82 | #include "toplev.h" | |
83 | #include "substring-locations.h" | |
84 | #include "diagnostic.h" | |
a09ca93a | 85 | #include "domwalk.h" |
aa67d03c JL |
86 | #include "alloc-pool.h" |
87 | #include "vr-values.h" | |
22fca489 | 88 | #include "tree-ssa-strlen.h" |
937a86b4 | 89 | #include "tree-dfa.h" |
88d0c3f0 | 90 | |
1eb4547b MS |
91 | /* The likely worst case value of MB_LEN_MAX for the target, large enough |
92 | for UTF-8. Ideally, this would be obtained by a target hook if it were | |
93 | to be used for optimization but it's good enough as is for warnings. */ | |
954b452a | 94 | #define target_mb_len_max() 6 |
1eb4547b | 95 | |
cfce1a4a MS |
96 | /* The maximum number of bytes a single non-string directive can result |
97 | in. This is the result of printf("%.*Lf", INT_MAX, -LDBL_MAX) for | |
98 | LDBL_MAX_10_EXP of 4932. */ | |
99 | #define IEEE_MAX_10_EXP 4932 | |
100 | #define target_dir_max() (target_int_max () + IEEE_MAX_10_EXP + 2) | |
101 | ||
88d0c3f0 MS |
102 | namespace { |
103 | ||
02f1ef40 MS |
104 | /* Set to the warning level for the current function which is equal |
105 | either to warn_format_trunc for bounded functions or to | |
106 | warn_format_overflow otherwise. */ | |
107 | ||
108 | static int warn_level; | |
109 | ||
5d93da1d MS |
110 | /* The minimum, maximum, likely, and unlikely maximum number of bytes |
111 | of output either a formatting function or an individual directive | |
112 | can result in. */ | |
113 | ||
114 | struct result_range | |
115 | { | |
116 | /* The absolute minimum number of bytes. The result of a successful | |
117 | conversion is guaranteed to be no less than this. (An erroneous | |
118 | conversion can be indicated by MIN > HOST_WIDE_INT_MAX.) */ | |
119 | unsigned HOST_WIDE_INT min; | |
120 | /* The likely maximum result that is used in diagnostics. In most | |
121 | cases MAX is the same as the worst case UNLIKELY result. */ | |
122 | unsigned HOST_WIDE_INT max; | |
123 | /* The likely result used to trigger diagnostics. For conversions | |
124 | that result in a range of bytes [MIN, MAX], LIKELY is somewhere | |
125 | in that range. */ | |
126 | unsigned HOST_WIDE_INT likely; | |
d2ae6d5c | 127 | /* In rare cases (e.g., for multibyte characters) UNLIKELY gives |
5d93da1d MS |
128 | the worst cases maximum result of a directive. In most cases |
129 | UNLIKELY == MAX. UNLIKELY is used to control the return value | |
130 | optimization but not in diagnostics. */ | |
131 | unsigned HOST_WIDE_INT unlikely; | |
132 | }; | |
133 | ||
88d0c3f0 MS |
134 | /* Return the value of INT_MIN for the target. */ |
135 | ||
de6aa933 | 136 | static inline HOST_WIDE_INT |
88d0c3f0 MS |
137 | target_int_min () |
138 | { | |
de6aa933 | 139 | return tree_to_shwi (TYPE_MIN_VALUE (integer_type_node)); |
1eb4547b MS |
140 | } |
141 | ||
142 | /* Return the value of INT_MAX for the target. */ | |
143 | ||
144 | static inline unsigned HOST_WIDE_INT | |
145 | target_int_max () | |
146 | { | |
de6aa933 | 147 | return tree_to_uhwi (TYPE_MAX_VALUE (integer_type_node)); |
1eb4547b MS |
148 | } |
149 | ||
150 | /* Return the value of SIZE_MAX for the target. */ | |
151 | ||
152 | static inline unsigned HOST_WIDE_INT | |
153 | target_size_max () | |
154 | { | |
de6aa933 | 155 | return tree_to_uhwi (TYPE_MAX_VALUE (size_type_node)); |
88d0c3f0 MS |
156 | } |
157 | ||
d127ae9f MS |
158 | /* A straightforward mapping from the execution character set to the host |
159 | character set indexed by execution character. */ | |
160 | ||
161 | static char target_to_host_charmap[256]; | |
162 | ||
163 | /* Initialize a mapping from the execution character set to the host | |
164 | character set. */ | |
165 | ||
166 | static bool | |
167 | init_target_to_host_charmap () | |
168 | { | |
169 | /* If the percent sign is non-zero the mapping has already been | |
170 | initialized. */ | |
171 | if (target_to_host_charmap['%']) | |
172 | return true; | |
173 | ||
174 | /* Initialize the target_percent character (done elsewhere). */ | |
175 | if (!init_target_chars ()) | |
176 | return false; | |
177 | ||
178 | /* The subset of the source character set used by printf conversion | |
179 | specifications (strictly speaking, not all letters are used but | |
180 | they are included here for the sake of simplicity). The dollar | |
181 | sign must be included even though it's not in the basic source | |
182 | character set. */ | |
183 | const char srcset[] = " 0123456789!\"#%&'()*+,-./:;<=>?[\\]^_{|}~$" | |
184 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; | |
185 | ||
186 | /* Set the mapping for all characters to some ordinary value (i,e., | |
187 | not none used in printf conversion specifications) and overwrite | |
188 | those that are used by conversion specifications with their | |
189 | corresponding values. */ | |
190 | memset (target_to_host_charmap + 1, '?', sizeof target_to_host_charmap - 1); | |
191 | ||
192 | /* Are the two sets of characters the same? */ | |
193 | bool all_same_p = true; | |
194 | ||
195 | for (const char *pc = srcset; *pc; ++pc) | |
196 | { | |
197 | /* Slice off the high end bits in case target characters are | |
198 | signed. All values are expected to be non-nul, otherwise | |
199 | there's a problem. */ | |
200 | if (unsigned char tc = lang_hooks.to_target_charset (*pc)) | |
201 | { | |
202 | target_to_host_charmap[tc] = *pc; | |
203 | if (tc != *pc) | |
204 | all_same_p = false; | |
205 | } | |
206 | else | |
207 | return false; | |
208 | ||
209 | } | |
210 | ||
211 | /* Set the first element to a non-zero value if the mapping | |
212 | is 1-to-1, otherwise leave it clear (NUL is assumed to be | |
213 | the same in both character sets). */ | |
214 | target_to_host_charmap[0] = all_same_p; | |
215 | ||
216 | return true; | |
217 | } | |
218 | ||
219 | /* Return the host source character corresponding to the character | |
220 | CH in the execution character set if one exists, or some innocuous | |
221 | (non-special, non-nul) source character otherwise. */ | |
222 | ||
223 | static inline unsigned char | |
224 | target_to_host (unsigned char ch) | |
225 | { | |
226 | return target_to_host_charmap[ch]; | |
227 | } | |
228 | ||
229 | /* Convert an initial substring of the string TARGSTR consisting of | |
230 | characters in the execution character set into a string in the | |
231 | source character set on the host and store up to HOSTSZ characters | |
232 | in the buffer pointed to by HOSTR. Return HOSTR. */ | |
233 | ||
234 | static const char* | |
235 | target_to_host (char *hostr, size_t hostsz, const char *targstr) | |
236 | { | |
237 | /* Make sure the buffer is reasonably big. */ | |
238 | gcc_assert (hostsz > 4); | |
239 | ||
240 | /* The interesting subset of source and execution characters are | |
241 | the same so no conversion is necessary. However, truncate | |
242 | overlong strings just like the translated strings are. */ | |
243 | if (target_to_host_charmap['\0'] == 1) | |
244 | { | |
c9c5e188 JJ |
245 | size_t len = strlen (targstr); |
246 | if (len >= hostsz) | |
247 | { | |
248 | memcpy (hostr, targstr, hostsz - 4); | |
249 | strcpy (hostr + hostsz - 4, "..."); | |
250 | } | |
251 | else | |
252 | memcpy (hostr, targstr, len + 1); | |
d127ae9f MS |
253 | return hostr; |
254 | } | |
255 | ||
256 | /* Convert the initial substring of TARGSTR to the corresponding | |
257 | characters in the host set, appending "..." if TARGSTR is too | |
258 | long to fit. Using the static buffer assumes the function is | |
259 | not called in between sequence points (which it isn't). */ | |
260 | for (char *ph = hostr; ; ++targstr) | |
261 | { | |
262 | *ph++ = target_to_host (*targstr); | |
263 | if (!*targstr) | |
264 | break; | |
265 | ||
c9c5e188 | 266 | if (size_t (ph - hostr) == hostsz) |
d127ae9f | 267 | { |
c9c5e188 | 268 | strcpy (ph - 4, "..."); |
d127ae9f MS |
269 | break; |
270 | } | |
271 | } | |
272 | ||
273 | return hostr; | |
274 | } | |
275 | ||
276 | /* Convert the sequence of decimal digits in the execution character | |
837865bd MS |
277 | starting at *PS to a HOST_WIDE_INT, analogously to strtol. Return |
278 | the result and set *PS to one past the last converted character. | |
279 | On range error set ERANGE to the digit that caused it. */ | |
d127ae9f | 280 | |
837865bd MS |
281 | static inline HOST_WIDE_INT |
282 | target_strtowi (const char **ps, const char **erange) | |
d127ae9f MS |
283 | { |
284 | unsigned HOST_WIDE_INT val = 0; | |
285 | for ( ; ; ++*ps) | |
286 | { | |
287 | unsigned char c = target_to_host (**ps); | |
288 | if (ISDIGIT (c)) | |
289 | { | |
290 | c -= '0'; | |
291 | ||
292 | /* Check for overflow. */ | |
837865bd | 293 | if (val > ((unsigned HOST_WIDE_INT) HOST_WIDE_INT_MAX - c) / 10LU) |
d127ae9f | 294 | { |
837865bd | 295 | val = HOST_WIDE_INT_MAX; |
d127ae9f MS |
296 | *erange = *ps; |
297 | ||
298 | /* Skip the remaining digits. */ | |
299 | do | |
300 | c = target_to_host (*++*ps); | |
301 | while (ISDIGIT (c)); | |
302 | break; | |
303 | } | |
304 | else | |
305 | val = val * 10 + c; | |
306 | } | |
307 | else | |
308 | break; | |
309 | } | |
310 | ||
311 | return val; | |
312 | } | |
313 | ||
88d0c3f0 MS |
314 | /* Given FORMAT, set *PLOC to the source location of the format string |
315 | and return the format string if it is known or null otherwise. */ | |
316 | ||
317 | static const char* | |
318 | get_format_string (tree format, location_t *ploc) | |
319 | { | |
88d0c3f0 MS |
320 | *ploc = EXPR_LOC_OR_LOC (format, input_location); |
321 | ||
6b2ffa6a | 322 | return c_getstr (format); |
88d0c3f0 MS |
323 | } |
324 | ||
346114b5 | 325 | /* For convenience and brevity, shorter named entrypoints of |
c896ecfe DM |
326 | format_string_diagnostic_t::emit_warning_va and |
327 | format_string_diagnostic_t::emit_warning_n_va. | |
346114b5 JJ |
328 | These have to be functions with the attribute so that exgettext |
329 | works properly. */ | |
88d0c3f0 | 330 | |
346114b5 JJ |
331 | static bool |
332 | ATTRIBUTE_GCC_DIAG (5, 6) | |
333 | fmtwarn (const substring_loc &fmt_loc, location_t param_loc, | |
e9e2bad7 MS |
334 | const char *corrected_substring, opt_code opt, |
335 | const char *gmsgid, ...) | |
346114b5 | 336 | { |
c896ecfe DM |
337 | format_string_diagnostic_t diag (fmt_loc, NULL, param_loc, NULL, |
338 | corrected_substring); | |
346114b5 JJ |
339 | va_list ap; |
340 | va_start (ap, gmsgid); | |
c896ecfe | 341 | bool warned = diag.emit_warning_va (opt, gmsgid, &ap); |
346114b5 | 342 | va_end (ap); |
88d0c3f0 | 343 | |
346114b5 JJ |
344 | return warned; |
345 | } | |
88d0c3f0 MS |
346 | |
347 | static bool | |
346114b5 JJ |
348 | ATTRIBUTE_GCC_DIAG (6, 8) ATTRIBUTE_GCC_DIAG (7, 8) |
349 | fmtwarn_n (const substring_loc &fmt_loc, location_t param_loc, | |
e9e2bad7 MS |
350 | const char *corrected_substring, opt_code opt, |
351 | unsigned HOST_WIDE_INT n, | |
346114b5 JJ |
352 | const char *singular_gmsgid, const char *plural_gmsgid, ...) |
353 | { | |
c896ecfe DM |
354 | format_string_diagnostic_t diag (fmt_loc, NULL, param_loc, NULL, |
355 | corrected_substring); | |
346114b5 JJ |
356 | va_list ap; |
357 | va_start (ap, plural_gmsgid); | |
c896ecfe DM |
358 | bool warned = diag.emit_warning_n_va (opt, n, singular_gmsgid, plural_gmsgid, |
359 | &ap); | |
346114b5 JJ |
360 | va_end (ap); |
361 | ||
362 | return warned; | |
363 | } | |
88d0c3f0 MS |
364 | |
365 | /* Format length modifiers. */ | |
366 | ||
367 | enum format_lengths | |
368 | { | |
369 | FMT_LEN_none, | |
370 | FMT_LEN_hh, // char argument | |
371 | FMT_LEN_h, // short | |
372 | FMT_LEN_l, // long | |
373 | FMT_LEN_ll, // long long | |
374 | FMT_LEN_L, // long double (and GNU long long) | |
375 | FMT_LEN_z, // size_t | |
376 | FMT_LEN_t, // ptrdiff_t | |
377 | FMT_LEN_j // intmax_t | |
378 | }; | |
379 | ||
380 | ||
88d0c3f0 MS |
381 | /* Description of the result of conversion either of a single directive |
382 | or the whole format string. */ | |
383 | ||
6c1dae73 | 384 | class fmtresult |
88d0c3f0 | 385 | { |
6c1dae73 | 386 | public: |
954b452a MS |
387 | /* Construct a FMTRESULT object with all counters initialized |
388 | to MIN. KNOWNRANGE is set when MIN is valid. */ | |
389 | fmtresult (unsigned HOST_WIDE_INT min = HOST_WIDE_INT_MAX) | |
937a86b4 | 390 | : argmin (), argmax (), dst_offset (HOST_WIDE_INT_MIN), nonstr (), |
954b452a | 391 | knownrange (min < HOST_WIDE_INT_MAX), |
bbcbd744 | 392 | mayfail (), nullp () |
1eb4547b | 393 | { |
954b452a MS |
394 | range.min = min; |
395 | range.max = min; | |
5d93da1d MS |
396 | range.likely = min; |
397 | range.unlikely = min; | |
954b452a MS |
398 | } |
399 | ||
5d93da1d MS |
400 | /* Construct a FMTRESULT object with MIN, MAX, and LIKELY counters. |
401 | KNOWNRANGE is set when both MIN and MAX are valid. */ | |
402 | fmtresult (unsigned HOST_WIDE_INT min, unsigned HOST_WIDE_INT max, | |
403 | unsigned HOST_WIDE_INT likely = HOST_WIDE_INT_MAX) | |
937a86b4 | 404 | : argmin (), argmax (), dst_offset (HOST_WIDE_INT_MIN), nonstr (), |
954b452a | 405 | knownrange (min < HOST_WIDE_INT_MAX && max < HOST_WIDE_INT_MAX), |
bbcbd744 | 406 | mayfail (), nullp () |
954b452a MS |
407 | { |
408 | range.min = min; | |
a151e93b | 409 | range.max = max; |
5d93da1d MS |
410 | range.likely = max < likely ? min : likely; |
411 | range.unlikely = max; | |
1eb4547b MS |
412 | } |
413 | ||
31c87a43 MS |
414 | /* Adjust result upward to reflect the RANGE of values the specified |
415 | width or precision is known to be in. */ | |
416 | fmtresult& adjust_for_width_or_precision (const HOST_WIDE_INT[2], | |
5d93da1d MS |
417 | tree = NULL_TREE, |
418 | unsigned = 0, unsigned = 0); | |
419 | ||
420 | /* Return the maximum number of decimal digits a value of TYPE | |
421 | formats as on output. */ | |
31c87a43 | 422 | static unsigned type_max_digits (tree, int); |
5d93da1d | 423 | |
88d0c3f0 MS |
424 | /* The range a directive's argument is in. */ |
425 | tree argmin, argmax; | |
426 | ||
937a86b4 MS |
427 | /* The starting offset into the destination of the formatted function |
428 | call of the %s argument that points into (aliases with) the same | |
429 | destination array. */ | |
430 | HOST_WIDE_INT dst_offset; | |
431 | ||
88d0c3f0 MS |
432 | /* The minimum and maximum number of bytes that a directive |
433 | results in on output for an argument in the range above. */ | |
434 | result_range range; | |
435 | ||
a41cc59d MS |
436 | /* Non-nul when the argument of a string directive is not a nul |
437 | terminated string. */ | |
438 | tree nonstr; | |
439 | ||
1eb4547b MS |
440 | /* True when the range above is obtained from a known value of |
441 | a directive's argument or its bounds and not the result of | |
442 | heuristics that depend on warning levels. */ | |
443 | bool knownrange; | |
444 | ||
bbcbd744 MS |
445 | /* True for a directive that may fail (such as wide character |
446 | directives). */ | |
447 | bool mayfail; | |
448 | ||
2c8c83ac MS |
449 | /* True when the argument is a null pointer. */ |
450 | bool nullp; | |
88d0c3f0 MS |
451 | }; |
452 | ||
31c87a43 MS |
453 | /* Adjust result upward to reflect the range ADJUST of values the |
454 | specified width or precision is known to be in. When non-null, | |
455 | TYPE denotes the type of the directive whose result is being | |
456 | adjusted, BASE gives the base of the directive (octal, decimal, | |
457 | or hex), and ADJ denotes the additional adjustment to the LIKELY | |
458 | counter that may need to be added when ADJUST is a range. */ | |
5d93da1d MS |
459 | |
460 | fmtresult& | |
31c87a43 | 461 | fmtresult::adjust_for_width_or_precision (const HOST_WIDE_INT adjust[2], |
5d93da1d MS |
462 | tree type /* = NULL_TREE */, |
463 | unsigned base /* = 0 */, | |
464 | unsigned adj /* = 0 */) | |
465 | { | |
466 | bool minadjusted = false; | |
467 | ||
5d93da1d | 468 | /* Adjust the minimum and likely counters. */ |
8657c838 | 469 | if (adjust[0] >= 0) |
5d93da1d MS |
470 | { |
471 | if (range.min < (unsigned HOST_WIDE_INT)adjust[0]) | |
472 | { | |
473 | range.min = adjust[0]; | |
474 | minadjusted = true; | |
475 | } | |
476 | ||
477 | /* Adjust the likely counter. */ | |
478 | if (range.likely < range.min) | |
479 | range.likely = range.min; | |
480 | } | |
481 | else if (adjust[0] == target_int_min () | |
482 | && (unsigned HOST_WIDE_INT)adjust[1] == target_int_max ()) | |
483 | knownrange = false; | |
484 | ||
485 | /* Adjust the maximum counter. */ | |
8657c838 | 486 | if (adjust[1] > 0) |
5d93da1d MS |
487 | { |
488 | if (range.max < (unsigned HOST_WIDE_INT)adjust[1]) | |
489 | { | |
490 | range.max = adjust[1]; | |
491 | ||
492 | /* Set KNOWNRANGE if both the minimum and maximum have been | |
493 | adjusted. Otherwise leave it at what it was before. */ | |
494 | knownrange = minadjusted; | |
495 | } | |
496 | } | |
497 | ||
498 | if (warn_level > 1 && type) | |
499 | { | |
500 | /* For large non-constant width or precision whose range spans | |
501 | the maximum number of digits produced by the directive for | |
502 | any argument, set the likely number of bytes to be at most | |
503 | the number digits plus other adjustment determined by the | |
504 | caller (one for sign or two for the hexadecimal "0x" | |
505 | prefix). */ | |
506 | unsigned dirdigs = type_max_digits (type, base); | |
507 | if (adjust[0] < dirdigs && dirdigs < adjust[1] | |
508 | && range.likely < dirdigs) | |
509 | range.likely = dirdigs + adj; | |
510 | } | |
511 | else if (range.likely < (range.min ? range.min : 1)) | |
512 | { | |
513 | /* Conservatively, set LIKELY to at least MIN but no less than | |
514 | 1 unless MAX is zero. */ | |
515 | range.likely = (range.min | |
516 | ? range.min | |
517 | : range.max && (range.max < HOST_WIDE_INT_MAX | |
518 | || warn_level > 1) ? 1 : 0); | |
519 | } | |
520 | ||
521 | /* Finally adjust the unlikely counter to be at least as large as | |
522 | the maximum. */ | |
523 | if (range.unlikely < range.max) | |
524 | range.unlikely = range.max; | |
525 | ||
526 | return *this; | |
527 | } | |
528 | ||
529 | /* Return the maximum number of digits a value of TYPE formats in | |
530 | BASE on output, not counting base prefix . */ | |
531 | ||
532 | unsigned | |
533 | fmtresult::type_max_digits (tree type, int base) | |
534 | { | |
535 | unsigned prec = TYPE_PRECISION (type); | |
7af7cb51 MS |
536 | switch (base) |
537 | { | |
aa353bd9 FD |
538 | case 2: |
539 | return prec; | |
7af7cb51 MS |
540 | case 8: |
541 | return (prec + 2) / 3; | |
542 | case 10: | |
543 | /* Decimal approximation: yields 3, 5, 10, and 20 for precision | |
544 | of 8, 16, 32, and 64 bits. */ | |
545 | return prec * 301 / 1000 + 1; | |
546 | case 16: | |
547 | return prec / 4; | |
548 | } | |
5d93da1d | 549 | |
7af7cb51 | 550 | gcc_unreachable (); |
5d93da1d MS |
551 | } |
552 | ||
31c87a43 | 553 | static bool |
f5299992 AH |
554 | get_int_range (tree, gimple *, HOST_WIDE_INT *, HOST_WIDE_INT *, |
555 | bool, HOST_WIDE_INT, range_query *); | |
31c87a43 | 556 | |
937a86b4 MS |
557 | struct call_info; |
558 | ||
31c87a43 MS |
559 | /* Description of a format directive. A directive is either a plain |
560 | string or a conversion specification that starts with '%'. */ | |
88d0c3f0 | 561 | |
954b452a | 562 | struct directive |
88d0c3f0 | 563 | { |
937a86b4 MS |
564 | directive (const call_info *inf, unsigned dno) |
565 | : info (inf), dirno (dno), argno (), beg (), len (), flags (), | |
566 | width (), prec (), modifier (), specifier (), arg (), fmtfunc () | |
567 | { } | |
568 | ||
569 | /* Reference to the info structure describing the call that this | |
570 | directive is a part of. */ | |
571 | const call_info *info; | |
572 | ||
954b452a MS |
573 | /* The 1-based directive number (for debugging). */ |
574 | unsigned dirno; | |
575 | ||
937a86b4 MS |
576 | /* The zero-based argument number of the directive's argument ARG in |
577 | the function's argument list. */ | |
578 | unsigned argno; | |
579 | ||
954b452a MS |
580 | /* The first character of the directive and its length. */ |
581 | const char *beg; | |
582 | size_t len; | |
583 | ||
88d0c3f0 MS |
584 | /* A bitmap of flags, one for each character. */ |
585 | unsigned flags[256 / sizeof (int)]; | |
88d0c3f0 | 586 | |
31c87a43 MS |
587 | /* The range of values of the specified width, or -1 if not specified. */ |
588 | HOST_WIDE_INT width[2]; | |
589 | /* The range of values of the specified precision, or -1 if not | |
590 | specified. */ | |
591 | HOST_WIDE_INT prec[2]; | |
88d0c3f0 MS |
592 | |
593 | /* Length modifier. */ | |
594 | format_lengths modifier; | |
595 | ||
596 | /* Format specifier character. */ | |
597 | char specifier; | |
598 | ||
954b452a MS |
599 | /* The argument of the directive or null when the directive doesn't |
600 | take one or when none is available (such as for vararg functions). */ | |
601 | tree arg; | |
88d0c3f0 | 602 | |
5d93da1d MS |
603 | /* Format conversion function that given a directive and an argument |
604 | returns the formatting result. */ | |
3c9f762a | 605 | fmtresult (*fmtfunc) (const directive &, tree, pointer_query &); |
88d0c3f0 | 606 | |
d2ae6d5c | 607 | /* Return True when the format flag CHR has been used. */ |
88d0c3f0 MS |
608 | bool get_flag (char chr) const |
609 | { | |
610 | unsigned char c = chr & 0xff; | |
611 | return (flags[c / (CHAR_BIT * sizeof *flags)] | |
612 | & (1U << (c % (CHAR_BIT * sizeof *flags)))); | |
613 | } | |
614 | ||
615 | /* Make a record of the format flag CHR having been used. */ | |
616 | void set_flag (char chr) | |
617 | { | |
618 | unsigned char c = chr & 0xff; | |
619 | flags[c / (CHAR_BIT * sizeof *flags)] | |
620 | |= (1U << (c % (CHAR_BIT * sizeof *flags))); | |
621 | } | |
622 | ||
623 | /* Reset the format flag CHR. */ | |
624 | void clear_flag (char chr) | |
625 | { | |
626 | unsigned char c = chr & 0xff; | |
627 | flags[c / (CHAR_BIT * sizeof *flags)] | |
628 | &= ~(1U << (c % (CHAR_BIT * sizeof *flags))); | |
629 | } | |
954b452a | 630 | |
31c87a43 | 631 | /* Set both bounds of the width range to VAL. */ |
954b452a MS |
632 | void set_width (HOST_WIDE_INT val) |
633 | { | |
31c87a43 | 634 | width[0] = width[1] = val; |
954b452a MS |
635 | } |
636 | ||
31c87a43 MS |
637 | /* Set the width range according to ARG, with both bounds being |
638 | no less than 0. For a constant ARG set both bounds to its value | |
639 | or 0, whichever is greater. For a non-constant ARG in some range | |
640 | set width to its range adjusting each bound to -1 if it's less. | |
641 | For an indeterminate ARG set width to [0, INT_MAX]. */ | |
f5299992 | 642 | void set_width (tree arg, range_query *); |
954b452a | 643 | |
31c87a43 | 644 | /* Set both bounds of the precision range to VAL. */ |
954b452a MS |
645 | void set_precision (HOST_WIDE_INT val) |
646 | { | |
31c87a43 | 647 | prec[0] = prec[1] = val; |
954b452a MS |
648 | } |
649 | ||
31c87a43 MS |
650 | /* Set the precision range according to ARG, with both bounds being |
651 | no less than -1. For a constant ARG set both bounds to its value | |
652 | or -1 whichever is greater. For a non-constant ARG in some range | |
653 | set precision to its range adjusting each bound to -1 if it's less. | |
654 | For an indeterminate ARG set precision to [-1, INT_MAX]. */ | |
f5299992 | 655 | void set_precision (tree arg, range_query *query); |
71dedb33 MS |
656 | |
657 | /* Return true if both width and precision are known to be | |
658 | either constant or in some range, false otherwise. */ | |
659 | bool known_width_and_precision () const | |
660 | { | |
661 | return ((width[1] < 0 | |
662 | || (unsigned HOST_WIDE_INT)width[1] <= target_int_max ()) | |
663 | && (prec[1] < 0 | |
664 | || (unsigned HOST_WIDE_INT)prec[1] < target_int_max ())); | |
665 | } | |
88d0c3f0 MS |
666 | }; |
667 | ||
937a86b4 MS |
668 | /* The result of a call to a formatted function. */ |
669 | ||
670 | struct format_result | |
671 | { | |
672 | format_result () | |
673 | : range (), aliases (), alias_count (), knownrange (), posunder4k (), | |
674 | floating (), warned () { /* No-op. */ } | |
675 | ||
676 | ~format_result () | |
677 | { | |
678 | XDELETEVEC (aliases); | |
679 | } | |
680 | ||
681 | /* Range of characters written by the formatted function. | |
682 | Setting the minimum to HOST_WIDE_INT_MAX disables all | |
683 | length tracking for the remainder of the format string. */ | |
684 | result_range range; | |
685 | ||
686 | struct alias_info | |
687 | { | |
688 | directive dir; /* The directive that aliases the destination. */ | |
689 | HOST_WIDE_INT offset; /* The offset at which it aliases it. */ | |
690 | result_range range; /* The raw result of the directive. */ | |
691 | }; | |
692 | ||
693 | /* An array of directives whose pointer argument aliases a part | |
694 | of the destination object of the formatted function. */ | |
695 | alias_info *aliases; | |
696 | unsigned alias_count; | |
697 | ||
698 | /* True when the range above is obtained from known values of | |
699 | directive arguments, or bounds on the amount of output such | |
700 | as width and precision, and not the result of heuristics that | |
701 | depend on warning levels. It's used to issue stricter diagnostics | |
702 | in cases where strings of unknown lengths are bounded by the arrays | |
703 | they are determined to refer to. KNOWNRANGE must not be used for | |
704 | the return value optimization. */ | |
705 | bool knownrange; | |
706 | ||
707 | /* True if no individual directive could fail or result in more than | |
708 | 4095 bytes of output (the total NUMBER_CHARS_{MIN,MAX} might be | |
709 | greater). Implementations are not required to handle directives | |
710 | that produce more than 4K bytes (leading to undefined behavior) | |
711 | and so when one is found it disables the return value optimization. | |
712 | Similarly, directives that can fail (such as wide character | |
713 | directives) disable the optimization. */ | |
714 | bool posunder4k; | |
715 | ||
716 | /* True when a floating point directive has been seen in the format | |
717 | string. */ | |
718 | bool floating; | |
719 | ||
720 | /* True when an intermediate result has caused a warning. Used to | |
721 | avoid issuing duplicate warnings while finishing the processing | |
722 | of a call. WARNED also disables the return value optimization. */ | |
723 | bool warned; | |
724 | ||
725 | /* Preincrement the number of output characters by 1. */ | |
726 | format_result& operator++ () | |
727 | { | |
728 | return *this += 1; | |
729 | } | |
730 | ||
731 | /* Postincrement the number of output characters by 1. */ | |
732 | format_result operator++ (int) | |
733 | { | |
734 | format_result prev (*this); | |
735 | *this += 1; | |
736 | return prev; | |
737 | } | |
738 | ||
739 | /* Increment the number of output characters by N. */ | |
740 | format_result& operator+= (unsigned HOST_WIDE_INT); | |
741 | ||
742 | /* Add a directive to the sequence of those with potentially aliasing | |
743 | arguments. */ | |
744 | void append_alias (const directive &, HOST_WIDE_INT, const result_range &); | |
745 | ||
746 | private: | |
747 | /* Not copyable or assignable. */ | |
748 | format_result (format_result&); | |
749 | void operator= (format_result&); | |
750 | }; | |
751 | ||
752 | format_result& | |
753 | format_result::operator+= (unsigned HOST_WIDE_INT n) | |
754 | { | |
755 | gcc_assert (n < HOST_WIDE_INT_MAX); | |
756 | ||
757 | if (range.min < HOST_WIDE_INT_MAX) | |
758 | range.min += n; | |
759 | ||
760 | if (range.max < HOST_WIDE_INT_MAX) | |
761 | range.max += n; | |
762 | ||
763 | if (range.likely < HOST_WIDE_INT_MAX) | |
764 | range.likely += n; | |
765 | ||
766 | if (range.unlikely < HOST_WIDE_INT_MAX) | |
767 | range.unlikely += n; | |
768 | ||
769 | return *this; | |
770 | } | |
771 | ||
772 | void | |
773 | format_result::append_alias (const directive &d, HOST_WIDE_INT off, | |
774 | const result_range &resrng) | |
775 | { | |
776 | unsigned cnt = alias_count + 1; | |
777 | alias_info *ar = XNEWVEC (alias_info, cnt); | |
778 | ||
779 | for (unsigned i = 0; i != alias_count; ++i) | |
780 | ar[i] = aliases[i]; | |
781 | ||
782 | ar[alias_count].dir = d; | |
783 | ar[alias_count].offset = off; | |
784 | ar[alias_count].range = resrng; | |
785 | ||
786 | XDELETEVEC (aliases); | |
787 | ||
788 | alias_count = cnt; | |
789 | aliases = ar; | |
790 | } | |
791 | ||
88d0c3f0 MS |
792 | /* Return the logarithm of X in BASE. */ |
793 | ||
794 | static int | |
795 | ilog (unsigned HOST_WIDE_INT x, int base) | |
796 | { | |
797 | int res = 0; | |
798 | do | |
799 | { | |
800 | ++res; | |
801 | x /= base; | |
802 | } while (x); | |
803 | return res; | |
804 | } | |
805 | ||
806 | /* Return the number of bytes resulting from converting into a string | |
5b8999d6 MS |
807 | the INTEGER_CST tree node X in BASE with a minimum of PREC digits. |
808 | PLUS indicates whether 1 for a plus sign should be added for positive | |
aa353bd9 FD |
809 | numbers, and PREFIX whether the length of an octal ('0') or hexadecimal |
810 | ('0x') or binary ('0b') prefix should be added for nonzero numbers. | |
811 | Return -1 if X cannot be represented. */ | |
5b8999d6 MS |
812 | |
813 | static HOST_WIDE_INT | |
814 | tree_digits (tree x, int base, HOST_WIDE_INT prec, bool plus, bool prefix) | |
88d0c3f0 MS |
815 | { |
816 | unsigned HOST_WIDE_INT absval; | |
817 | ||
5b8999d6 | 818 | HOST_WIDE_INT res; |
88d0c3f0 MS |
819 | |
820 | if (TYPE_UNSIGNED (TREE_TYPE (x))) | |
821 | { | |
822 | if (tree_fits_uhwi_p (x)) | |
823 | { | |
824 | absval = tree_to_uhwi (x); | |
825 | res = plus; | |
826 | } | |
827 | else | |
828 | return -1; | |
829 | } | |
830 | else | |
831 | { | |
832 | if (tree_fits_shwi_p (x)) | |
833 | { | |
834 | HOST_WIDE_INT i = tree_to_shwi (x); | |
209042e6 MS |
835 | if (HOST_WIDE_INT_MIN == i) |
836 | { | |
837 | /* Avoid undefined behavior due to negating a minimum. */ | |
838 | absval = HOST_WIDE_INT_MAX; | |
839 | res = 1; | |
840 | } | |
841 | else if (i < 0) | |
842 | { | |
843 | absval = -i; | |
844 | res = 1; | |
845 | } | |
846 | else | |
847 | { | |
848 | absval = i; | |
849 | res = plus; | |
850 | } | |
88d0c3f0 MS |
851 | } |
852 | else | |
853 | return -1; | |
854 | } | |
855 | ||
5b8999d6 MS |
856 | int ndigs = ilog (absval, base); |
857 | ||
858 | res += prec < ndigs ? ndigs : prec; | |
88d0c3f0 | 859 | |
f589a1ce MS |
860 | /* Adjust a non-zero value for the base prefix, either hexadecimal, |
861 | or, unless precision has resulted in a leading zero, also octal. */ | |
aa353bd9 | 862 | if (prefix && absval) |
88d0c3f0 | 863 | { |
aa353bd9 | 864 | if (base == 8 && prec <= ndigs) |
88d0c3f0 | 865 | res += 1; |
aa353bd9 | 866 | else if (base == 16 || base == 2) /* 0x...(0X...) or 0b...(0B...). */ |
88d0c3f0 MS |
867 | res += 2; |
868 | } | |
869 | ||
870 | return res; | |
871 | } | |
872 | ||
88d0c3f0 MS |
873 | /* Description of a call to a formatted function. */ |
874 | ||
22fca489 | 875 | struct call_info |
88d0c3f0 MS |
876 | { |
877 | /* Function call statement. */ | |
878 | gimple *callstmt; | |
879 | ||
880 | /* Function called. */ | |
881 | tree func; | |
882 | ||
883 | /* Called built-in function code. */ | |
884 | built_in_function fncode; | |
885 | ||
937a86b4 MS |
886 | /* The "origin" of the destination pointer argument, which is either |
887 | the DECL of the destination buffer being written into or a pointer | |
888 | that points to it, plus some offset. */ | |
889 | tree dst_origin; | |
890 | ||
891 | /* For a destination pointing to a struct array member, the offset of | |
892 | the member. */ | |
893 | HOST_WIDE_INT dst_field; | |
894 | ||
895 | /* The offset into the destination buffer. */ | |
896 | HOST_WIDE_INT dst_offset; | |
897 | ||
88d0c3f0 MS |
898 | /* Format argument and format string extracted from it. */ |
899 | tree format; | |
900 | const char *fmtstr; | |
901 | ||
902 | /* The location of the format argument. */ | |
903 | location_t fmtloc; | |
904 | ||
905 | /* The destination object size for __builtin___xxx_chk functions | |
906 | typically determined by __builtin_object_size, or -1 if unknown. */ | |
907 | unsigned HOST_WIDE_INT objsize; | |
908 | ||
909 | /* Number of the first variable argument. */ | |
910 | unsigned HOST_WIDE_INT argidx; | |
911 | ||
912 | /* True for functions like snprintf that specify the size of | |
913 | the destination, false for others like sprintf that don't. */ | |
914 | bool bounded; | |
d7b0fcaa MS |
915 | |
916 | /* True for bounded functions like snprintf that specify a zero-size | |
917 | buffer as a request to compute the size of output without actually | |
51ad5d5b JJ |
918 | writing any. NOWRITE is cleared in response to the %n directive |
919 | which has side-effects similar to writing output. */ | |
d7b0fcaa | 920 | bool nowrite; |
efcc8d38 MS |
921 | |
922 | /* Return true if the called function's return value is used. */ | |
923 | bool retval_used () const | |
924 | { | |
925 | return gimple_get_lhs (callstmt); | |
926 | } | |
927 | ||
928 | /* Return the warning option corresponding to the called function. */ | |
e9e2bad7 | 929 | opt_code warnopt () const |
efcc8d38 | 930 | { |
bce6f760 | 931 | return bounded ? OPT_Wformat_truncation_ : OPT_Wformat_overflow_; |
efcc8d38 | 932 | } |
eb319c50 MS |
933 | |
934 | /* Return true for calls to file formatted functions. */ | |
935 | bool is_file_func () const | |
936 | { | |
937 | return (fncode == BUILT_IN_FPRINTF | |
938 | || fncode == BUILT_IN_FPRINTF_CHK | |
939 | || fncode == BUILT_IN_FPRINTF_UNLOCKED | |
940 | || fncode == BUILT_IN_VFPRINTF | |
941 | || fncode == BUILT_IN_VFPRINTF_CHK); | |
942 | } | |
943 | ||
944 | /* Return true for calls to string formatted functions. */ | |
945 | bool is_string_func () const | |
946 | { | |
947 | return (fncode == BUILT_IN_SPRINTF | |
948 | || fncode == BUILT_IN_SPRINTF_CHK | |
949 | || fncode == BUILT_IN_SNPRINTF | |
950 | || fncode == BUILT_IN_SNPRINTF_CHK | |
951 | || fncode == BUILT_IN_VSPRINTF | |
952 | || fncode == BUILT_IN_VSPRINTF_CHK | |
953 | || fncode == BUILT_IN_VSNPRINTF | |
954 | || fncode == BUILT_IN_VSNPRINTF_CHK); | |
955 | } | |
88d0c3f0 MS |
956 | }; |
957 | ||
f5299992 AH |
958 | void |
959 | directive::set_width (tree arg, range_query *query) | |
960 | { | |
961 | get_int_range (arg, info->callstmt, width, width + 1, true, 0, query); | |
962 | } | |
963 | ||
964 | void | |
965 | directive::set_precision (tree arg, range_query *query) | |
966 | { | |
967 | get_int_range (arg, info->callstmt, prec, prec + 1, false, -1, query); | |
968 | } | |
969 | ||
954b452a MS |
970 | /* Return the result of formatting a no-op directive (such as '%n'). */ |
971 | ||
972 | static fmtresult | |
3c9f762a | 973 | format_none (const directive &, tree, pointer_query &) |
954b452a MS |
974 | { |
975 | fmtresult res (0); | |
954b452a MS |
976 | return res; |
977 | } | |
978 | ||
88d0c3f0 MS |
979 | /* Return the result of formatting the '%%' directive. */ |
980 | ||
981 | static fmtresult | |
3c9f762a | 982 | format_percent (const directive &, tree, pointer_query &) |
88d0c3f0 | 983 | { |
954b452a | 984 | fmtresult res (1); |
88d0c3f0 MS |
985 | return res; |
986 | } | |
987 | ||
988 | ||
42aa1173 | 989 | /* Compute intmax_type_node and uintmax_type_node similarly to how |
e53b6e56 | 990 | tree.cc builds size_type_node. */ |
88d0c3f0 MS |
991 | |
992 | static void | |
993 | build_intmax_type_nodes (tree *pintmax, tree *puintmax) | |
994 | { | |
42aa1173 | 995 | if (strcmp (UINTMAX_TYPE, "unsigned int") == 0) |
88d0c3f0 MS |
996 | { |
997 | *pintmax = integer_type_node; | |
998 | *puintmax = unsigned_type_node; | |
999 | } | |
42aa1173 | 1000 | else if (strcmp (UINTMAX_TYPE, "long unsigned int") == 0) |
88d0c3f0 MS |
1001 | { |
1002 | *pintmax = long_integer_type_node; | |
1003 | *puintmax = long_unsigned_type_node; | |
1004 | } | |
42aa1173 | 1005 | else if (strcmp (UINTMAX_TYPE, "long long unsigned int") == 0) |
88d0c3f0 MS |
1006 | { |
1007 | *pintmax = long_long_integer_type_node; | |
1008 | *puintmax = long_long_unsigned_type_node; | |
1009 | } | |
1010 | else | |
1011 | { | |
1012 | for (int i = 0; i < NUM_INT_N_ENTS; i++) | |
1013 | if (int_n_enabled_p[i]) | |
1014 | { | |
5e580306 | 1015 | char name[50], altname[50]; |
88d0c3f0 | 1016 | sprintf (name, "__int%d unsigned", int_n_data[i].bitsize); |
5e580306 | 1017 | sprintf (altname, "__int%d__ unsigned", int_n_data[i].bitsize); |
88d0c3f0 | 1018 | |
5e580306 JL |
1019 | if (strcmp (name, UINTMAX_TYPE) == 0 |
1020 | || strcmp (altname, UINTMAX_TYPE) == 0) | |
88d0c3f0 MS |
1021 | { |
1022 | *pintmax = int_n_trees[i].signed_type; | |
1023 | *puintmax = int_n_trees[i].unsigned_type; | |
42aa1173 | 1024 | return; |
88d0c3f0 MS |
1025 | } |
1026 | } | |
42aa1173 | 1027 | gcc_unreachable (); |
88d0c3f0 MS |
1028 | } |
1029 | } | |
1030 | ||
3812f460 MS |
1031 | /* Determine the range [*PMIN, *PMAX] that the expression ARG is |
1032 | in and that is representable in type int. | |
1033 | Return true when the range is a subrange of that of int. | |
1034 | When ARG is null it is as if it had the full range of int. | |
31c87a43 MS |
1035 | When ABSOLUTE is true the range reflects the absolute value of |
1036 | the argument. When ABSOLUTE is false, negative bounds of | |
1037 | the determined range are replaced with NEGBOUND. */ | |
1038 | ||
1039 | static bool | |
f5299992 AH |
1040 | get_int_range (tree arg, gimple *stmt, |
1041 | HOST_WIDE_INT *pmin, HOST_WIDE_INT *pmax, | |
bbe2e114 | 1042 | bool absolute, HOST_WIDE_INT negbound, |
f5299992 | 1043 | range_query *query) |
31c87a43 | 1044 | { |
3812f460 MS |
1045 | /* The type of the result. */ |
1046 | const_tree type = integer_type_node; | |
1047 | ||
31c87a43 MS |
1048 | bool knownrange = false; |
1049 | ||
1050 | if (!arg) | |
1051 | { | |
3812f460 MS |
1052 | *pmin = tree_to_shwi (TYPE_MIN_VALUE (type)); |
1053 | *pmax = tree_to_shwi (TYPE_MAX_VALUE (type)); | |
31c87a43 | 1054 | } |
e602bbfc MS |
1055 | else if (TREE_CODE (arg) == INTEGER_CST |
1056 | && TYPE_PRECISION (TREE_TYPE (arg)) <= TYPE_PRECISION (type)) | |
31c87a43 MS |
1057 | { |
1058 | /* For a constant argument return its value adjusted as specified | |
1059 | by NEGATIVE and NEGBOUND and return true to indicate that the | |
1060 | result is known. */ | |
1061 | *pmin = tree_fits_shwi_p (arg) ? tree_to_shwi (arg) : tree_to_uhwi (arg); | |
1062 | *pmax = *pmin; | |
1063 | knownrange = true; | |
1064 | } | |
1065 | else | |
1066 | { | |
1067 | /* True if the argument's range cannot be determined. */ | |
1068 | bool unknown = true; | |
1069 | ||
3812f460 | 1070 | tree argtype = TREE_TYPE (arg); |
31c87a43 | 1071 | |
3812f460 MS |
1072 | /* Ignore invalid arguments with greater precision that that |
1073 | of the expected type (e.g., in sprintf("%*i", 12LL, i)). | |
1074 | They will have been detected and diagnosed by -Wformat and | |
1075 | so it's not important to complicate this code to try to deal | |
1076 | with them again. */ | |
31c87a43 | 1077 | if (TREE_CODE (arg) == SSA_NAME |
3812f460 MS |
1078 | && INTEGRAL_TYPE_P (argtype) |
1079 | && TYPE_PRECISION (argtype) <= TYPE_PRECISION (type)) | |
31c87a43 MS |
1080 | { |
1081 | /* Try to determine the range of values of the integer argument. */ | |
f5299992 AH |
1082 | value_range vr; |
1083 | query->range_of_expr (vr, arg, stmt); | |
22fca489 | 1084 | |
f5299992 | 1085 | if (!vr.undefined_p () && !vr.varying_p ()) |
31c87a43 MS |
1086 | { |
1087 | HOST_WIDE_INT type_min | |
3812f460 MS |
1088 | = (TYPE_UNSIGNED (argtype) |
1089 | ? tree_to_uhwi (TYPE_MIN_VALUE (argtype)) | |
1090 | : tree_to_shwi (TYPE_MIN_VALUE (argtype))); | |
31c87a43 | 1091 | |
3812f460 | 1092 | HOST_WIDE_INT type_max = tree_to_uhwi (TYPE_MAX_VALUE (argtype)); |
31c87a43 | 1093 | |
c79d8335 | 1094 | tree type = TREE_TYPE (arg); |
f5299992 AH |
1095 | tree tmin = wide_int_to_tree (type, vr.lower_bound ()); |
1096 | tree tmax = wide_int_to_tree (type, vr.upper_bound ()); | |
c79d8335 AH |
1097 | *pmin = TREE_INT_CST_LOW (tmin); |
1098 | *pmax = TREE_INT_CST_LOW (tmax); | |
31c87a43 | 1099 | |
3812f460 MS |
1100 | if (*pmin < *pmax) |
1101 | { | |
1102 | /* Return true if the adjusted range is a subrange of | |
1103 | the full range of the argument's type. *PMAX may | |
1104 | be less than *PMIN when the argument is unsigned | |
1105 | and its upper bound is in excess of TYPE_MAX. In | |
1106 | that (invalid) case disregard the range and use that | |
1107 | of the expected type instead. */ | |
1108 | knownrange = type_min < *pmin || *pmax < type_max; | |
1109 | ||
1110 | unknown = false; | |
1111 | } | |
31c87a43 MS |
1112 | } |
1113 | } | |
1114 | ||
1115 | /* Handle an argument with an unknown range as if none had been | |
1116 | provided. */ | |
1117 | if (unknown) | |
f5299992 AH |
1118 | return get_int_range (NULL_TREE, NULL, pmin, pmax, absolute, |
1119 | negbound, query); | |
31c87a43 MS |
1120 | } |
1121 | ||
1122 | /* Adjust each bound as specified by ABSOLUTE and NEGBOUND. */ | |
1123 | if (absolute) | |
1124 | { | |
1125 | if (*pmin < 0) | |
1126 | { | |
1127 | if (*pmin == *pmax) | |
1128 | *pmin = *pmax = -*pmin; | |
1129 | else | |
1130 | { | |
3812f460 MS |
1131 | /* Make sure signed overlow is avoided. */ |
1132 | gcc_assert (*pmin != HOST_WIDE_INT_MIN); | |
1133 | ||
31c87a43 MS |
1134 | HOST_WIDE_INT tmp = -*pmin; |
1135 | *pmin = 0; | |
1136 | if (*pmax < tmp) | |
1137 | *pmax = tmp; | |
1138 | } | |
1139 | } | |
1140 | } | |
1141 | else if (*pmin < negbound) | |
1142 | *pmin = negbound; | |
1143 | ||
1144 | return knownrange; | |
1145 | } | |
1146 | ||
573aa7d4 MS |
1147 | /* With the range [*ARGMIN, *ARGMAX] of an integer directive's actual |
1148 | argument, due to the conversion from either *ARGMIN or *ARGMAX to | |
1149 | the type of the directive's formal argument it's possible for both | |
1150 | to result in the same number of bytes or a range of bytes that's | |
1151 | less than the number of bytes that would result from formatting | |
1152 | some other value in the range [*ARGMIN, *ARGMAX]. This can be | |
1153 | determined by checking for the actual argument being in the range | |
1154 | of the type of the directive. If it isn't it must be assumed to | |
1155 | take on the full range of the directive's type. | |
5b00f9d2 JJ |
1156 | Return true when the range has been adjusted to the full range |
1157 | of DIRTYPE, and false otherwise. */ | |
573aa7d4 MS |
1158 | |
1159 | static bool | |
1160 | adjust_range_for_overflow (tree dirtype, tree *argmin, tree *argmax) | |
1161 | { | |
1162 | tree argtype = TREE_TYPE (*argmin); | |
1163 | unsigned argprec = TYPE_PRECISION (argtype); | |
1164 | unsigned dirprec = TYPE_PRECISION (dirtype); | |
1165 | ||
1166 | /* If the actual argument and the directive's argument have the same | |
1167 | precision and sign there can be no overflow and so there is nothing | |
1168 | to adjust. */ | |
1169 | if (argprec == dirprec && TYPE_SIGN (argtype) == TYPE_SIGN (dirtype)) | |
1170 | return false; | |
1171 | ||
1172 | /* The logic below was inspired/lifted from the CONVERT_EXPR_CODE_P | |
e53b6e56 | 1173 | branch in the extract_range_from_unary_expr function in tree-vrp.cc. */ |
573aa7d4 MS |
1174 | |
1175 | if (TREE_CODE (*argmin) == INTEGER_CST | |
1176 | && TREE_CODE (*argmax) == INTEGER_CST | |
1177 | && (dirprec >= argprec | |
1178 | || integer_zerop (int_const_binop (RSHIFT_EXPR, | |
1179 | int_const_binop (MINUS_EXPR, | |
1180 | *argmax, | |
1181 | *argmin), | |
1182 | size_int (dirprec))))) | |
1183 | { | |
1184 | *argmin = force_fit_type (dirtype, wi::to_widest (*argmin), 0, false); | |
1185 | *argmax = force_fit_type (dirtype, wi::to_widest (*argmax), 0, false); | |
1186 | ||
1187 | /* If *ARGMIN is still less than *ARGMAX the conversion above | |
1188 | is safe. Otherwise, it has overflowed and would be unsafe. */ | |
1189 | if (tree_int_cst_le (*argmin, *argmax)) | |
1190 | return false; | |
1191 | } | |
1192 | ||
5b00f9d2 JJ |
1193 | *argmin = TYPE_MIN_VALUE (dirtype); |
1194 | *argmax = TYPE_MAX_VALUE (dirtype); | |
573aa7d4 MS |
1195 | return true; |
1196 | } | |
1197 | ||
88d0c3f0 | 1198 | /* Return a range representing the minimum and maximum number of bytes |
31c87a43 MS |
1199 | that the format directive DIR will output for any argument given |
1200 | the WIDTH and PRECISION (extracted from DIR). This function is | |
1201 | used when the directive argument or its value isn't known. */ | |
88d0c3f0 MS |
1202 | |
1203 | static fmtresult | |
3c9f762a | 1204 | format_integer (const directive &dir, tree arg, pointer_query &ptr_qry) |
88d0c3f0 | 1205 | { |
42aa1173 JJ |
1206 | tree intmax_type_node; |
1207 | tree uintmax_type_node; | |
88d0c3f0 | 1208 | |
02f1ef40 MS |
1209 | /* Base to format the number in. */ |
1210 | int base; | |
1211 | ||
5d93da1d MS |
1212 | /* True when a conversion is preceded by a prefix indicating the base |
1213 | of the argument (octal or hexadecimal). */ | |
aa353bd9 | 1214 | const bool maybebase = dir.get_flag ('#'); |
5d93da1d | 1215 | |
02f1ef40 MS |
1216 | /* True when a signed conversion is preceded by a sign or space. */ |
1217 | bool maybesign = false; | |
1218 | ||
1219 | /* True for signed conversions (i.e., 'd' and 'i'). */ | |
1220 | bool sign = false; | |
1221 | ||
1222 | switch (dir.specifier) | |
1223 | { | |
1224 | case 'd': | |
1225 | case 'i': | |
1226 | /* Space and '+' are only meaningful for signed conversions. */ | |
1227 | maybesign = dir.get_flag (' ') | dir.get_flag ('+'); | |
1228 | sign = true; | |
1229 | base = 10; | |
1230 | break; | |
1231 | case 'u': | |
1232 | base = 10; | |
1233 | break; | |
aa353bd9 FD |
1234 | case 'b': |
1235 | case 'B': | |
1236 | base = 2; | |
1237 | break; | |
02f1ef40 MS |
1238 | case 'o': |
1239 | base = 8; | |
1240 | break; | |
1241 | case 'X': | |
1242 | case 'x': | |
1243 | base = 16; | |
1244 | break; | |
1245 | default: | |
1246 | gcc_unreachable (); | |
1247 | } | |
88d0c3f0 | 1248 | |
aa353bd9 FD |
1249 | const unsigned adj = (sign | maybebase) + (base == 2 || base == 16); |
1250 | ||
88d0c3f0 MS |
1251 | /* The type of the "formal" argument expected by the directive. */ |
1252 | tree dirtype = NULL_TREE; | |
1253 | ||
1254 | /* Determine the expected type of the argument from the length | |
1255 | modifier. */ | |
954b452a | 1256 | switch (dir.modifier) |
88d0c3f0 MS |
1257 | { |
1258 | case FMT_LEN_none: | |
954b452a | 1259 | if (dir.specifier == 'p') |
88d0c3f0 MS |
1260 | dirtype = ptr_type_node; |
1261 | else | |
1262 | dirtype = sign ? integer_type_node : unsigned_type_node; | |
1263 | break; | |
1264 | ||
1265 | case FMT_LEN_h: | |
1266 | dirtype = sign ? short_integer_type_node : short_unsigned_type_node; | |
1267 | break; | |
1268 | ||
1269 | case FMT_LEN_hh: | |
1270 | dirtype = sign ? signed_char_type_node : unsigned_char_type_node; | |
1271 | break; | |
1272 | ||
1273 | case FMT_LEN_l: | |
1274 | dirtype = sign ? long_integer_type_node : long_unsigned_type_node; | |
1275 | break; | |
1276 | ||
1277 | case FMT_LEN_L: | |
1278 | case FMT_LEN_ll: | |
ed30d2a6 MS |
1279 | dirtype = (sign |
1280 | ? long_long_integer_type_node | |
1281 | : long_long_unsigned_type_node); | |
88d0c3f0 MS |
1282 | break; |
1283 | ||
1284 | case FMT_LEN_z: | |
42aa1173 | 1285 | dirtype = signed_or_unsigned_type_for (!sign, size_type_node); |
88d0c3f0 MS |
1286 | break; |
1287 | ||
1288 | case FMT_LEN_t: | |
42aa1173 | 1289 | dirtype = signed_or_unsigned_type_for (!sign, ptrdiff_type_node); |
88d0c3f0 MS |
1290 | break; |
1291 | ||
1292 | case FMT_LEN_j: | |
42aa1173 | 1293 | build_intmax_type_nodes (&intmax_type_node, &uintmax_type_node); |
88d0c3f0 MS |
1294 | dirtype = sign ? intmax_type_node : uintmax_type_node; |
1295 | break; | |
1296 | ||
1297 | default: | |
42aa1173 | 1298 | return fmtresult (); |
88d0c3f0 MS |
1299 | } |
1300 | ||
1301 | /* The type of the argument to the directive, either deduced from | |
1302 | the actual non-constant argument if one is known, or from | |
1303 | the directive itself when none has been provided because it's | |
1304 | a va_list. */ | |
1305 | tree argtype = NULL_TREE; | |
1306 | ||
1307 | if (!arg) | |
1308 | { | |
1309 | /* When the argument has not been provided, use the type of | |
1310 | the directive's argument as an approximation. This will | |
1311 | result in false positives for directives like %i with | |
1312 | arguments with smaller precision (such as short or char). */ | |
1313 | argtype = dirtype; | |
1314 | } | |
1315 | else if (TREE_CODE (arg) == INTEGER_CST) | |
1316 | { | |
88d0c3f0 MS |
1317 | /* When a constant argument has been provided use its value |
1318 | rather than type to determine the length of the output. */ | |
5d93da1d | 1319 | fmtresult res; |
de6aa933 | 1320 | |
31c87a43 | 1321 | if ((dir.prec[0] <= 0 && dir.prec[1] >= 0) && integer_zerop (arg)) |
de6aa933 | 1322 | { |
573aa7d4 MS |
1323 | /* As a special case, a precision of zero with a zero argument |
1324 | results in zero bytes except in base 8 when the '#' flag is | |
1325 | specified, and for signed conversions in base 8 and 10 when | |
71dedb33 MS |
1326 | either the space or '+' flag has been specified and it results |
1327 | in just one byte (with width having the normal effect). This | |
1328 | must extend to the case of a specified precision with | |
1329 | an unknown value because it can be zero. */ | |
5d93da1d | 1330 | res.range.min = ((base == 8 && dir.get_flag ('#')) || maybesign); |
31c87a43 | 1331 | if (res.range.min == 0 && dir.prec[0] != dir.prec[1]) |
5d93da1d MS |
1332 | { |
1333 | res.range.max = 1; | |
1334 | res.range.likely = 1; | |
1335 | } | |
1336 | else | |
1337 | { | |
1338 | res.range.max = res.range.min; | |
1339 | res.range.likely = res.range.min; | |
1340 | } | |
de6aa933 MS |
1341 | } |
1342 | else | |
1343 | { | |
1344 | /* Convert the argument to the type of the directive. */ | |
1345 | arg = fold_convert (dirtype, arg); | |
88d0c3f0 | 1346 | |
31c87a43 | 1347 | res.range.min = tree_digits (arg, base, dir.prec[0], |
5d93da1d | 1348 | maybesign, maybebase); |
31c87a43 | 1349 | if (dir.prec[0] == dir.prec[1]) |
5d93da1d | 1350 | res.range.max = res.range.min; |
31c87a43 MS |
1351 | else |
1352 | res.range.max = tree_digits (arg, base, dir.prec[1], | |
1353 | maybesign, maybebase); | |
5d93da1d | 1354 | res.range.likely = res.range.min; |
4d82d0bc | 1355 | res.knownrange = true; |
de6aa933 | 1356 | } |
88d0c3f0 | 1357 | |
5d93da1d MS |
1358 | res.range.unlikely = res.range.max; |
1359 | ||
1360 | /* Bump up the counters if WIDTH is greater than LEN. */ | |
aa353bd9 | 1361 | res.adjust_for_width_or_precision (dir.width, dirtype, base, adj); |
5d93da1d | 1362 | /* Bump up the counters again if PRECision is greater still. */ |
aa353bd9 | 1363 | res.adjust_for_width_or_precision (dir.prec, dirtype, base, adj); |
31c87a43 | 1364 | |
88d0c3f0 MS |
1365 | return res; |
1366 | } | |
9c582dbb | 1367 | else if (INTEGRAL_TYPE_P (TREE_TYPE (arg)) |
88d0c3f0 | 1368 | || TREE_CODE (TREE_TYPE (arg)) == POINTER_TYPE) |
0e817197 JJ |
1369 | /* Determine the type of the provided non-constant argument. */ |
1370 | argtype = TREE_TYPE (arg); | |
88d0c3f0 | 1371 | else |
0e817197 JJ |
1372 | /* Don't bother with invalid arguments since they likely would |
1373 | have already been diagnosed, and disable any further checking | |
1374 | of the format string by returning [-1, -1]. */ | |
1375 | return fmtresult (); | |
88d0c3f0 | 1376 | |
1eb4547b | 1377 | fmtresult res; |
88d0c3f0 MS |
1378 | |
1379 | /* Using either the range the non-constant argument is in, or its | |
1380 | type (either "formal" or actual), create a range of values that | |
1381 | constrain the length of output given the warning level. */ | |
1382 | tree argmin = NULL_TREE; | |
1383 | tree argmax = NULL_TREE; | |
1384 | ||
053d5e0c JJ |
1385 | if (arg |
1386 | && TREE_CODE (arg) == SSA_NAME | |
9c582dbb | 1387 | && INTEGRAL_TYPE_P (argtype)) |
88d0c3f0 MS |
1388 | { |
1389 | /* Try to determine the range of values of the integer argument | |
1390 | (range information is not available for pointers). */ | |
f5299992 | 1391 | value_range vr; |
3c9f762a | 1392 | ptr_qry.rvals->range_of_expr (vr, arg, dir.info->callstmt); |
22fca489 | 1393 | |
f5299992 | 1394 | if (!vr.varying_p () && !vr.undefined_p ()) |
88d0c3f0 | 1395 | { |
f5299992 AH |
1396 | argmin = wide_int_to_tree (TREE_TYPE (arg), vr.lower_bound ()); |
1397 | argmax = wide_int_to_tree (TREE_TYPE (arg), vr.upper_bound ()); | |
573aa7d4 MS |
1398 | |
1399 | /* Set KNOWNRANGE if the argument is in a known subrange | |
71dedb33 MS |
1400 | of the directive's type and neither width nor precision |
1401 | is unknown. (KNOWNRANGE may be reset below). */ | |
573aa7d4 | 1402 | res.knownrange |
71dedb33 MS |
1403 | = ((!tree_int_cst_equal (TYPE_MIN_VALUE (dirtype), argmin) |
1404 | || !tree_int_cst_equal (TYPE_MAX_VALUE (dirtype), argmax)) | |
1405 | && dir.known_width_and_precision ()); | |
573aa7d4 MS |
1406 | |
1407 | res.argmin = argmin; | |
1408 | res.argmax = argmax; | |
88d0c3f0 | 1409 | } |
c79d8335 | 1410 | else |
88d0c3f0 MS |
1411 | { |
1412 | /* The argument here may be the result of promoting the actual | |
1413 | argument to int. Try to determine the type of the actual | |
5fe39ec0 | 1414 | argument before promotion and narrow down its range that |
88d0c3f0 MS |
1415 | way. */ |
1416 | gimple *def = SSA_NAME_DEF_STMT (arg); | |
5fe39ec0 | 1417 | if (is_gimple_assign (def)) |
88d0c3f0 MS |
1418 | { |
1419 | tree_code code = gimple_assign_rhs_code (def); | |
1eb4547b MS |
1420 | if (code == INTEGER_CST) |
1421 | { | |
1422 | arg = gimple_assign_rhs1 (def); | |
3c9f762a | 1423 | return format_integer (dir, arg, ptr_qry); |
1eb4547b MS |
1424 | } |
1425 | ||
88d0c3f0 | 1426 | if (code == NOP_EXPR) |
0e817197 JJ |
1427 | { |
1428 | tree type = TREE_TYPE (gimple_assign_rhs1 (def)); | |
9c582dbb | 1429 | if (INTEGRAL_TYPE_P (type) |
0e817197 JJ |
1430 | || TREE_CODE (type) == POINTER_TYPE) |
1431 | argtype = type; | |
1432 | } | |
88d0c3f0 MS |
1433 | } |
1434 | } | |
1435 | } | |
1436 | ||
1437 | if (!argmin) | |
1438 | { | |
5b00f9d2 | 1439 | if (TREE_CODE (argtype) == POINTER_TYPE) |
88d0c3f0 | 1440 | { |
5b00f9d2 JJ |
1441 | argmin = build_int_cst (pointer_sized_int_node, 0); |
1442 | argmax = build_all_ones_cst (pointer_sized_int_node); | |
88d0c3f0 MS |
1443 | } |
1444 | else | |
1445 | { | |
5b00f9d2 JJ |
1446 | argmin = TYPE_MIN_VALUE (argtype); |
1447 | argmax = TYPE_MAX_VALUE (argtype); | |
88d0c3f0 | 1448 | } |
573aa7d4 MS |
1449 | } |
1450 | ||
1451 | /* Clear KNOWNRANGE if the range has been adjusted to the maximum | |
1452 | of the directive. If it has been cleared then since ARGMIN and/or | |
1453 | ARGMAX have been adjusted also adjust the corresponding ARGMIN and | |
1454 | ARGMAX in the result to include in diagnostics. */ | |
1455 | if (adjust_range_for_overflow (dirtype, &argmin, &argmax)) | |
1456 | { | |
1457 | res.knownrange = false; | |
1458 | res.argmin = argmin; | |
1459 | res.argmax = argmax; | |
1460 | } | |
1461 | ||
5b00f9d2 JJ |
1462 | /* Recursively compute the minimum and maximum from the known range. */ |
1463 | if (TYPE_UNSIGNED (dirtype) || tree_int_cst_sgn (argmin) >= 0) | |
573aa7d4 | 1464 | { |
5b00f9d2 JJ |
1465 | /* For unsigned conversions/directives or signed when |
1466 | the minimum is positive, use the minimum and maximum to compute | |
1467 | the shortest and longest output, respectively. */ | |
3c9f762a MS |
1468 | res.range.min = format_integer (dir, argmin, ptr_qry).range.min; |
1469 | res.range.max = format_integer (dir, argmax, ptr_qry).range.max; | |
573aa7d4 | 1470 | } |
5b00f9d2 | 1471 | else if (tree_int_cst_sgn (argmax) < 0) |
573aa7d4 | 1472 | { |
5b00f9d2 JJ |
1473 | /* For signed conversions/directives if maximum is negative, |
1474 | use the minimum as the longest output and maximum as the | |
1475 | shortest output. */ | |
3c9f762a MS |
1476 | res.range.min = format_integer (dir, argmax, ptr_qry).range.min; |
1477 | res.range.max = format_integer (dir, argmin, ptr_qry).range.max; | |
573aa7d4 | 1478 | } |
5b00f9d2 | 1479 | else |
88d0c3f0 | 1480 | { |
5b00f9d2 JJ |
1481 | /* Otherwise, 0 is inside of the range and minimum negative. Use 0 |
1482 | as the shortest output and for the longest output compute the | |
1483 | length of the output of both minimum and maximum and pick the | |
1484 | longer. */ | |
bbe2e114 | 1485 | unsigned HOST_WIDE_INT max1 |
3c9f762a | 1486 | = format_integer (dir, argmin, ptr_qry).range.max; |
bbe2e114 | 1487 | unsigned HOST_WIDE_INT max2 |
3c9f762a | 1488 | = format_integer (dir, argmax, ptr_qry).range.max; |
bbe2e114 | 1489 | res.range.min |
3c9f762a | 1490 | = format_integer (dir, integer_zero_node, ptr_qry).range.min; |
5b00f9d2 | 1491 | res.range.max = MAX (max1, max2); |
88d0c3f0 MS |
1492 | } |
1493 | ||
34a64e47 | 1494 | /* If the range is known, use the maximum as the likely length. */ |
f589a1ce MS |
1495 | if (res.knownrange) |
1496 | res.range.likely = res.range.max; | |
1497 | else | |
1498 | { | |
34a64e47 JJ |
1499 | /* Otherwise, use the minimum. Except for the case where for %#x or |
1500 | %#o the minimum is just for a single value in the range (0) and | |
1501 | for all other values it is something longer, like 0x1 or 01. | |
1502 | Use the length for value 1 in that case instead as the likely | |
1503 | length. */ | |
f589a1ce | 1504 | res.range.likely = res.range.min; |
34a64e47 JJ |
1505 | if (maybebase |
1506 | && base != 10 | |
1507 | && (tree_int_cst_sgn (argmin) < 0 || tree_int_cst_sgn (argmax) > 0)) | |
f589a1ce MS |
1508 | { |
1509 | if (res.range.min == 1) | |
1510 | res.range.likely += base == 8 ? 1 : 2; | |
1511 | else if (res.range.min == 2 | |
aa353bd9 | 1512 | && (base == 16 || base == 2) |
f589a1ce MS |
1513 | && (dir.width[0] == 2 || dir.prec[0] == 2)) |
1514 | ++res.range.likely; | |
1515 | } | |
1516 | } | |
1517 | ||
5d93da1d | 1518 | res.range.unlikely = res.range.max; |
aa353bd9 FD |
1519 | res.adjust_for_width_or_precision (dir.width, dirtype, base, adj); |
1520 | res.adjust_for_width_or_precision (dir.prec, dirtype, base, adj); | |
5d93da1d | 1521 | |
88d0c3f0 MS |
1522 | return res; |
1523 | } | |
1524 | ||
cfce1a4a MS |
1525 | /* Return the number of bytes that a format directive consisting of FLAGS, |
1526 | PRECision, format SPECification, and MPFR rounding specifier RNDSPEC, | |
1527 | would result for argument X under ideal conditions (i.e., if PREC | |
1528 | weren't excessive). MPFR 3.1 allocates large amounts of memory for | |
1529 | values of PREC with large magnitude and can fail (see MPFR bug #21056). | |
1530 | This function works around those problems. */ | |
1531 | ||
1532 | static unsigned HOST_WIDE_INT | |
1533 | get_mpfr_format_length (mpfr_ptr x, const char *flags, HOST_WIDE_INT prec, | |
1534 | char spec, char rndspec) | |
1535 | { | |
1536 | char fmtstr[40]; | |
1537 | ||
1538 | HOST_WIDE_INT len = strlen (flags); | |
1539 | ||
1540 | fmtstr[0] = '%'; | |
1541 | memcpy (fmtstr + 1, flags, len); | |
1542 | memcpy (fmtstr + 1 + len, ".*R", 3); | |
1543 | fmtstr[len + 4] = rndspec; | |
1544 | fmtstr[len + 5] = spec; | |
1545 | fmtstr[len + 6] = '\0'; | |
1546 | ||
5b4f088d MS |
1547 | spec = TOUPPER (spec); |
1548 | if (spec == 'E' || spec == 'F') | |
1549 | { | |
1550 | /* For %e, specify the precision explicitly since mpfr_sprintf | |
1551 | does its own thing just to be different (see MPFR bug 21088). */ | |
1552 | if (prec < 0) | |
1553 | prec = 6; | |
1554 | } | |
1555 | else | |
1556 | { | |
1557 | /* Avoid passing negative precisions with larger magnitude to MPFR | |
1558 | to avoid exposing its bugs. (A negative precision is supposed | |
1559 | to be ignored.) */ | |
1560 | if (prec < 0) | |
1561 | prec = -1; | |
1562 | } | |
cfce1a4a MS |
1563 | |
1564 | HOST_WIDE_INT p = prec; | |
1565 | ||
71dedb33 | 1566 | if (spec == 'G' && !strchr (flags, '#')) |
cfce1a4a | 1567 | { |
71dedb33 MS |
1568 | /* For G/g without the pound flag, precision gives the maximum number |
1569 | of significant digits which is bounded by LDBL_MAX_10_EXP, or, for | |
1570 | a 128 bit IEEE extended precision, 4932. Using twice as much here | |
1571 | should be more than sufficient for any real format. */ | |
cfce1a4a MS |
1572 | if ((IEEE_MAX_10_EXP * 2) < prec) |
1573 | prec = IEEE_MAX_10_EXP * 2; | |
1574 | p = prec; | |
1575 | } | |
1576 | else | |
1577 | { | |
1578 | /* Cap precision arbitrarily at 1KB and add the difference | |
1579 | (if any) to the MPFR result. */ | |
8657c838 | 1580 | if (prec > 1024) |
cfce1a4a MS |
1581 | p = 1024; |
1582 | } | |
1583 | ||
1584 | len = mpfr_snprintf (NULL, 0, fmtstr, (int)p, x); | |
1585 | ||
1586 | /* Handle the unlikely (impossible?) error by returning more than | |
1587 | the maximum dictated by the function's return type. */ | |
1588 | if (len < 0) | |
1589 | return target_dir_max () + 1; | |
1590 | ||
1591 | /* Adjust the return value by the difference. */ | |
1592 | if (p < prec) | |
1593 | len += prec - p; | |
1594 | ||
1595 | return len; | |
1596 | } | |
1597 | ||
88d0c3f0 | 1598 | /* Return the number of bytes to format using the format specifier |
5b4f088d MS |
1599 | SPEC and the precision PREC the largest value in the real floating |
1600 | TYPE. */ | |
88d0c3f0 | 1601 | |
cfce1a4a MS |
1602 | static unsigned HOST_WIDE_INT |
1603 | format_floating_max (tree type, char spec, HOST_WIDE_INT prec) | |
88d0c3f0 MS |
1604 | { |
1605 | machine_mode mode = TYPE_MODE (type); | |
1606 | ||
1607 | /* IBM Extended mode. */ | |
1608 | if (MODE_COMPOSITE_P (mode)) | |
1609 | mode = DFmode; | |
1610 | ||
d2ae6d5c | 1611 | /* Get the real type format description for the target. */ |
88d0c3f0 MS |
1612 | const real_format *rfmt = REAL_MODE_FORMAT (mode); |
1613 | REAL_VALUE_TYPE rv; | |
1614 | ||
954b452a | 1615 | real_maxval (&rv, 0, mode); |
88d0c3f0 MS |
1616 | |
1617 | /* Convert the GCC real value representation with the precision | |
1618 | of the real type to the mpfr_t format with the GCC default | |
1619 | round-to-nearest mode. */ | |
1620 | mpfr_t x; | |
1621 | mpfr_init2 (x, rfmt->p); | |
90ca6847 | 1622 | mpfr_from_real (x, &rv, MPFR_RNDN); |
88d0c3f0 | 1623 | |
1eb4547b | 1624 | /* Return a value one greater to account for the leading minus sign. */ |
0a95c7e2 ML |
1625 | unsigned HOST_WIDE_INT r |
1626 | = 1 + get_mpfr_format_length (x, "", prec, spec, 'D'); | |
1627 | mpfr_clear (x); | |
1628 | return r; | |
88d0c3f0 MS |
1629 | } |
1630 | ||
1631 | /* Return a range representing the minimum and maximum number of bytes | |
a983542b MS |
1632 | that the directive DIR will output for any argument. PREC gives |
1633 | the adjusted precision range to account for negative precisions | |
1634 | meaning the default 6. This function is used when the directive | |
1635 | argument or its value isn't known. */ | |
88d0c3f0 MS |
1636 | |
1637 | static fmtresult | |
a983542b | 1638 | format_floating (const directive &dir, const HOST_WIDE_INT prec[2]) |
88d0c3f0 MS |
1639 | { |
1640 | tree type; | |
88d0c3f0 | 1641 | |
954b452a | 1642 | switch (dir.modifier) |
88d0c3f0 | 1643 | { |
30dc6ee9 | 1644 | case FMT_LEN_l: |
88d0c3f0 MS |
1645 | case FMT_LEN_none: |
1646 | type = double_type_node; | |
1647 | break; | |
1648 | ||
1649 | case FMT_LEN_L: | |
1650 | type = long_double_type_node; | |
88d0c3f0 MS |
1651 | break; |
1652 | ||
1653 | case FMT_LEN_ll: | |
1654 | type = long_double_type_node; | |
88d0c3f0 MS |
1655 | break; |
1656 | ||
1657 | default: | |
1eb4547b | 1658 | return fmtresult (); |
88d0c3f0 MS |
1659 | } |
1660 | ||
1661 | /* The minimum and maximum number of bytes produced by the directive. */ | |
1eb4547b | 1662 | fmtresult res; |
88d0c3f0 | 1663 | |
5d93da1d MS |
1664 | /* The minimum output as determined by flags. It's always at least 1. |
1665 | When plus or space are set the output is preceded by either a sign | |
1666 | or a space. */ | |
a983542b MS |
1667 | unsigned flagmin = (1 /* for the first digit */ |
1668 | + (dir.get_flag ('+') | dir.get_flag (' '))); | |
88d0c3f0 | 1669 | |
7af7cb51 MS |
1670 | /* The minimum is 3 for "inf" and "nan" for all specifiers, plus 1 |
1671 | for the plus sign/space with the '+' and ' ' flags, respectively, | |
1672 | unless reduced below. */ | |
1673 | res.range.min = 2 + flagmin; | |
1674 | ||
5d93da1d MS |
1675 | /* When the pound flag is set the decimal point is included in output |
1676 | regardless of precision. Whether or not a decimal point is included | |
1677 | otherwise depends on the specification and precision. */ | |
1678 | bool radix = dir.get_flag ('#'); | |
88d0c3f0 | 1679 | |
954b452a | 1680 | switch (dir.specifier) |
88d0c3f0 MS |
1681 | { |
1682 | case 'A': | |
1683 | case 'a': | |
1684 | { | |
5d93da1d | 1685 | HOST_WIDE_INT minprec = 6 + !radix /* decimal point */; |
31c87a43 | 1686 | if (dir.prec[0] <= 0) |
5d93da1d | 1687 | minprec = 0; |
31c87a43 MS |
1688 | else if (dir.prec[0] > 0) |
1689 | minprec = dir.prec[0] + !radix /* decimal point */; | |
5d93da1d | 1690 | |
7af7cb51 MS |
1691 | res.range.likely = (2 /* 0x */ |
1692 | + flagmin | |
1693 | + radix | |
1694 | + minprec | |
1695 | + 3 /* p+0 */); | |
5d93da1d | 1696 | |
a983542b | 1697 | res.range.max = format_floating_max (type, 'a', prec[1]); |
5d93da1d MS |
1698 | |
1699 | /* The unlikely maximum accounts for the longest multibyte | |
1700 | decimal point character. */ | |
31c87a43 | 1701 | res.range.unlikely = res.range.max; |
a983542b | 1702 | if (dir.prec[1] > 0) |
31c87a43 | 1703 | res.range.unlikely += target_mb_len_max () - 1; |
5b4f088d | 1704 | |
88d0c3f0 MS |
1705 | break; |
1706 | } | |
1707 | ||
1708 | case 'E': | |
1709 | case 'e': | |
1710 | { | |
a983542b MS |
1711 | /* Minimum output attributable to precision and, when it's |
1712 | non-zero, decimal point. */ | |
1713 | HOST_WIDE_INT minprec = prec[0] ? prec[0] + !radix : 0; | |
1714 | ||
7af7cb51 | 1715 | /* The likely minimum output is "[-+]1.234567e+00" regardless |
88d0c3f0 | 1716 | of the value of the actual argument. */ |
7af7cb51 MS |
1717 | res.range.likely = (flagmin |
1718 | + radix | |
1719 | + minprec | |
1720 | + 2 /* e+ */ + 2); | |
5d93da1d | 1721 | |
a983542b | 1722 | res.range.max = format_floating_max (type, 'e', prec[1]); |
5d93da1d MS |
1723 | |
1724 | /* The unlikely maximum accounts for the longest multibyte | |
1725 | decimal point character. */ | |
31c87a43 MS |
1726 | if (dir.prec[0] != dir.prec[1] |
1727 | || dir.prec[0] == -1 || dir.prec[0] > 0) | |
5d93da1d MS |
1728 | res.range.unlikely = res.range.max + target_mb_len_max () -1; |
1729 | else | |
1730 | res.range.unlikely = res.range.max; | |
88d0c3f0 MS |
1731 | break; |
1732 | } | |
1733 | ||
1734 | case 'F': | |
1735 | case 'f': | |
1736 | { | |
a983542b MS |
1737 | /* Minimum output attributable to precision and, when it's non-zero, |
1738 | decimal point. */ | |
1739 | HOST_WIDE_INT minprec = prec[0] ? prec[0] + !radix : 0; | |
1740 | ||
7af7cb51 MS |
1741 | /* For finite numbers (i.e., not infinity or NaN) the lower bound |
1742 | when precision isn't specified is 8 bytes ("1.23456" since | |
1743 | precision is taken to be 6). When precision is zero, the lower | |
1744 | bound is 1 byte (e.g., "1"). Otherwise, when precision is greater | |
1745 | than zero, then the lower bound is 2 plus precision (plus flags). | |
1746 | But in all cases, the lower bound is no greater than 3. */ | |
1747 | unsigned HOST_WIDE_INT min = flagmin + radix + minprec; | |
1748 | if (min < res.range.min) | |
1749 | res.range.min = min; | |
5d93da1d MS |
1750 | |
1751 | /* Compute the upper bound for -TYPE_MAX. */ | |
a983542b | 1752 | res.range.max = format_floating_max (type, 'f', prec[1]); |
5d93da1d | 1753 | |
71dedb33 MS |
1754 | /* The minimum output with unknown precision is a single byte |
1755 | (e.g., "0") but the more likely output is 3 bytes ("0.0"). */ | |
1756 | if (dir.prec[0] < 0 && dir.prec[1] > 0) | |
1757 | res.range.likely = 3; | |
1758 | else | |
7af7cb51 | 1759 | res.range.likely = min; |
5d93da1d MS |
1760 | |
1761 | /* The unlikely maximum accounts for the longest multibyte | |
1762 | decimal point character. */ | |
31c87a43 MS |
1763 | if (dir.prec[0] != dir.prec[1] |
1764 | || dir.prec[0] == -1 || dir.prec[0] > 0) | |
5d93da1d | 1765 | res.range.unlikely = res.range.max + target_mb_len_max () - 1; |
88d0c3f0 MS |
1766 | break; |
1767 | } | |
5b4f088d | 1768 | |
88d0c3f0 MS |
1769 | case 'G': |
1770 | case 'g': | |
1771 | { | |
5b4f088d MS |
1772 | /* The %g output depends on precision and the exponent of |
1773 | the argument. Since the value of the argument isn't known | |
1774 | the lower bound on the range of bytes (not counting flags | |
71dedb33 MS |
1775 | or width) is 1 plus radix (i.e., either "0" or "0." for |
1776 | "%g" and "%#g", respectively, with a zero argument). */ | |
7af7cb51 MS |
1777 | unsigned HOST_WIDE_INT min = flagmin + radix; |
1778 | if (min < res.range.min) | |
1779 | res.range.min = min; | |
71dedb33 MS |
1780 | |
1781 | char spec = 'g'; | |
1782 | HOST_WIDE_INT maxprec = dir.prec[1]; | |
1783 | if (radix && maxprec) | |
1784 | { | |
1785 | /* When the pound flag (radix) is set, trailing zeros aren't | |
1786 | trimmed and so the longest output is the same as for %e, | |
1787 | except with precision minus 1 (as specified in C11). */ | |
1788 | spec = 'e'; | |
1789 | if (maxprec > 0) | |
1790 | --maxprec; | |
1791 | else if (maxprec < 0) | |
1792 | maxprec = 5; | |
1793 | } | |
a983542b MS |
1794 | else |
1795 | maxprec = prec[1]; | |
71dedb33 MS |
1796 | |
1797 | res.range.max = format_floating_max (type, spec, maxprec); | |
1798 | ||
1799 | /* The likely output is either the maximum computed above | |
1800 | minus 1 (assuming the maximum is positive) when precision | |
1801 | is known (or unspecified), or the same minimum as for %e | |
1802 | (which is computed for a non-negative argument). Unlike | |
1803 | for the other specifiers above the likely output isn't | |
1804 | the minimum because for %g that's 1 which is unlikely. */ | |
1805 | if (dir.prec[1] < 0 | |
1806 | || (unsigned HOST_WIDE_INT)dir.prec[1] < target_int_max ()) | |
1807 | res.range.likely = res.range.max - 1; | |
1808 | else | |
1809 | { | |
1810 | HOST_WIDE_INT minprec = 6 + !radix /* decimal point */; | |
1811 | res.range.likely = (flagmin | |
1812 | + radix | |
1813 | + minprec | |
1814 | + 2 /* e+ */ + 2); | |
1815 | } | |
5d93da1d MS |
1816 | |
1817 | /* The unlikely maximum accounts for the longest multibyte | |
1818 | decimal point character. */ | |
1819 | res.range.unlikely = res.range.max + target_mb_len_max () - 1; | |
88d0c3f0 MS |
1820 | break; |
1821 | } | |
1822 | ||
1823 | default: | |
1eb4547b | 1824 | return fmtresult (); |
88d0c3f0 MS |
1825 | } |
1826 | ||
5d93da1d MS |
1827 | /* Bump up the byte counters if WIDTH is greater. */ |
1828 | res.adjust_for_width_or_precision (dir.width); | |
88d0c3f0 MS |
1829 | return res; |
1830 | } | |
1831 | ||
1832 | /* Return a range representing the minimum and maximum number of bytes | |
31c87a43 MS |
1833 | that the directive DIR will write on output for the floating argument |
1834 | ARG. */ | |
88d0c3f0 MS |
1835 | |
1836 | static fmtresult | |
3c9f762a | 1837 | format_floating (const directive &dir, tree arg, pointer_query &) |
88d0c3f0 | 1838 | { |
31c87a43 | 1839 | HOST_WIDE_INT prec[] = { dir.prec[0], dir.prec[1] }; |
aa215959 JJ |
1840 | tree type = (dir.modifier == FMT_LEN_L || dir.modifier == FMT_LEN_ll |
1841 | ? long_double_type_node : double_type_node); | |
88d0c3f0 | 1842 | |
71dedb33 MS |
1843 | /* For an indeterminate precision the lower bound must be assumed |
1844 | to be zero. */ | |
5d93da1d | 1845 | if (TOUPPER (dir.specifier) == 'A') |
88d0c3f0 | 1846 | { |
71dedb33 MS |
1847 | /* Get the number of fractional decimal digits needed to represent |
1848 | the argument without a loss of accuracy. */ | |
71dedb33 MS |
1849 | unsigned fmtprec |
1850 | = REAL_MODE_FORMAT (TYPE_MODE (type))->p; | |
1851 | ||
1852 | /* The precision of the IEEE 754 double format is 53. | |
1853 | The precision of all other GCC binary double formats | |
1854 | is 56 or less. */ | |
1855 | unsigned maxprec = fmtprec <= 56 ? 13 : 15; | |
1856 | ||
5d93da1d MS |
1857 | /* For %a, leave the minimum precision unspecified to let |
1858 | MFPR trim trailing zeros (as it and many other systems | |
1859 | including Glibc happen to do) and set the maximum | |
1860 | precision to reflect what it would be with trailing zeros | |
1861 | present (as Solaris and derived systems do). */ | |
71dedb33 | 1862 | if (dir.prec[1] < 0) |
5d93da1d | 1863 | { |
71dedb33 MS |
1864 | /* Both bounds are negative implies that precision has |
1865 | not been specified. */ | |
1866 | prec[0] = maxprec; | |
1867 | prec[1] = -1; | |
1868 | } | |
1869 | else if (dir.prec[0] < 0) | |
1870 | { | |
1871 | /* With a negative lower bound and a non-negative upper | |
1872 | bound set the minimum precision to zero and the maximum | |
1873 | to the greater of the maximum precision (i.e., with | |
1874 | trailing zeros present) and the specified upper bound. */ | |
1875 | prec[0] = 0; | |
1876 | prec[1] = dir.prec[1] < maxprec ? maxprec : dir.prec[1]; | |
1877 | } | |
1878 | } | |
1879 | else if (dir.prec[0] < 0) | |
1880 | { | |
1881 | if (dir.prec[1] < 0) | |
1882 | { | |
1883 | /* A precision in a strictly negative range is ignored and | |
1884 | the default of 6 is used instead. */ | |
1885 | prec[0] = prec[1] = 6; | |
1886 | } | |
1887 | else | |
1888 | { | |
1889 | /* For a precision in a partly negative range, the lower bound | |
1890 | must be assumed to be zero and the new upper bound is the | |
1891 | greater of 6 (the default precision used when the specified | |
1892 | precision is negative) and the upper bound of the specified | |
1893 | range. */ | |
1894 | prec[0] = 0; | |
1895 | prec[1] = dir.prec[1] < 6 ? 6 : dir.prec[1]; | |
5d93da1d | 1896 | } |
88d0c3f0 MS |
1897 | } |
1898 | ||
aa215959 JJ |
1899 | if (!arg |
1900 | || TREE_CODE (arg) != REAL_CST | |
1901 | || !useless_type_conversion_p (type, TREE_TYPE (arg))) | |
a983542b MS |
1902 | return format_floating (dir, prec); |
1903 | ||
954b452a MS |
1904 | /* The minimum and maximum number of bytes produced by the directive. */ |
1905 | fmtresult res; | |
88d0c3f0 | 1906 | |
d2ae6d5c | 1907 | /* Get the real type format description for the target. */ |
954b452a MS |
1908 | const REAL_VALUE_TYPE *rvp = TREE_REAL_CST_PTR (arg); |
1909 | const real_format *rfmt = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg))); | |
1910 | ||
7af7cb51 MS |
1911 | if (!real_isfinite (rvp)) |
1912 | { | |
1913 | /* The format for Infinity and NaN is "[-]inf"/"[-]infinity" | |
1914 | and "[-]nan" with the choice being implementation-defined | |
1915 | but not locale dependent. */ | |
1916 | bool sign = dir.get_flag ('+') || real_isneg (rvp); | |
1917 | res.range.min = 3 + sign; | |
1918 | ||
1919 | res.range.likely = res.range.min; | |
1920 | res.range.max = res.range.min; | |
bd1f3566 MS |
1921 | /* The unlikely maximum is "[-/+]infinity" or "[-/+][qs]nan". |
1922 | For NaN, the C/POSIX standards specify two formats: | |
1923 | "[-/+]nan" | |
1924 | and | |
1925 | "[-/+]nan(n-char-sequence)" | |
1926 | No known printf implementation outputs the latter format but AIX | |
1927 | outputs QNaN and SNaN for quiet and signalling NaN, respectively, | |
1928 | so the unlikely maximum reflects that. */ | |
1929 | res.range.unlikely = sign + (real_isinf (rvp) ? 8 : 4); | |
7af7cb51 MS |
1930 | |
1931 | /* The range for infinity and NaN is known unless either width | |
1932 | or precision is unknown. Width has the same effect regardless | |
1933 | of whether the argument is finite. Precision is either ignored | |
1934 | (e.g., Glibc) or can have an effect on the short vs long format | |
1935 | such as inf/infinity (e.g., Solaris). */ | |
1936 | res.knownrange = dir.known_width_and_precision (); | |
1937 | ||
1938 | /* Adjust the range for width but ignore precision. */ | |
1939 | res.adjust_for_width_or_precision (dir.width); | |
1940 | ||
1941 | return res; | |
1942 | } | |
1943 | ||
954b452a MS |
1944 | char fmtstr [40]; |
1945 | char *pfmt = fmtstr; | |
88d0c3f0 | 1946 | |
954b452a MS |
1947 | /* Append flags. */ |
1948 | for (const char *pf = "-+ #0"; *pf; ++pf) | |
1949 | if (dir.get_flag (*pf)) | |
1950 | *pfmt++ = *pf; | |
88d0c3f0 | 1951 | |
954b452a | 1952 | *pfmt = '\0'; |
88d0c3f0 | 1953 | |
954b452a MS |
1954 | { |
1955 | /* Set up an array to easily iterate over. */ | |
1956 | unsigned HOST_WIDE_INT* const minmax[] = { | |
1957 | &res.range.min, &res.range.max | |
1958 | }; | |
88d0c3f0 | 1959 | |
ca32b29e | 1960 | for (int i = 0; i != ARRAY_SIZE (minmax); ++i) |
5b4f088d | 1961 | { |
954b452a MS |
1962 | /* Convert the GCC real value representation with the precision |
1963 | of the real type to the mpfr_t format rounding down in the | |
d2ae6d5c JW |
1964 | first iteration that computes the minimum and up in the second |
1965 | that computes the maximum. This order is arbitrary because | |
954b452a MS |
1966 | rounding in either direction can result in longer output. */ |
1967 | mpfr_t mpfrval; | |
1968 | mpfr_init2 (mpfrval, rfmt->p); | |
90ca6847 | 1969 | mpfr_from_real (mpfrval, rvp, i ? MPFR_RNDU : MPFR_RNDD); |
954b452a MS |
1970 | |
1971 | /* Use the MPFR rounding specifier to round down in the first | |
1972 | iteration and then up. In most but not all cases this will | |
1973 | result in the same number of bytes. */ | |
1974 | char rndspec = "DU"[i]; | |
1975 | ||
1976 | /* Format it and store the result in the corresponding member | |
1977 | of the result struct. */ | |
5d93da1d MS |
1978 | *minmax[i] = get_mpfr_format_length (mpfrval, fmtstr, prec[i], |
1979 | dir.specifier, rndspec); | |
0a95c7e2 | 1980 | mpfr_clear (mpfrval); |
5b4f088d | 1981 | } |
954b452a | 1982 | } |
5b4f088d | 1983 | |
954b452a MS |
1984 | /* Make sure the minimum is less than the maximum (MPFR rounding |
1985 | in the call to mpfr_snprintf can result in the reverse. */ | |
1986 | if (res.range.max < res.range.min) | |
1987 | { | |
1988 | unsigned HOST_WIDE_INT tmp = res.range.min; | |
1989 | res.range.min = res.range.max; | |
1990 | res.range.max = tmp; | |
1991 | } | |
1eb4547b | 1992 | |
71dedb33 MS |
1993 | /* The range is known unless either width or precision is unknown. */ |
1994 | res.knownrange = dir.known_width_and_precision (); | |
1995 | ||
1996 | /* For the same floating point constant, unless width or precision | |
1997 | is unknown, use the longer output as the likely maximum since | |
d2ae6d5c | 1998 | with round to nearest either is equally likely. Otherwise, when |
71dedb33 MS |
1999 | precision is unknown, use the greater of the minimum and 3 as |
2000 | the likely output (for "0.0" since zero precision is unlikely). */ | |
2001 | if (res.knownrange) | |
2002 | res.range.likely = res.range.max; | |
2003 | else if (res.range.min < 3 | |
2004 | && dir.prec[0] < 0 | |
2005 | && (unsigned HOST_WIDE_INT)dir.prec[1] == target_int_max ()) | |
2006 | res.range.likely = 3; | |
2007 | else | |
2008 | res.range.likely = res.range.min; | |
5d93da1d | 2009 | |
5d93da1d MS |
2010 | res.range.unlikely = res.range.max; |
2011 | ||
2012 | if (res.range.max > 2 && (prec[0] != 0 || prec[1] != 0)) | |
954b452a | 2013 | { |
5d93da1d MS |
2014 | /* Unless the precision is zero output longer than 2 bytes may |
2015 | include the decimal point which must be a single character | |
2016 | up to MB_LEN_MAX in length. This is overly conservative | |
2017 | since in some conversions some constants result in no decimal | |
2018 | point (e.g., in %g). */ | |
2019 | res.range.unlikely += target_mb_len_max () - 1; | |
88d0c3f0 MS |
2020 | } |
2021 | ||
5d93da1d | 2022 | res.adjust_for_width_or_precision (dir.width); |
954b452a | 2023 | return res; |
88d0c3f0 MS |
2024 | } |
2025 | ||
2026 | /* Return a FMTRESULT struct set to the lengths of the shortest and longest | |
2027 | strings referenced by the expression STR, or (-1, -1) when not known. | |
2028 | Used by the format_string function below. */ | |
2029 | ||
2030 | static fmtresult | |
1ff4dbdd | 2031 | get_string_length (tree str, gimple *stmt, unsigned HOST_WIDE_INT max_size, |
3c9f762a | 2032 | unsigned eltsize, pointer_query &ptr_qry) |
88d0c3f0 MS |
2033 | { |
2034 | if (!str) | |
1eb4547b | 2035 | return fmtresult (); |
88d0c3f0 | 2036 | |
a7160771 MS |
2037 | /* Try to determine the dynamic string length first. |
2038 | Set MAXBOUND to an arbitrary non-null non-integer node as a request | |
2039 | to have it set to the length of the longest string in a PHI. */ | |
5d6655eb | 2040 | c_strlen_data lendata = { }; |
a7160771 | 2041 | lendata.maxbound = str; |
22fca489 | 2042 | if (eltsize == 1) |
3c9f762a | 2043 | get_range_strlen_dynamic (str, stmt, &lendata, ptr_qry); |
22fca489 MS |
2044 | else |
2045 | { | |
2046 | /* Determine the length of the shortest and longest string referenced | |
2047 | by STR. Strings of unknown lengths are bounded by the sizes of | |
2048 | arrays that subexpressions of STR may refer to. Pointers that | |
2049 | aren't known to point any such arrays result in LENDATA.MAXLEN | |
2050 | set to SIZE_MAX. */ | |
2051 | get_range_strlen (str, &lendata, eltsize); | |
2052 | } | |
2053 | ||
a7160771 MS |
2054 | /* If LENDATA.MAXBOUND is not equal to .MINLEN it corresponds to the bound |
2055 | of the largest array STR refers to, if known, or it's set to SIZE_MAX | |
2056 | otherwise. */ | |
5d6655eb | 2057 | |
22fca489 | 2058 | /* Return the default result when nothing is known about the string. */ |
a7160771 MS |
2059 | if ((lendata.maxbound && !tree_fits_uhwi_p (lendata.maxbound)) |
2060 | || !tree_fits_uhwi_p (lendata.maxlen)) | |
407b92bc | 2061 | { |
a7160771 MS |
2062 | fmtresult res; |
2063 | res.nonstr = lendata.decl; | |
2064 | return res; | |
2065 | } | |
2066 | ||
2067 | unsigned HOST_WIDE_INT lenmax = tree_to_uhwi (max_object_size ()) - 2; | |
2068 | if (integer_zerop (lendata.minlen) | |
2069 | && (!lendata.maxbound || lenmax <= tree_to_uhwi (lendata.maxbound)) | |
2070 | && lenmax <= tree_to_uhwi (lendata.maxlen)) | |
2071 | { | |
1ff4dbdd MS |
2072 | if (max_size > 0 && max_size < HOST_WIDE_INT_MAX) |
2073 | { | |
2074 | /* Adjust the conservative unknown/unbounded result if MAX_SIZE | |
2075 | is valid. Set UNLIKELY to maximum in case MAX_SIZE refers | |
2076 | to a subobject. | |
2077 | TODO: This is overly conservative. Set UNLIKELY to the size | |
2078 | of the outermost enclosing declared object. */ | |
2079 | fmtresult res (0, max_size - 1); | |
2080 | res.nonstr = lendata.decl; | |
2081 | res.range.likely = res.range.max; | |
2082 | res.range.unlikely = HOST_WIDE_INT_MAX; | |
2083 | return res; | |
2084 | } | |
2085 | ||
a7160771 MS |
2086 | fmtresult res; |
2087 | res.nonstr = lendata.decl; | |
2088 | return res; | |
407b92bc | 2089 | } |
88d0c3f0 | 2090 | |
3c9f762a | 2091 | /* The minimum length of the string. */ |
5d6655eb MS |
2092 | HOST_WIDE_INT min |
2093 | = (tree_fits_uhwi_p (lendata.minlen) | |
2094 | ? tree_to_uhwi (lendata.minlen) | |
2095 | : 0); | |
2096 | ||
3c9f762a MS |
2097 | /* The maximum length of the string; initially set to MAXBOUND which |
2098 | may be less than MAXLEN, but may be adjusted up below. */ | |
5d6655eb | 2099 | HOST_WIDE_INT max |
22fca489 | 2100 | = (lendata.maxbound && tree_fits_uhwi_p (lendata.maxbound) |
5d6655eb MS |
2101 | ? tree_to_uhwi (lendata.maxbound) |
2102 | : HOST_WIDE_INT_M1U); | |
2103 | ||
3c9f762a MS |
2104 | /* True if either the maximum length is unknown or (conservatively) |
2105 | the array bound is less than the maximum length. That can happen | |
2106 | when the length of the string is unknown but the array in which | |
2107 | the string is stored is a member of a struct. The warning uses | |
2108 | the size of the member as the upper bound but the optimization | |
2109 | doesn't. The optimization could still use the size of | |
2110 | enclosing object as the upper bound but that's not done here. */ | |
2111 | const bool unbounded | |
2112 | = (integer_all_onesp (lendata.maxlen) | |
2113 | || (lendata.maxbound | |
2114 | && tree_int_cst_lt (lendata.maxbound, lendata.maxlen))); | |
5d6655eb MS |
2115 | |
2116 | /* Set the max/likely counters to unbounded when a minimum is known | |
2117 | but the maximum length isn't bounded. This implies that STR is | |
2118 | a conditional expression involving a string of known length and | |
d2ae6d5c | 2119 | an expression of unknown/unbounded length. */ |
5d6655eb MS |
2120 | if (min |
2121 | && (unsigned HOST_WIDE_INT)min < HOST_WIDE_INT_M1U | |
2122 | && unbounded) | |
2123 | max = HOST_WIDE_INT_M1U; | |
2124 | ||
2125 | /* get_range_strlen() returns the target value of SIZE_MAX for | |
2126 | strings of unknown length. Bump it up to HOST_WIDE_INT_M1U | |
2127 | which may be bigger. */ | |
2128 | if ((unsigned HOST_WIDE_INT)min == target_size_max ()) | |
2129 | min = HOST_WIDE_INT_M1U; | |
2130 | if ((unsigned HOST_WIDE_INT)max == target_size_max ()) | |
2131 | max = HOST_WIDE_INT_M1U; | |
2132 | ||
2133 | fmtresult res (min, max); | |
2134 | res.nonstr = lendata.decl; | |
2135 | ||
2136 | /* Set RES.KNOWNRANGE to true if and only if all strings referenced | |
2137 | by STR are known to be bounded (though not necessarily by their | |
2138 | actual length but perhaps by their maximum possible length). */ | |
2139 | if (res.range.max < target_int_max ()) | |
88d0c3f0 | 2140 | { |
5d6655eb | 2141 | res.knownrange = true; |
700d4cb0 | 2142 | /* When the length of the longest string is known and not |
5d6655eb MS |
2143 | excessive use it as the likely length of the string(s). */ |
2144 | res.range.likely = res.range.max; | |
2145 | } | |
2146 | else | |
2147 | { | |
2148 | /* When the upper bound is unknown (it can be zero or excessive) | |
22fca489 | 2149 | set the likely length to the greater of 1. If MAXBOUND is |
a7160771 | 2150 | known, also reset the length of the lower bound to zero. */ |
5d6655eb | 2151 | res.range.likely = res.range.min ? res.range.min : warn_level > 1; |
a7160771 | 2152 | if (lendata.maxbound && !integer_all_onesp (lendata.maxbound)) |
22fca489 | 2153 | res.range.min = 0; |
88d0c3f0 MS |
2154 | } |
2155 | ||
5d6655eb MS |
2156 | res.range.unlikely = unbounded ? HOST_WIDE_INT_MAX : res.range.max; |
2157 | ||
2158 | return res; | |
88d0c3f0 MS |
2159 | } |
2160 | ||
2161 | /* Return the minimum and maximum number of characters formatted | |
954b452a MS |
2162 | by the '%c' format directives and its wide character form for |
2163 | the argument ARG. ARG can be null (for functions such as | |
2164 | vsprinf). */ | |
88d0c3f0 MS |
2165 | |
2166 | static fmtresult | |
3c9f762a | 2167 | format_character (const directive &dir, tree arg, pointer_query &ptr_qry) |
88d0c3f0 | 2168 | { |
1eb4547b | 2169 | fmtresult res; |
88d0c3f0 | 2170 | |
5d93da1d MS |
2171 | res.knownrange = true; |
2172 | ||
bbcbd744 MS |
2173 | if (dir.specifier == 'C' |
2174 | || dir.modifier == FMT_LEN_l) | |
5d93da1d | 2175 | { |
5d93da1d MS |
2176 | /* A wide character can result in as few as zero bytes. */ |
2177 | res.range.min = 0; | |
2178 | ||
31c87a43 | 2179 | HOST_WIDE_INT min, max; |
3c9f762a MS |
2180 | if (get_int_range (arg, dir.info->callstmt, &min, &max, false, 0, |
2181 | ptr_qry.rvals)) | |
5d93da1d | 2182 | { |
31c87a43 MS |
2183 | if (min == 0 && max == 0) |
2184 | { | |
2185 | /* The NUL wide character results in no bytes. */ | |
2186 | res.range.max = 0; | |
2187 | res.range.likely = 0; | |
2188 | res.range.unlikely = 0; | |
2189 | } | |
bbcbd744 | 2190 | else if (min >= 0 && min < 128) |
31c87a43 | 2191 | { |
bbcbd744 MS |
2192 | /* Be conservative if the target execution character set |
2193 | is not a 1-to-1 mapping to the source character set or | |
2194 | if the source set is not ASCII. */ | |
2195 | bool one_2_one_ascii | |
2196 | = (target_to_host_charmap[0] == 1 && target_to_host ('a') == 97); | |
2197 | ||
31c87a43 MS |
2198 | /* A wide character in the ASCII range most likely results |
2199 | in a single byte, and only unlikely in up to MB_LEN_MAX. */ | |
bbcbd744 | 2200 | res.range.max = one_2_one_ascii ? 1 : target_mb_len_max ();; |
31c87a43 MS |
2201 | res.range.likely = 1; |
2202 | res.range.unlikely = target_mb_len_max (); | |
bbcbd744 | 2203 | res.mayfail = !one_2_one_ascii; |
31c87a43 MS |
2204 | } |
2205 | else | |
2206 | { | |
2207 | /* A wide character outside the ASCII range likely results | |
2208 | in up to two bytes, and only unlikely in up to MB_LEN_MAX. */ | |
2209 | res.range.max = target_mb_len_max (); | |
2210 | res.range.likely = 2; | |
2211 | res.range.unlikely = res.range.max; | |
bbcbd744 MS |
2212 | /* Converting such a character may fail. */ |
2213 | res.mayfail = true; | |
31c87a43 | 2214 | } |
5d93da1d MS |
2215 | } |
2216 | else | |
2217 | { | |
31c87a43 MS |
2218 | /* An unknown wide character is treated the same as a wide |
2219 | character outside the ASCII range. */ | |
5d93da1d MS |
2220 | res.range.max = target_mb_len_max (); |
2221 | res.range.likely = 2; | |
2222 | res.range.unlikely = res.range.max; | |
bbcbd744 | 2223 | res.mayfail = true; |
5d93da1d | 2224 | } |
954b452a MS |
2225 | } |
2226 | else | |
2227 | { | |
d2ae6d5c | 2228 | /* A plain '%c' directive. Its output is exactly 1. */ |
954b452a | 2229 | res.range.min = res.range.max = 1; |
31c87a43 | 2230 | res.range.likely = res.range.unlikely = 1; |
954b452a | 2231 | res.knownrange = true; |
954b452a MS |
2232 | } |
2233 | ||
5d93da1d MS |
2234 | /* Bump up the byte counters if WIDTH is greater. */ |
2235 | return res.adjust_for_width_or_precision (dir.width); | |
954b452a MS |
2236 | } |
2237 | ||
1ff4dbdd | 2238 | /* If TYPE is an array or struct or union, increment *FLDOFF by the starting |
10d1986a MS |
2239 | offset of the member that *OFF points into if one can be determined and |
2240 | set *FLDSIZE to its size in bytes and decrement *OFF by the same. | |
2241 | Otherwise do nothing. */ | |
937a86b4 | 2242 | |
1ff4dbdd MS |
2243 | static void |
2244 | set_aggregate_size_and_offset (tree type, HOST_WIDE_INT *fldoff, | |
2245 | HOST_WIDE_INT *fldsize, HOST_WIDE_INT *off) | |
937a86b4 | 2246 | { |
1ff4dbdd MS |
2247 | /* The byte offset of the most basic struct member the byte |
2248 | offset *OFF corresponds to, or for a (multidimensional) | |
2249 | array member, the byte offset of the array element. */ | |
2250 | if (TREE_CODE (type) == ARRAY_TYPE | |
2251 | && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE) | |
937a86b4 | 2252 | { |
1ff4dbdd MS |
2253 | HOST_WIDE_INT index = 0, arrsize = 0; |
2254 | if (array_elt_at_offset (type, *off, &index, &arrsize)) | |
2255 | { | |
2256 | *fldoff += index; | |
1ff4dbdd MS |
2257 | *fldsize = arrsize; |
2258 | } | |
10d1986a | 2259 | /* Otherwise leave *FLDOFF et al. unchanged. */ |
937a86b4 | 2260 | } |
1ff4dbdd | 2261 | else if (RECORD_OR_UNION_TYPE_P (type)) |
937a86b4 | 2262 | { |
1ff4dbdd MS |
2263 | HOST_WIDE_INT index = 0; |
2264 | tree sub = field_at_offset (type, NULL_TREE, *off, &index); | |
2265 | if (sub) | |
937a86b4 | 2266 | { |
1ff4dbdd MS |
2267 | tree subsize = DECL_SIZE_UNIT (sub); |
2268 | if (*fldsize < HOST_WIDE_INT_MAX | |
2269 | && subsize | |
2270 | && tree_fits_uhwi_p (subsize)) | |
2271 | *fldsize = tree_to_uhwi (subsize); | |
937a86b4 | 2272 | else |
1ff4dbdd MS |
2273 | *fldsize = HOST_WIDE_INT_MAX; |
2274 | *fldoff += index; | |
2275 | *off -= index; | |
937a86b4 | 2276 | } |
10d1986a | 2277 | /* Otherwise leave *FLDOFF et al. unchanged. */ |
937a86b4 | 2278 | } |
937a86b4 MS |
2279 | } |
2280 | ||
10d1986a MS |
2281 | /* For an expression X of pointer type, recursively try to find its origin |
2282 | (either object DECL or pointer such as PARM_DECL) Y and return such a Y. | |
1ff4dbdd MS |
2283 | When X refers to an array element or struct member, set *FLDOFF to |
2284 | the offset of the element or member from the beginning of the "most | |
2285 | derived" object and *FLDSIZE to its size. When nonnull, set *OFF to | |
2286 | the overall offset from the beginning of the object so that | |
2287 | *FLDOFF <= *OFF. */ | |
937a86b4 MS |
2288 | |
2289 | static tree | |
1ff4dbdd MS |
2290 | get_origin_and_offset_r (tree x, HOST_WIDE_INT *fldoff, HOST_WIDE_INT *fldsize, |
2291 | HOST_WIDE_INT *off) | |
937a86b4 | 2292 | { |
1ff4dbdd MS |
2293 | HOST_WIDE_INT sizebuf = -1; |
2294 | if (!fldsize) | |
2295 | fldsize = &sizebuf; | |
2296 | ||
2297 | if (DECL_P (x)) | |
2298 | { | |
2299 | /* Set the size if it hasn't been set yet. */ | |
2300 | if (tree size = DECL_SIZE_UNIT (x)) | |
2301 | if (*fldsize < 0 && tree_fits_shwi_p (size)) | |
2302 | *fldsize = tree_to_shwi (size); | |
2303 | return x; | |
2304 | } | |
2305 | ||
937a86b4 MS |
2306 | switch (TREE_CODE (x)) |
2307 | { | |
2308 | case ADDR_EXPR: | |
2309 | x = TREE_OPERAND (x, 0); | |
1ff4dbdd | 2310 | return get_origin_and_offset_r (x, fldoff, fldsize, off); |
937a86b4 MS |
2311 | |
2312 | case ARRAY_REF: | |
2313 | { | |
10d1986a MS |
2314 | tree sub = TREE_OPERAND (x, 1); |
2315 | unsigned HOST_WIDE_INT idx = | |
2316 | tree_fits_uhwi_p (sub) ? tree_to_uhwi (sub) : HOST_WIDE_INT_MAX; | |
937a86b4 | 2317 | |
10d1986a MS |
2318 | tree elsz = array_ref_element_size (x); |
2319 | unsigned HOST_WIDE_INT elbytes = | |
2320 | tree_fits_shwi_p (elsz) ? tree_to_shwi (elsz) : HOST_WIDE_INT_MAX; | |
2321 | ||
2322 | unsigned HOST_WIDE_INT byteoff = idx * elbytes; | |
2323 | ||
2324 | if (byteoff < HOST_WIDE_INT_MAX | |
2325 | && elbytes < HOST_WIDE_INT_MAX | |
edb9330c | 2326 | && (elbytes == 0 || byteoff / elbytes == idx)) |
937a86b4 | 2327 | { |
10d1986a MS |
2328 | /* For in-bounds constant offsets into constant-sized arrays |
2329 | bump up *OFF, and for what's likely arrays or structs of | |
2330 | arrays, also *FLDOFF, as necessary. */ | |
937a86b4 | 2331 | if (off) |
10d1986a MS |
2332 | *off += byteoff; |
2333 | if (elbytes > 1) | |
2334 | *fldoff += byteoff; | |
937a86b4 | 2335 | } |
937a86b4 | 2336 | else |
10d1986a | 2337 | *fldoff = HOST_WIDE_INT_MAX; |
937a86b4 MS |
2338 | |
2339 | x = TREE_OPERAND (x, 0); | |
10d1986a | 2340 | return get_origin_and_offset_r (x, fldoff, fldsize, off); |
937a86b4 MS |
2341 | } |
2342 | ||
2343 | case MEM_REF: | |
2344 | if (off) | |
2345 | { | |
2346 | tree offset = TREE_OPERAND (x, 1); | |
2347 | *off = (tree_fits_uhwi_p (offset) | |
2348 | ? tree_to_uhwi (offset) : HOST_WIDE_INT_MAX); | |
2349 | } | |
2350 | ||
2351 | x = TREE_OPERAND (x, 0); | |
2352 | ||
2353 | if (off) | |
2354 | { | |
8fefa21f RB |
2355 | tree xtype |
2356 | = (TREE_CODE (x) == ADDR_EXPR | |
2357 | ? TREE_TYPE (TREE_OPERAND (x, 0)) : TREE_TYPE (TREE_TYPE (x))); | |
937a86b4 | 2358 | |
1ff4dbdd | 2359 | set_aggregate_size_and_offset (xtype, fldoff, fldsize, off); |
937a86b4 MS |
2360 | } |
2361 | ||
1ff4dbdd | 2362 | return get_origin_and_offset_r (x, fldoff, fldsize, nullptr); |
937a86b4 MS |
2363 | |
2364 | case COMPONENT_REF: | |
2365 | { | |
10d1986a | 2366 | tree foff = component_ref_field_offset (x); |
937a86b4 | 2367 | tree fld = TREE_OPERAND (x, 1); |
10d1986a MS |
2368 | if (!tree_fits_shwi_p (foff) |
2369 | || !tree_fits_shwi_p (DECL_FIELD_BIT_OFFSET (fld))) | |
2370 | return x; | |
2371 | *fldoff += (tree_to_shwi (foff) | |
2372 | + (tree_to_shwi (DECL_FIELD_BIT_OFFSET (fld)) | |
2373 | / BITS_PER_UNIT)); | |
937a86b4 | 2374 | |
1ff4dbdd | 2375 | get_origin_and_offset_r (fld, fldoff, fldsize, off); |
937a86b4 | 2376 | x = TREE_OPERAND (x, 0); |
1ff4dbdd | 2377 | return get_origin_and_offset_r (x, fldoff, nullptr, off); |
937a86b4 MS |
2378 | } |
2379 | ||
2380 | case SSA_NAME: | |
2381 | { | |
2382 | gimple *def = SSA_NAME_DEF_STMT (x); | |
2383 | if (is_gimple_assign (def)) | |
2384 | { | |
2385 | tree_code code = gimple_assign_rhs_code (def); | |
2386 | if (code == ADDR_EXPR) | |
2387 | { | |
2388 | x = gimple_assign_rhs1 (def); | |
1ff4dbdd | 2389 | return get_origin_and_offset_r (x, fldoff, fldsize, off); |
937a86b4 MS |
2390 | } |
2391 | ||
2392 | if (code == POINTER_PLUS_EXPR) | |
2393 | { | |
2394 | tree offset = gimple_assign_rhs2 (def); | |
1ff4dbdd MS |
2395 | if (off && tree_fits_uhwi_p (offset)) |
2396 | *off = tree_to_uhwi (offset); | |
937a86b4 MS |
2397 | |
2398 | x = gimple_assign_rhs1 (def); | |
1ff4dbdd MS |
2399 | x = get_origin_and_offset_r (x, fldoff, fldsize, off); |
2400 | if (off && !tree_fits_uhwi_p (offset)) | |
2401 | *off = HOST_WIDE_INT_MAX; | |
2402 | if (off) | |
2403 | { | |
2404 | tree xtype = TREE_TYPE (x); | |
2405 | set_aggregate_size_and_offset (xtype, fldoff, fldsize, off); | |
2406 | } | |
2407 | return x; | |
937a86b4 MS |
2408 | } |
2409 | else if (code == VAR_DECL) | |
2410 | { | |
2411 | x = gimple_assign_rhs1 (def); | |
1ff4dbdd | 2412 | return get_origin_and_offset_r (x, fldoff, fldsize, off); |
937a86b4 MS |
2413 | } |
2414 | } | |
2415 | else if (gimple_nop_p (def) && SSA_NAME_VAR (x)) | |
2416 | x = SSA_NAME_VAR (x); | |
1ff4dbdd MS |
2417 | |
2418 | tree xtype = TREE_TYPE (x); | |
2419 | if (POINTER_TYPE_P (xtype)) | |
2420 | xtype = TREE_TYPE (xtype); | |
2421 | ||
2422 | if (off) | |
2423 | set_aggregate_size_and_offset (xtype, fldoff, fldsize, off); | |
937a86b4 MS |
2424 | } |
2425 | ||
2426 | default: | |
2427 | break; | |
2428 | } | |
2429 | ||
2430 | return x; | |
2431 | } | |
2432 | ||
10d1986a MS |
2433 | /* Nonrecursive version of the above. |
2434 | The function never returns null unless X is null to begin with. */ | |
1ff4dbdd MS |
2435 | |
2436 | static tree | |
2437 | get_origin_and_offset (tree x, HOST_WIDE_INT *fldoff, HOST_WIDE_INT *off, | |
2438 | HOST_WIDE_INT *fldsize = nullptr) | |
2439 | { | |
10d1986a MS |
2440 | if (!x) |
2441 | return NULL_TREE; | |
2442 | ||
1ff4dbdd MS |
2443 | HOST_WIDE_INT sizebuf; |
2444 | if (!fldsize) | |
2445 | fldsize = &sizebuf; | |
2446 | ||
10d1986a | 2447 | /* Invalidate *FLDSIZE. */ |
1ff4dbdd | 2448 | *fldsize = -1; |
10d1986a | 2449 | *fldoff = *off = 0; |
1ff4dbdd | 2450 | |
10d1986a | 2451 | return get_origin_and_offset_r (x, fldoff, fldsize, off); |
1ff4dbdd MS |
2452 | } |
2453 | ||
937a86b4 MS |
2454 | /* If ARG refers to the same (sub)object or array element as described |
2455 | by DST and DST_FLD, return the byte offset into the struct member or | |
1ff4dbdd MS |
2456 | array element referenced by ARG and set *ARG_SIZE to the size of |
2457 | the (sub)object. Otherwise return HOST_WIDE_INT_MIN to indicate | |
2458 | that ARG and DST do not refer to the same object. */ | |
937a86b4 MS |
2459 | |
2460 | static HOST_WIDE_INT | |
1ff4dbdd MS |
2461 | alias_offset (tree arg, HOST_WIDE_INT *arg_size, |
2462 | tree dst, HOST_WIDE_INT dst_fld) | |
937a86b4 MS |
2463 | { |
2464 | /* See if the argument refers to the same base object as the destination | |
2465 | of the formatted function call, and if so, try to determine if they | |
2466 | can alias. */ | |
2467 | if (!arg || !dst || !ptr_derefs_may_alias_p (arg, dst)) | |
2468 | return HOST_WIDE_INT_MIN; | |
2469 | ||
2470 | /* The two arguments may refer to the same object. If they both refer | |
10d1986a MS |
2471 | to a struct member, see if the members are one and the same. If so, |
2472 | return the offset into the member. */ | |
937a86b4 MS |
2473 | HOST_WIDE_INT arg_off = 0, arg_fld = 0; |
2474 | ||
1ff4dbdd | 2475 | tree arg_orig = get_origin_and_offset (arg, &arg_fld, &arg_off, arg_size); |
937a86b4 MS |
2476 | |
2477 | if (arg_orig == dst && arg_fld == dst_fld) | |
2478 | return arg_off; | |
2479 | ||
2480 | return HOST_WIDE_INT_MIN; | |
2481 | } | |
2482 | ||
954b452a | 2483 | /* Return the minimum and maximum number of characters formatted |
31c87a43 MS |
2484 | by the '%s' format directive and its wide character form for |
2485 | the argument ARG. ARG can be null (for functions such as | |
2486 | vsprinf). */ | |
954b452a MS |
2487 | |
2488 | static fmtresult | |
3c9f762a | 2489 | format_string (const directive &dir, tree arg, pointer_query &ptr_qry) |
954b452a MS |
2490 | { |
2491 | fmtresult res; | |
88d0c3f0 | 2492 | |
1ff4dbdd MS |
2493 | /* The size of the (sub)object ARG refers to. Used to adjust |
2494 | the conservative get_string_length() result. */ | |
2495 | HOST_WIDE_INT arg_size = 0; | |
2496 | ||
937a86b4 MS |
2497 | if (warn_restrict) |
2498 | { | |
2499 | /* See if ARG might alias the destination of the call with | |
2500 | DST_ORIGIN and DST_FIELD. If so, store the starting offset | |
2501 | so that the overlap can be determined for certain later, | |
2502 | when the amount of output of the call (including subsequent | |
2503 | directives) has been computed. Otherwise, store HWI_MIN. */ | |
1ff4dbdd | 2504 | res.dst_offset = alias_offset (arg, &arg_size, dir.info->dst_origin, |
937a86b4 | 2505 | dir.info->dst_field); |
1ff4dbdd MS |
2506 | if (res.dst_offset >= 0 && res.dst_offset <= arg_size) |
2507 | arg_size -= res.dst_offset; | |
2508 | else | |
2509 | arg_size = 0; | |
937a86b4 MS |
2510 | } |
2511 | ||
954b452a | 2512 | /* Compute the range the argument's length can be in. */ |
0863decd JL |
2513 | int count_by = 1; |
2514 | if (dir.specifier == 'S' || dir.modifier == FMT_LEN_l) | |
2515 | { | |
2516 | /* Get a node for a C type that will be the same size | |
2517 | as a wchar_t on the target. */ | |
2518 | tree node = get_typenode_from_name (MODIFIED_WCHAR_TYPE); | |
2519 | ||
2520 | /* Now that we have a suitable node, get the number of | |
2521 | bytes it occupies. */ | |
937a86b4 | 2522 | count_by = int_size_in_bytes (node); |
0863decd JL |
2523 | gcc_checking_assert (count_by == 2 || count_by == 4); |
2524 | } | |
2525 | ||
1ff4dbdd | 2526 | fmtresult slen = |
3c9f762a | 2527 | get_string_length (arg, dir.info->callstmt, arg_size, count_by, ptr_qry); |
a151e93b MS |
2528 | if (slen.range.min == slen.range.max |
2529 | && slen.range.min < HOST_WIDE_INT_MAX) | |
88d0c3f0 | 2530 | { |
5d93da1d MS |
2531 | /* The argument is either a string constant or it refers |
2532 | to one of a number of strings of the same length. */ | |
88d0c3f0 | 2533 | |
954b452a MS |
2534 | /* A '%s' directive with a string argument with constant length. */ |
2535 | res.range = slen.range; | |
88d0c3f0 | 2536 | |
bbcbd744 MS |
2537 | if (dir.specifier == 'S' |
2538 | || dir.modifier == FMT_LEN_l) | |
954b452a | 2539 | { |
5d93da1d MS |
2540 | /* In the worst case the length of output of a wide string S |
2541 | is bounded by MB_LEN_MAX * wcslen (S). */ | |
2542 | res.range.max *= target_mb_len_max (); | |
2543 | res.range.unlikely = res.range.max; | |
700d4cb0 | 2544 | /* It's likely that the total length is not more that |
5d93da1d MS |
2545 | 2 * wcslen (S).*/ |
2546 | res.range.likely = res.range.min * 2; | |
88d0c3f0 | 2547 | |
8657c838 | 2548 | if (dir.prec[1] >= 0 |
31c87a43 | 2549 | && (unsigned HOST_WIDE_INT)dir.prec[1] < res.range.max) |
5d93da1d | 2550 | { |
31c87a43 MS |
2551 | res.range.max = dir.prec[1]; |
2552 | res.range.likely = dir.prec[1]; | |
2553 | res.range.unlikely = dir.prec[1]; | |
5d93da1d | 2554 | } |
88d0c3f0 | 2555 | |
31c87a43 MS |
2556 | if (dir.prec[0] < 0 && dir.prec[1] > -1) |
2557 | res.range.min = 0; | |
8657c838 | 2558 | else if (dir.prec[0] >= 0) |
31c87a43 MS |
2559 | res.range.likely = dir.prec[0]; |
2560 | ||
5d93da1d MS |
2561 | /* Even a non-empty wide character string need not convert into |
2562 | any bytes. */ | |
2563 | res.range.min = 0; | |
bbcbd744 MS |
2564 | |
2565 | /* A non-empty wide character conversion may fail. */ | |
2566 | if (slen.range.max > 0) | |
2567 | res.mayfail = true; | |
954b452a | 2568 | } |
5d93da1d | 2569 | else |
954b452a | 2570 | { |
5d93da1d MS |
2571 | res.knownrange = true; |
2572 | ||
31c87a43 | 2573 | if (dir.prec[0] < 0 && dir.prec[1] > -1) |
5d93da1d | 2574 | res.range.min = 0; |
31c87a43 MS |
2575 | else if ((unsigned HOST_WIDE_INT)dir.prec[0] < res.range.min) |
2576 | res.range.min = dir.prec[0]; | |
2577 | ||
2578 | if ((unsigned HOST_WIDE_INT)dir.prec[1] < res.range.max) | |
5d93da1d | 2579 | { |
31c87a43 MS |
2580 | res.range.max = dir.prec[1]; |
2581 | res.range.likely = dir.prec[1]; | |
2582 | res.range.unlikely = dir.prec[1]; | |
5d93da1d | 2583 | } |
954b452a MS |
2584 | } |
2585 | } | |
2586 | else if (arg && integer_zerop (arg)) | |
2587 | { | |
2588 | /* Handle null pointer argument. */ | |
88d0c3f0 | 2589 | |
954b452a MS |
2590 | fmtresult res (0); |
2591 | res.nullp = true; | |
2592 | return res; | |
2593 | } | |
2594 | else | |
2595 | { | |
ebee1eb9 MS |
2596 | /* For a '%s' and '%ls' directive with a non-constant string (either |
2597 | one of a number of strings of known length or an unknown string) | |
2598 | the minimum number of characters is lesser of PRECISION[0] and | |
2599 | the length of the shortest known string or zero, and the maximum | |
d2ae6d5c | 2600 | is the lesser of the length of the longest known string or |
ebee1eb9 MS |
2601 | PTRDIFF_MAX and PRECISION[1]. The likely length is either |
2602 | the minimum at level 1 and the greater of the minimum and 1 | |
2603 | at level 2. This result is adjust upward for width (if it's | |
2604 | specified). */ | |
2605 | ||
bbcbd744 MS |
2606 | if (dir.specifier == 'S' |
2607 | || dir.modifier == FMT_LEN_l) | |
ebee1eb9 MS |
2608 | { |
2609 | /* A wide character converts to as few as zero bytes. */ | |
2610 | slen.range.min = 0; | |
2611 | if (slen.range.max < target_int_max ()) | |
2612 | slen.range.max *= target_mb_len_max (); | |
2613 | ||
2614 | if (slen.range.likely < target_int_max ()) | |
2615 | slen.range.likely *= 2; | |
2616 | ||
2617 | if (slen.range.likely < target_int_max ()) | |
2618 | slen.range.unlikely *= target_mb_len_max (); | |
bbcbd744 MS |
2619 | |
2620 | /* A non-empty wide character conversion may fail. */ | |
2621 | if (slen.range.max > 0) | |
2622 | res.mayfail = true; | |
ebee1eb9 MS |
2623 | } |
2624 | ||
2625 | res.range = slen.range; | |
88d0c3f0 | 2626 | |
8657c838 | 2627 | if (dir.prec[0] >= 0) |
954b452a | 2628 | { |
ebee1eb9 MS |
2629 | /* Adjust the minimum to zero if the string length is unknown, |
2630 | or at most the lower bound of the precision otherwise. */ | |
954b452a | 2631 | if (slen.range.min >= target_int_max ()) |
ebee1eb9 | 2632 | res.range.min = 0; |
31c87a43 | 2633 | else if ((unsigned HOST_WIDE_INT)dir.prec[0] < slen.range.min) |
ebee1eb9 | 2634 | res.range.min = dir.prec[0]; |
954b452a | 2635 | |
ebee1eb9 | 2636 | /* Make both maxima no greater than the upper bound of precision. */ |
31c87a43 | 2637 | if ((unsigned HOST_WIDE_INT)dir.prec[1] < slen.range.max |
954b452a | 2638 | || slen.range.max >= target_int_max ()) |
5d93da1d | 2639 | { |
ebee1eb9 MS |
2640 | res.range.max = dir.prec[1]; |
2641 | res.range.unlikely = dir.prec[1]; | |
5d93da1d | 2642 | } |
ebee1eb9 MS |
2643 | |
2644 | /* If precision is constant, set the likely counter to the lesser | |
2645 | of it and the maximum string length. Otherwise, if the lower | |
2646 | bound of precision is greater than zero, set the likely counter | |
2647 | to the minimum. Otherwise set it to zero or one based on | |
2648 | the warning level. */ | |
2649 | if (dir.prec[0] == dir.prec[1]) | |
2650 | res.range.likely | |
2651 | = ((unsigned HOST_WIDE_INT)dir.prec[0] < slen.range.max | |
2652 | ? dir.prec[0] : slen.range.max); | |
2653 | else if (dir.prec[0] > 0) | |
2654 | res.range.likely = res.range.min; | |
2655 | else | |
2656 | res.range.likely = warn_level > 1; | |
2657 | } | |
2658 | else if (dir.prec[1] >= 0) | |
2659 | { | |
2660 | res.range.min = 0; | |
2661 | if ((unsigned HOST_WIDE_INT)dir.prec[1] < slen.range.max) | |
2662 | res.range.max = dir.prec[1]; | |
2663 | res.range.likely = dir.prec[1] ? warn_level > 1 : 0; | |
5d6655eb MS |
2664 | if ((unsigned HOST_WIDE_INT)dir.prec[1] < slen.range.unlikely) |
2665 | res.range.unlikely = dir.prec[1]; | |
954b452a MS |
2666 | } |
2667 | else if (slen.range.min >= target_int_max ()) | |
2668 | { | |
ebee1eb9 MS |
2669 | res.range.min = 0; |
2670 | res.range.max = HOST_WIDE_INT_MAX; | |
2671 | /* At level 1 strings of unknown length are assumed to be | |
5d93da1d MS |
2672 | empty, while at level 1 they are assumed to be one byte |
2673 | long. */ | |
ebee1eb9 | 2674 | res.range.likely = warn_level > 1; |
5d6655eb | 2675 | res.range.unlikely = HOST_WIDE_INT_MAX; |
ebee1eb9 MS |
2676 | } |
2677 | else | |
2678 | { | |
2679 | /* A string of unknown length unconstrained by precision is | |
2680 | assumed to be empty at level 1 and just one character long | |
2681 | at higher levels. */ | |
2682 | if (res.range.likely >= target_int_max ()) | |
2683 | res.range.likely = warn_level > 1; | |
88d0c3f0 MS |
2684 | } |
2685 | } | |
2686 | ||
a41cc59d MS |
2687 | /* If the argument isn't a nul-terminated string and the number |
2688 | of bytes on output isn't bounded by precision, set NONSTR. */ | |
2689 | if (slen.nonstr && slen.range.min < (unsigned HOST_WIDE_INT)dir.prec[0]) | |
2690 | res.nonstr = slen.nonstr; | |
2691 | ||
5d93da1d MS |
2692 | /* Bump up the byte counters if WIDTH is greater. */ |
2693 | return res.adjust_for_width_or_precision (dir.width); | |
2694 | } | |
2695 | ||
2696 | /* Format plain string (part of the format string itself). */ | |
2697 | ||
2698 | static fmtresult | |
3c9f762a | 2699 | format_plain (const directive &dir, tree, pointer_query &) |
5d93da1d MS |
2700 | { |
2701 | fmtresult res (dir.len); | |
2702 | return res; | |
2703 | } | |
2704 | ||
2705 | /* Return true if the RESULT of a directive in a call describe by INFO | |
2706 | should be diagnosed given the AVAILable space in the destination. */ | |
2707 | ||
2708 | static bool | |
22fca489 | 2709 | should_warn_p (const call_info &info, |
5d93da1d MS |
2710 | const result_range &avail, const result_range &result) |
2711 | { | |
2712 | if (result.max <= avail.min) | |
2713 | { | |
2714 | /* The least amount of space remaining in the destination is big | |
2715 | enough for the longest output. */ | |
2716 | return false; | |
2717 | } | |
2718 | ||
2719 | if (info.bounded) | |
de6aa933 | 2720 | { |
8657c838 | 2721 | if (warn_format_trunc == 1 && result.min <= avail.max |
5d93da1d MS |
2722 | && info.retval_used ()) |
2723 | { | |
2724 | /* The likely amount of space remaining in the destination is big | |
2725 | enough for the least output and the return value is used. */ | |
2726 | return false; | |
2727 | } | |
88d0c3f0 | 2728 | |
8657c838 | 2729 | if (warn_format_trunc == 1 && result.likely <= avail.likely |
5d93da1d MS |
2730 | && !info.retval_used ()) |
2731 | { | |
2732 | /* The likely amount of space remaining in the destination is big | |
2733 | enough for the likely output and the return value is unused. */ | |
2734 | return false; | |
2735 | } | |
88d0c3f0 | 2736 | |
5d93da1d MS |
2737 | if (warn_format_trunc == 2 |
2738 | && result.likely <= avail.min | |
2739 | && (result.max <= avail.min | |
2740 | || result.max > HOST_WIDE_INT_MAX)) | |
2741 | { | |
2742 | /* The minimum amount of space remaining in the destination is big | |
2743 | enough for the longest output. */ | |
2744 | return false; | |
2745 | } | |
de6aa933 | 2746 | } |
5d93da1d MS |
2747 | else |
2748 | { | |
8657c838 | 2749 | if (warn_level == 1 && result.likely <= avail.likely) |
5d93da1d MS |
2750 | { |
2751 | /* The likely amount of space remaining in the destination is big | |
2752 | enough for the likely output. */ | |
2753 | return false; | |
2754 | } | |
88d0c3f0 | 2755 | |
5d93da1d MS |
2756 | if (warn_level == 2 |
2757 | && result.likely <= avail.min | |
2758 | && (result.max <= avail.min | |
2759 | || result.max > HOST_WIDE_INT_MAX)) | |
2760 | { | |
2761 | /* The minimum amount of space remaining in the destination is big | |
2762 | enough for the longest output. */ | |
2763 | return false; | |
2764 | } | |
2765 | } | |
1eb4547b | 2766 | |
5d93da1d | 2767 | return true; |
88d0c3f0 MS |
2768 | } |
2769 | ||
02f1ef40 MS |
2770 | /* At format string location describe by DIRLOC in a call described |
2771 | by INFO, issue a warning for a directive DIR whose output may be | |
2772 | in excess of the available space AVAIL_RANGE in the destination | |
2773 | given the formatting result FMTRES. This function does nothing | |
2774 | except decide whether to issue a warning for a possible write | |
2775 | past the end or truncation and, if so, format the warning. | |
2776 | Return true if a warning has been issued. */ | |
2777 | ||
2778 | static bool | |
89b6abbb | 2779 | maybe_warn (substring_loc &dirloc, location_t argloc, |
22fca489 | 2780 | const call_info &info, |
5d93da1d | 2781 | const result_range &avail_range, const result_range &res, |
02f1ef40 MS |
2782 | const directive &dir) |
2783 | { | |
5d93da1d MS |
2784 | if (!should_warn_p (info, avail_range, res)) |
2785 | return false; | |
2786 | ||
2787 | /* A warning will definitely be issued below. */ | |
2788 | ||
2789 | /* The maximum byte count to reference in the warning. Larger counts | |
2790 | imply that the upper bound is unknown (and could be anywhere between | |
2791 | RES.MIN + 1 and SIZE_MAX / 2) are printed as "N or more bytes" rather | |
2792 | than "between N and X" where X is some huge number. */ | |
2793 | unsigned HOST_WIDE_INT maxbytes = target_dir_max (); | |
02f1ef40 | 2794 | |
5d93da1d MS |
2795 | /* True when there is enough room in the destination for the least |
2796 | amount of a directive's output but not enough for its likely or | |
2797 | maximum output. */ | |
2798 | bool maybe = (res.min <= avail_range.max | |
2799 | && (avail_range.min < res.likely | |
2800 | || (res.max < HOST_WIDE_INT_MAX | |
2801 | && avail_range.min < res.max))); | |
2802 | ||
d127ae9f MS |
2803 | /* Buffer for the directive in the host character set (used when |
2804 | the source character set is different). */ | |
2805 | char hostdir[32]; | |
2806 | ||
5d93da1d | 2807 | if (avail_range.min == avail_range.max) |
02f1ef40 | 2808 | { |
5d93da1d MS |
2809 | /* The size of the destination region is exact. */ |
2810 | unsigned HOST_WIDE_INT navail = avail_range.max; | |
2811 | ||
d127ae9f | 2812 | if (target_to_host (*dir.beg) != '%') |
02f1ef40 | 2813 | { |
5d93da1d MS |
2814 | /* For plain character directives (i.e., the format string itself) |
2815 | but not others, point the caret at the first character that's | |
2816 | past the end of the destination. */ | |
36367d1f JJ |
2817 | if (navail < dir.len) |
2818 | dirloc.set_caret_index (dirloc.get_caret_idx () + navail); | |
02f1ef40 | 2819 | } |
5d93da1d MS |
2820 | |
2821 | if (*dir.beg == '\0') | |
02f1ef40 | 2822 | { |
5d93da1d MS |
2823 | /* This is the terminating nul. */ |
2824 | gcc_assert (res.min == 1 && res.min == res.max); | |
2825 | ||
89b6abbb | 2826 | return fmtwarn (dirloc, UNKNOWN_LOCATION, NULL, info.warnopt (), |
346114b5 JJ |
2827 | info.bounded |
2828 | ? (maybe | |
2829 | ? G_("%qE output may be truncated before the " | |
2830 | "last format character") | |
2831 | : G_("%qE output truncated before the last " | |
2832 | "format character")) | |
2833 | : (maybe | |
2834 | ? G_("%qE may write a terminating nul past the " | |
2835 | "end of the destination") | |
2836 | : G_("%qE writing a terminating nul past the " | |
2837 | "end of the destination")), | |
2838 | info.func); | |
5d93da1d MS |
2839 | } |
2840 | ||
2841 | if (res.min == res.max) | |
2842 | { | |
346114b5 JJ |
2843 | const char *d = target_to_host (hostdir, sizeof hostdir, dir.beg); |
2844 | if (!info.bounded) | |
2845 | return fmtwarn_n (dirloc, argloc, NULL, info.warnopt (), res.min, | |
2846 | "%<%.*s%> directive writing %wu byte into a " | |
2847 | "region of size %wu", | |
2848 | "%<%.*s%> directive writing %wu bytes into a " | |
2849 | "region of size %wu", | |
2850 | (int) dir.len, d, res.min, navail); | |
2851 | else if (maybe) | |
2852 | return fmtwarn_n (dirloc, argloc, NULL, info.warnopt (), res.min, | |
2853 | "%<%.*s%> directive output may be truncated " | |
2854 | "writing %wu byte into a region of size %wu", | |
2855 | "%<%.*s%> directive output may be truncated " | |
2856 | "writing %wu bytes into a region of size %wu", | |
2857 | (int) dir.len, d, res.min, navail); | |
2858 | else | |
2859 | return fmtwarn_n (dirloc, argloc, NULL, info.warnopt (), res.min, | |
2860 | "%<%.*s%> directive output truncated writing " | |
2861 | "%wu byte into a region of size %wu", | |
2862 | "%<%.*s%> directive output truncated writing " | |
2863 | "%wu bytes into a region of size %wu", | |
2864 | (int) dir.len, d, res.min, navail); | |
5d93da1d | 2865 | } |
8657c838 | 2866 | if (res.min == 0 && res.max < maxbytes) |
346114b5 JJ |
2867 | return fmtwarn (dirloc, argloc, NULL, |
2868 | info.warnopt (), | |
2869 | info.bounded | |
2870 | ? (maybe | |
2871 | ? G_("%<%.*s%> directive output may be truncated " | |
2872 | "writing up to %wu bytes into a region of " | |
2873 | "size %wu") | |
2874 | : G_("%<%.*s%> directive output truncated writing " | |
2875 | "up to %wu bytes into a region of size %wu")) | |
2876 | : G_("%<%.*s%> directive writing up to %wu bytes " | |
2877 | "into a region of size %wu"), (int) dir.len, | |
2878 | target_to_host (hostdir, sizeof hostdir, dir.beg), | |
2879 | res.max, navail); | |
5d93da1d | 2880 | |
8657c838 | 2881 | if (res.min == 0 && maxbytes <= res.max) |
346114b5 JJ |
2882 | /* This is a special case to avoid issuing the potentially |
2883 | confusing warning: | |
2884 | writing 0 or more bytes into a region of size 0. */ | |
2885 | return fmtwarn (dirloc, argloc, NULL, info.warnopt (), | |
2886 | info.bounded | |
2887 | ? (maybe | |
2888 | ? G_("%<%.*s%> directive output may be truncated " | |
2889 | "writing likely %wu or more bytes into a " | |
2890 | "region of size %wu") | |
2891 | : G_("%<%.*s%> directive output truncated writing " | |
2892 | "likely %wu or more bytes into a region of " | |
2893 | "size %wu")) | |
2894 | : G_("%<%.*s%> directive writing likely %wu or more " | |
2895 | "bytes into a region of size %wu"), (int) dir.len, | |
2896 | target_to_host (hostdir, sizeof hostdir, dir.beg), | |
2897 | res.likely, navail); | |
5d93da1d MS |
2898 | |
2899 | if (res.max < maxbytes) | |
346114b5 JJ |
2900 | return fmtwarn (dirloc, argloc, NULL, info.warnopt (), |
2901 | info.bounded | |
2902 | ? (maybe | |
2903 | ? G_("%<%.*s%> directive output may be truncated " | |
2904 | "writing between %wu and %wu bytes into a " | |
2905 | "region of size %wu") | |
2906 | : G_("%<%.*s%> directive output truncated " | |
2907 | "writing between %wu and %wu bytes into a " | |
2908 | "region of size %wu")) | |
2909 | : G_("%<%.*s%> directive writing between %wu and " | |
2910 | "%wu bytes into a region of size %wu"), | |
2911 | (int) dir.len, | |
2912 | target_to_host (hostdir, sizeof hostdir, dir.beg), | |
2913 | res.min, res.max, navail); | |
2914 | ||
2915 | return fmtwarn (dirloc, argloc, NULL, info.warnopt (), | |
2916 | info.bounded | |
2917 | ? (maybe | |
2918 | ? G_("%<%.*s%> directive output may be truncated " | |
2919 | "writing %wu or more bytes into a region of " | |
2920 | "size %wu") | |
2921 | : G_("%<%.*s%> directive output truncated writing " | |
2922 | "%wu or more bytes into a region of size %wu")) | |
2923 | : G_("%<%.*s%> directive writing %wu or more bytes " | |
2924 | "into a region of size %wu"), (int) dir.len, | |
d127ae9f | 2925 | target_to_host (hostdir, sizeof hostdir, dir.beg), |
5d93da1d | 2926 | res.min, navail); |
02f1ef40 MS |
2927 | } |
2928 | ||
5d93da1d MS |
2929 | /* The size of the destination region is a range. */ |
2930 | ||
d127ae9f | 2931 | if (target_to_host (*dir.beg) != '%') |
5d93da1d MS |
2932 | { |
2933 | unsigned HOST_WIDE_INT navail = avail_range.max; | |
2934 | ||
2935 | /* For plain character directives (i.e., the format string itself) | |
2936 | but not others, point the caret at the first character that's | |
2937 | past the end of the destination. */ | |
36367d1f JJ |
2938 | if (navail < dir.len) |
2939 | dirloc.set_caret_index (dirloc.get_caret_idx () + navail); | |
5d93da1d MS |
2940 | } |
2941 | ||
2942 | if (*dir.beg == '\0') | |
2943 | { | |
2944 | gcc_assert (res.min == 1 && res.min == res.max); | |
2945 | ||
346114b5 JJ |
2946 | return fmtwarn (dirloc, UNKNOWN_LOCATION, NULL, info.warnopt (), |
2947 | info.bounded | |
2948 | ? (maybe | |
2949 | ? G_("%qE output may be truncated before the last " | |
2950 | "format character") | |
2951 | : G_("%qE output truncated before the last format " | |
2952 | "character")) | |
2953 | : (maybe | |
2954 | ? G_("%qE may write a terminating nul past the end " | |
2955 | "of the destination") | |
2956 | : G_("%qE writing a terminating nul past the end " | |
2957 | "of the destination")), info.func); | |
5d93da1d MS |
2958 | } |
2959 | ||
2960 | if (res.min == res.max) | |
2961 | { | |
346114b5 JJ |
2962 | const char *d = target_to_host (hostdir, sizeof hostdir, dir.beg); |
2963 | if (!info.bounded) | |
2964 | return fmtwarn_n (dirloc, argloc, NULL, info.warnopt (), res.min, | |
2965 | "%<%.*s%> directive writing %wu byte into a region " | |
2966 | "of size between %wu and %wu", | |
2967 | "%<%.*s%> directive writing %wu bytes into a region " | |
2968 | "of size between %wu and %wu", (int) dir.len, d, | |
2969 | res.min, avail_range.min, avail_range.max); | |
2970 | else if (maybe) | |
2971 | return fmtwarn_n (dirloc, argloc, NULL, info.warnopt (), res.min, | |
2972 | "%<%.*s%> directive output may be truncated writing " | |
2973 | "%wu byte into a region of size between %wu and %wu", | |
2974 | "%<%.*s%> directive output may be truncated writing " | |
2975 | "%wu bytes into a region of size between %wu and " | |
2976 | "%wu", (int) dir.len, d, res.min, avail_range.min, | |
2977 | avail_range.max); | |
2978 | else | |
2979 | return fmtwarn_n (dirloc, argloc, NULL, info.warnopt (), res.min, | |
2980 | "%<%.*s%> directive output truncated writing %wu " | |
2981 | "byte into a region of size between %wu and %wu", | |
2982 | "%<%.*s%> directive output truncated writing %wu " | |
2983 | "bytes into a region of size between %wu and %wu", | |
2984 | (int) dir.len, d, res.min, avail_range.min, | |
2985 | avail_range.max); | |
5d93da1d MS |
2986 | } |
2987 | ||
8657c838 | 2988 | if (res.min == 0 && res.max < maxbytes) |
346114b5 JJ |
2989 | return fmtwarn (dirloc, argloc, NULL, info.warnopt (), |
2990 | info.bounded | |
2991 | ? (maybe | |
2992 | ? G_("%<%.*s%> directive output may be truncated " | |
2993 | "writing up to %wu bytes into a region of size " | |
2994 | "between %wu and %wu") | |
2995 | : G_("%<%.*s%> directive output truncated writing " | |
2996 | "up to %wu bytes into a region of size between " | |
2997 | "%wu and %wu")) | |
2998 | : G_("%<%.*s%> directive writing up to %wu bytes " | |
2999 | "into a region of size between %wu and %wu"), | |
3000 | (int) dir.len, | |
3001 | target_to_host (hostdir, sizeof hostdir, dir.beg), | |
3002 | res.max, avail_range.min, avail_range.max); | |
5d93da1d | 3003 | |
8657c838 | 3004 | if (res.min == 0 && maxbytes <= res.max) |
346114b5 JJ |
3005 | /* This is a special case to avoid issuing the potentially confusing |
3006 | warning: | |
3007 | writing 0 or more bytes into a region of size between 0 and N. */ | |
3008 | return fmtwarn (dirloc, argloc, NULL, info.warnopt (), | |
3009 | info.bounded | |
3010 | ? (maybe | |
3011 | ? G_("%<%.*s%> directive output may be truncated " | |
3012 | "writing likely %wu or more bytes into a region " | |
3013 | "of size between %wu and %wu") | |
3014 | : G_("%<%.*s%> directive output truncated writing " | |
3015 | "likely %wu or more bytes into a region of size " | |
3016 | "between %wu and %wu")) | |
3017 | : G_("%<%.*s%> directive writing likely %wu or more bytes " | |
3018 | "into a region of size between %wu and %wu"), | |
3019 | (int) dir.len, | |
3020 | target_to_host (hostdir, sizeof hostdir, dir.beg), | |
3021 | res.likely, avail_range.min, avail_range.max); | |
5d93da1d MS |
3022 | |
3023 | if (res.max < maxbytes) | |
346114b5 JJ |
3024 | return fmtwarn (dirloc, argloc, NULL, info.warnopt (), |
3025 | info.bounded | |
3026 | ? (maybe | |
3027 | ? G_("%<%.*s%> directive output may be truncated " | |
3028 | "writing between %wu and %wu bytes into a region " | |
3029 | "of size between %wu and %wu") | |
3030 | : G_("%<%.*s%> directive output truncated writing " | |
3031 | "between %wu and %wu bytes into a region of size " | |
3032 | "between %wu and %wu")) | |
3033 | : G_("%<%.*s%> directive writing between %wu and " | |
3034 | "%wu bytes into a region of size between %wu and " | |
3035 | "%wu"), (int) dir.len, | |
3036 | target_to_host (hostdir, sizeof hostdir, dir.beg), | |
3037 | res.min, res.max, avail_range.min, avail_range.max); | |
3038 | ||
3039 | return fmtwarn (dirloc, argloc, NULL, info.warnopt (), | |
3040 | info.bounded | |
3041 | ? (maybe | |
3042 | ? G_("%<%.*s%> directive output may be truncated writing " | |
3043 | "%wu or more bytes into a region of size between " | |
3044 | "%wu and %wu") | |
3045 | : G_("%<%.*s%> directive output truncated writing " | |
3046 | "%wu or more bytes into a region of size between " | |
3047 | "%wu and %wu")) | |
3048 | : G_("%<%.*s%> directive writing %wu or more bytes " | |
3049 | "into a region of size between %wu and %wu"), | |
3050 | (int) dir.len, | |
d127ae9f MS |
3051 | target_to_host (hostdir, sizeof hostdir, dir.beg), |
3052 | res.min, avail_range.min, avail_range.max); | |
02f1ef40 MS |
3053 | } |
3054 | ||
937a86b4 | 3055 | /* Given the formatting result described by RES and NAVAIL, the number |
d2ae6d5c JW |
3056 | of available bytes in the destination, return the range of bytes |
3057 | remaining in the destination. */ | |
937a86b4 MS |
3058 | |
3059 | static inline result_range | |
3060 | bytes_remaining (unsigned HOST_WIDE_INT navail, const format_result &res) | |
3061 | { | |
3062 | result_range range; | |
3063 | ||
3064 | if (HOST_WIDE_INT_MAX <= navail) | |
3065 | { | |
3066 | range.min = range.max = range.likely = range.unlikely = navail; | |
3067 | return range; | |
3068 | } | |
3069 | ||
3070 | /* The lower bound of the available range is the available size | |
3071 | minus the maximum output size, and the upper bound is the size | |
3072 | minus the minimum. */ | |
3073 | range.max = res.range.min < navail ? navail - res.range.min : 0; | |
3074 | ||
3075 | range.likely = res.range.likely < navail ? navail - res.range.likely : 0; | |
3076 | ||
3077 | if (res.range.max < HOST_WIDE_INT_MAX) | |
3078 | range.min = res.range.max < navail ? navail - res.range.max : 0; | |
3079 | else | |
3080 | range.min = range.likely; | |
3081 | ||
3082 | range.unlikely = (res.range.unlikely < navail | |
3083 | ? navail - res.range.unlikely : 0); | |
3084 | ||
3085 | return range; | |
3086 | } | |
3087 | ||
31c87a43 MS |
3088 | /* Compute the length of the output resulting from the directive DIR |
3089 | in a call described by INFO and update the overall result of the call | |
3090 | in *RES. Return true if the directive has been handled. */ | |
88d0c3f0 | 3091 | |
954b452a | 3092 | static bool |
22fca489 | 3093 | format_directive (const call_info &info, |
bbe2e114 | 3094 | format_result *res, const directive &dir, |
3c9f762a | 3095 | pointer_query &ptr_qry) |
88d0c3f0 MS |
3096 | { |
3097 | /* Offset of the beginning of the directive from the beginning | |
3098 | of the format string. */ | |
31c87a43 MS |
3099 | size_t offset = dir.beg - info.fmtstr; |
3100 | size_t start = offset; | |
3101 | size_t length = offset + dir.len - !!dir.len; | |
88d0c3f0 MS |
3102 | |
3103 | /* Create a location for the whole directive from the % to the format | |
3104 | specifier. */ | |
3105 | substring_loc dirloc (info.fmtloc, TREE_TYPE (info.format), | |
31c87a43 | 3106 | offset, start, length); |
88d0c3f0 | 3107 | |
89b6abbb | 3108 | /* Also get the location of the argument if possible. |
88d0c3f0 | 3109 | This doesn't work for integer literals or function calls. */ |
89b6abbb DM |
3110 | location_t argloc = UNKNOWN_LOCATION; |
3111 | if (dir.arg) | |
3112 | argloc = EXPR_LOCATION (dir.arg); | |
88d0c3f0 MS |
3113 | |
3114 | /* Bail when there is no function to compute the output length, | |
3115 | or when minimum length checking has been disabled. */ | |
5d93da1d | 3116 | if (!dir.fmtfunc || res->range.min >= HOST_WIDE_INT_MAX) |
954b452a | 3117 | return false; |
88d0c3f0 | 3118 | |
31c87a43 | 3119 | /* Compute the range of lengths of the formatted output. */ |
3c9f762a | 3120 | fmtresult fmtres = dir.fmtfunc (dir, dir.arg, ptr_qry); |
88d0c3f0 | 3121 | |
1eb4547b MS |
3122 | /* Record whether the output of all directives is known to be |
3123 | bounded by some maximum, implying that their arguments are | |
3124 | either known exactly or determined to be in a known range | |
3125 | or, for strings, limited by the upper bounds of the arrays | |
3126 | they refer to. */ | |
3127 | res->knownrange &= fmtres.knownrange; | |
88d0c3f0 | 3128 | |
1eb4547b | 3129 | if (!fmtres.knownrange) |
88d0c3f0 | 3130 | { |
1eb4547b | 3131 | /* Only when the range is known, check it against the host value |
cfce1a4a MS |
3132 | of INT_MAX + (the number of bytes of the "%.*Lf" directive with |
3133 | INT_MAX precision, which is the longest possible output of any | |
3134 | single directive). That's the largest valid byte count (though | |
3135 | not valid call to a printf-like function because it can never | |
3136 | return such a count). Otherwise, the range doesn't correspond | |
3137 | to known values of the argument. */ | |
3138 | if (fmtres.range.max > target_dir_max ()) | |
1eb4547b MS |
3139 | { |
3140 | /* Normalize the MAX counter to avoid having to deal with it | |
3141 | later. The counter can be less than HOST_WIDE_INT_M1U | |
3142 | when compiling for an ILP32 target on an LP64 host. */ | |
3143 | fmtres.range.max = HOST_WIDE_INT_M1U; | |
3144 | /* Disable exact and maximum length checking after a failure | |
3145 | to determine the maximum number of characters (for example | |
3146 | for wide characters or wide character strings) but continue | |
3147 | tracking the minimum number of characters. */ | |
5d93da1d | 3148 | res->range.max = HOST_WIDE_INT_M1U; |
1eb4547b MS |
3149 | } |
3150 | ||
cfce1a4a | 3151 | if (fmtres.range.min > target_dir_max ()) |
1eb4547b MS |
3152 | { |
3153 | /* Disable exact length checking after a failure to determine | |
3154 | even the minimum number of characters (it shouldn't happen | |
3155 | except in an error) but keep tracking the minimum and maximum | |
3156 | number of characters. */ | |
954b452a | 3157 | return true; |
1eb4547b | 3158 | } |
88d0c3f0 MS |
3159 | } |
3160 | ||
d127ae9f MS |
3161 | /* Buffer for the directive in the host character set (used when |
3162 | the source character set is different). */ | |
3163 | char hostdir[32]; | |
3164 | ||
5d93da1d MS |
3165 | int dirlen = dir.len; |
3166 | ||
2c8c83ac MS |
3167 | if (fmtres.nullp) |
3168 | { | |
89b6abbb | 3169 | fmtwarn (dirloc, argloc, NULL, info.warnopt (), |
6d3bab5d MS |
3170 | "%<%.*s%> directive argument is null", |
3171 | dirlen, target_to_host (hostdir, sizeof hostdir, dir.beg)); | |
2c8c83ac MS |
3172 | |
3173 | /* Don't bother processing the rest of the format string. */ | |
3174 | res->warned = true; | |
5d93da1d MS |
3175 | res->range.min = HOST_WIDE_INT_M1U; |
3176 | res->range.max = HOST_WIDE_INT_M1U; | |
954b452a | 3177 | return false; |
2c8c83ac MS |
3178 | } |
3179 | ||
88d0c3f0 MS |
3180 | /* Compute the number of available bytes in the destination. There |
3181 | must always be at least one byte of space for the terminating | |
3182 | NUL that's appended after the format string has been processed. */ | |
5d93da1d | 3183 | result_range avail_range = bytes_remaining (info.objsize, *res); |
88d0c3f0 | 3184 | |
937a86b4 MS |
3185 | /* If the argument aliases a part of the destination of the formatted |
3186 | call at offset FMTRES.DST_OFFSET append the directive and its result | |
3187 | to the set of aliases for later processing. */ | |
3188 | if (fmtres.dst_offset != HOST_WIDE_INT_MIN) | |
3189 | res->append_alias (dir, fmtres.dst_offset, fmtres.range); | |
3190 | ||
02f1ef40 MS |
3191 | bool warned = res->warned; |
3192 | ||
3193 | if (!warned) | |
89b6abbb | 3194 | warned = maybe_warn (dirloc, argloc, info, avail_range, |
02f1ef40 MS |
3195 | fmtres.range, dir); |
3196 | ||
5d93da1d MS |
3197 | /* Bump up the total maximum if it isn't too big. */ |
3198 | if (res->range.max < HOST_WIDE_INT_MAX | |
3199 | && fmtres.range.max < HOST_WIDE_INT_MAX) | |
3200 | res->range.max += fmtres.range.max; | |
88d0c3f0 | 3201 | |
5d93da1d | 3202 | /* Raise the total unlikely maximum by the larger of the maximum |
bf00c9e0 MS |
3203 | and the unlikely maximum. */ |
3204 | unsigned HOST_WIDE_INT save = res->range.unlikely; | |
5d93da1d MS |
3205 | if (fmtres.range.max < fmtres.range.unlikely) |
3206 | res->range.unlikely += fmtres.range.unlikely; | |
88d0c3f0 | 3207 | else |
5d93da1d MS |
3208 | res->range.unlikely += fmtres.range.max; |
3209 | ||
bf00c9e0 MS |
3210 | if (res->range.unlikely < save) |
3211 | res->range.unlikely = HOST_WIDE_INT_M1U; | |
3212 | ||
5d93da1d MS |
3213 | res->range.min += fmtres.range.min; |
3214 | res->range.likely += fmtres.range.likely; | |
88d0c3f0 MS |
3215 | |
3216 | /* Has the minimum directive output length exceeded the maximum | |
3217 | of 4095 bytes required to be supported? */ | |
3218 | bool minunder4k = fmtres.range.min < 4096; | |
5d93da1d | 3219 | bool maxunder4k = fmtres.range.max < 4096; |
bbcbd744 MS |
3220 | /* Clear POSUNDER4K in the overall result if the maximum has exceeded |
3221 | the 4k (this is necessary to avoid the return value optimization | |
5d93da1d MS |
3222 | that may not be safe in the maximum case). */ |
3223 | if (!maxunder4k) | |
bbcbd744 MS |
3224 | res->posunder4k = false; |
3225 | /* Also clear POSUNDER4K if the directive may fail. */ | |
3226 | if (fmtres.mayfail) | |
3227 | res->posunder4k = false; | |
88d0c3f0 | 3228 | |
5d93da1d MS |
3229 | if (!warned |
3230 | /* Only warn at level 2. */ | |
01512446 | 3231 | && warn_level > 1 |
eb319c50 MS |
3232 | /* Only warn for string functions. */ |
3233 | && info.is_string_func () | |
5d93da1d MS |
3234 | && (!minunder4k |
3235 | || (!maxunder4k && fmtres.range.max < HOST_WIDE_INT_MAX))) | |
88d0c3f0 MS |
3236 | { |
3237 | /* The directive output may be longer than the maximum required | |
3238 | to be handled by an implementation according to 7.21.6.1, p15 | |
3239 | of C11. Warn on this only at level 2 but remember this and | |
3240 | prevent folding the return value when done. This allows for | |
3241 | the possibility of the actual libc call failing due to ENOMEM | |
eb319c50 MS |
3242 | (like Glibc does with very large precision or width). |
3243 | Issue the "may exceed" warning only for string functions and | |
3244 | not for fprintf or printf. */ | |
88d0c3f0 MS |
3245 | |
3246 | if (fmtres.range.min == fmtres.range.max) | |
346114b5 | 3247 | warned = fmtwarn (dirloc, argloc, NULL, info.warnopt (), |
573aa7d4 | 3248 | "%<%.*s%> directive output of %wu bytes exceeds " |
346114b5 | 3249 | "minimum required size of 4095", dirlen, |
d127ae9f MS |
3250 | target_to_host (hostdir, sizeof hostdir, dir.beg), |
3251 | fmtres.range.min); | |
eb319c50 MS |
3252 | else if (!minunder4k) |
3253 | warned = fmtwarn (dirloc, argloc, NULL, info.warnopt (), | |
3254 | "%<%.*s%> directive output between %wu and %wu " | |
3255 | "bytes exceeds minimum required size of 4095", | |
3256 | dirlen, | |
3257 | target_to_host (hostdir, sizeof hostdir, dir.beg), | |
3258 | fmtres.range.min, fmtres.range.max); | |
3259 | else if (!info.retval_used () && info.is_string_func ()) | |
346114b5 | 3260 | warned = fmtwarn (dirloc, argloc, NULL, info.warnopt (), |
eb319c50 MS |
3261 | "%<%.*s%> directive output between %wu and %wu " |
3262 | "bytes may exceed minimum required size of " | |
3263 | "4095", | |
346114b5 JJ |
3264 | dirlen, |
3265 | target_to_host (hostdir, sizeof hostdir, dir.beg), | |
3266 | fmtres.range.min, fmtres.range.max); | |
88d0c3f0 MS |
3267 | } |
3268 | ||
5d93da1d MS |
3269 | /* Has the likely and maximum directive output exceeded INT_MAX? */ |
3270 | bool likelyximax = *dir.beg && res->range.likely > target_int_max (); | |
bf00c9e0 MS |
3271 | /* Don't consider the maximum to be in excess when it's the result |
3272 | of a string of unknown length (i.e., whose maximum has been set | |
3273 | to be greater than or equal to HOST_WIDE_INT_MAX. */ | |
3274 | bool maxximax = (*dir.beg | |
3275 | && res->range.max > target_int_max () | |
3276 | && res->range.max < HOST_WIDE_INT_MAX); | |
88d0c3f0 | 3277 | |
573aa7d4 | 3278 | if (!warned |
5d93da1d MS |
3279 | /* Warn for the likely output size at level 1. */ |
3280 | && (likelyximax | |
3281 | /* But only warn for the maximum at level 2. */ | |
01512446 | 3282 | || (warn_level > 1 |
5d93da1d MS |
3283 | && maxximax |
3284 | && fmtres.range.max < HOST_WIDE_INT_MAX))) | |
88d0c3f0 | 3285 | { |
eb319c50 MS |
3286 | if (fmtres.range.min > target_int_max ()) |
3287 | { | |
3288 | /* The directive output exceeds INT_MAX bytes. */ | |
3289 | if (fmtres.range.min == fmtres.range.max) | |
3290 | warned = fmtwarn (dirloc, argloc, NULL, info.warnopt (), | |
3291 | "%<%.*s%> directive output of %wu bytes exceeds " | |
3292 | "%<INT_MAX%>", dirlen, | |
3293 | target_to_host (hostdir, sizeof hostdir, dir.beg), | |
3294 | fmtres.range.min); | |
3295 | else | |
3296 | warned = fmtwarn (dirloc, argloc, NULL, info.warnopt (), | |
3297 | "%<%.*s%> directive output between %wu and " | |
3298 | "%wu bytes exceeds %<INT_MAX%>", dirlen, | |
3299 | target_to_host (hostdir, sizeof hostdir, dir.beg), | |
3300 | fmtres.range.min, fmtres.range.max); | |
3301 | } | |
3302 | else if (res->range.min > target_int_max ()) | |
3303 | { | |
3304 | /* The directive output is under INT_MAX but causes the result | |
3305 | to exceed INT_MAX bytes. */ | |
3306 | if (fmtres.range.min == fmtres.range.max) | |
3307 | warned = fmtwarn (dirloc, argloc, NULL, info.warnopt (), | |
3308 | "%<%.*s%> directive output of %wu bytes causes " | |
3309 | "result to exceed %<INT_MAX%>", dirlen, | |
3310 | target_to_host (hostdir, sizeof hostdir, dir.beg), | |
3311 | fmtres.range.min); | |
3312 | else | |
3313 | warned = fmtwarn (dirloc, argloc, NULL, info.warnopt (), | |
3314 | "%<%.*s%> directive output between %wu and " | |
3315 | "%wu bytes causes result to exceed %<INT_MAX%>", | |
3316 | dirlen, | |
3317 | target_to_host (hostdir, sizeof hostdir, dir.beg), | |
3318 | fmtres.range.min, fmtres.range.max); | |
3319 | } | |
3320 | else if ((!info.retval_used () || !info.bounded) | |
3321 | && (info.is_string_func ())) | |
3322 | /* Warn for calls to string functions that either aren't bounded | |
3323 | (sprintf) or whose return value isn't used. */ | |
346114b5 | 3324 | warned = fmtwarn (dirloc, argloc, NULL, info.warnopt (), |
eb319c50 MS |
3325 | "%<%.*s%> directive output between %wu and " |
3326 | "%wu bytes may cause result to exceed " | |
3327 | "%<INT_MAX%>", dirlen, | |
346114b5 JJ |
3328 | target_to_host (hostdir, sizeof hostdir, dir.beg), |
3329 | fmtres.range.min, fmtres.range.max); | |
88d0c3f0 | 3330 | } |
573aa7d4 | 3331 | |
a41cc59d MS |
3332 | if (!warned && fmtres.nonstr) |
3333 | { | |
3334 | warned = fmtwarn (dirloc, argloc, NULL, info.warnopt (), | |
3335 | "%<%.*s%> directive argument is not a nul-terminated " | |
3336 | "string", | |
3337 | dirlen, | |
3338 | target_to_host (hostdir, sizeof hostdir, dir.beg)); | |
3339 | if (warned && DECL_P (fmtres.nonstr)) | |
3340 | inform (DECL_SOURCE_LOCATION (fmtres.nonstr), | |
3341 | "referenced argument declared here"); | |
3342 | return false; | |
3343 | } | |
3344 | ||
5d93da1d MS |
3345 | if (warned && fmtres.range.min < fmtres.range.likely |
3346 | && fmtres.range.likely < fmtres.range.max) | |
1c89478a | 3347 | inform_n (info.fmtloc, fmtres.range.likely, |
281cbf31 JJ |
3348 | "assuming directive output of %wu byte", |
3349 | "assuming directive output of %wu bytes", | |
5d93da1d | 3350 | fmtres.range.likely); |
5d93da1d | 3351 | |
573aa7d4 MS |
3352 | if (warned && fmtres.argmin) |
3353 | { | |
3354 | if (fmtres.argmin == fmtres.argmax) | |
3355 | inform (info.fmtloc, "directive argument %qE", fmtres.argmin); | |
3356 | else if (fmtres.knownrange) | |
3357 | inform (info.fmtloc, "directive argument in the range [%E, %E]", | |
3358 | fmtres.argmin, fmtres.argmax); | |
3359 | else | |
3360 | inform (info.fmtloc, | |
3361 | "using the range [%E, %E] for directive argument", | |
3362 | fmtres.argmin, fmtres.argmax); | |
3363 | } | |
3364 | ||
3365 | res->warned |= warned; | |
02f1ef40 | 3366 | |
eb319c50 | 3367 | if (!dir.beg[0] && res->warned) |
88d0c3f0 | 3368 | { |
88d0c3f0 MS |
3369 | location_t callloc = gimple_location (info.callstmt); |
3370 | ||
5d93da1d MS |
3371 | unsigned HOST_WIDE_INT min = res->range.min; |
3372 | unsigned HOST_WIDE_INT max = res->range.max; | |
88d0c3f0 | 3373 | |
eb319c50 MS |
3374 | if (info.objsize < HOST_WIDE_INT_MAX) |
3375 | { | |
3376 | /* If a warning has been issued for buffer overflow or truncation | |
3377 | help the user figure out how big a buffer they need. */ | |
3378 | ||
3379 | if (min == max) | |
ff146a59 JJ |
3380 | inform_n (callloc, min, |
3381 | "%qE output %wu byte into a destination of size %wu", | |
3382 | "%qE output %wu bytes into a destination of size %wu", | |
3383 | info.func, min, info.objsize); | |
eb319c50 MS |
3384 | else if (max < HOST_WIDE_INT_MAX) |
3385 | inform (callloc, | |
3386 | "%qE output between %wu and %wu bytes into " | |
3387 | "a destination of size %wu", | |
3388 | info.func, min, max, info.objsize); | |
3389 | else if (min < res->range.likely && res->range.likely < max) | |
3390 | inform (callloc, | |
3391 | "%qE output %wu or more bytes (assuming %wu) into " | |
3392 | "a destination of size %wu", | |
3393 | info.func, min, res->range.likely, info.objsize); | |
3394 | else | |
3395 | inform (callloc, | |
3396 | "%qE output %wu or more bytes into a destination of size " | |
3397 | "%wu", | |
3398 | info.func, min, info.objsize); | |
3399 | } | |
3400 | else if (!info.is_string_func ()) | |
3401 | { | |
700d4cb0 | 3402 | /* If the warning is for a file function like fprintf |
eb319c50 MS |
3403 | of printf with no destination size just print the computed |
3404 | result. */ | |
3405 | if (min == max) | |
ff146a59 JJ |
3406 | inform_n (callloc, min, |
3407 | "%qE output %wu byte", "%qE output %wu bytes", | |
3408 | info.func, min); | |
eb319c50 MS |
3409 | else if (max < HOST_WIDE_INT_MAX) |
3410 | inform (callloc, | |
3411 | "%qE output between %wu and %wu bytes", | |
3412 | info.func, min, max); | |
3413 | else if (min < res->range.likely && res->range.likely < max) | |
3414 | inform (callloc, | |
3415 | "%qE output %wu or more bytes (assuming %wu)", | |
3416 | info.func, min, res->range.likely); | |
3417 | else | |
3418 | inform (callloc, | |
3419 | "%qE output %wu or more bytes", | |
3420 | info.func, min); | |
3421 | } | |
88d0c3f0 MS |
3422 | } |
3423 | ||
5d93da1d | 3424 | if (dump_file && *dir.beg) |
88d0c3f0 | 3425 | { |
a6fd2cd7 MS |
3426 | fprintf (dump_file, |
3427 | " Result: " | |
3428 | HOST_WIDE_INT_PRINT_DEC ", " HOST_WIDE_INT_PRINT_DEC ", " | |
3429 | HOST_WIDE_INT_PRINT_DEC ", " HOST_WIDE_INT_PRINT_DEC " (" | |
3430 | HOST_WIDE_INT_PRINT_DEC ", " HOST_WIDE_INT_PRINT_DEC ", " | |
3431 | HOST_WIDE_INT_PRINT_DEC ", " HOST_WIDE_INT_PRINT_DEC ")\n", | |
3432 | fmtres.range.min, fmtres.range.likely, | |
3433 | fmtres.range.max, fmtres.range.unlikely, | |
3434 | res->range.min, res->range.likely, | |
3435 | res->range.max, res->range.unlikely); | |
88d0c3f0 | 3436 | } |
5d93da1d MS |
3437 | |
3438 | return true; | |
88d0c3f0 MS |
3439 | } |
3440 | ||
954b452a MS |
3441 | /* Parse a format directive in function call described by INFO starting |
3442 | at STR and populate DIR structure. Bump up *ARGNO by the number of | |
3443 | arguments extracted for the directive. Return the length of | |
3444 | the directive. */ | |
88d0c3f0 | 3445 | |
954b452a | 3446 | static size_t |
22fca489 | 3447 | parse_directive (call_info &info, |
954b452a | 3448 | directive &dir, format_result *res, |
bbe2e114 | 3449 | const char *str, unsigned *argno, |
f5299992 | 3450 | range_query *query) |
88d0c3f0 | 3451 | { |
d127ae9f | 3452 | const char *pcnt = strchr (str, target_percent); |
954b452a | 3453 | dir.beg = str; |
88d0c3f0 | 3454 | |
954b452a MS |
3455 | if (size_t len = pcnt ? pcnt - str : *str ? strlen (str) : 1) |
3456 | { | |
3457 | /* This directive is either a plain string or the terminating nul | |
3458 | (which isn't really a directive but it simplifies things to | |
3459 | handle it as if it were). */ | |
3460 | dir.len = len; | |
5d93da1d | 3461 | dir.fmtfunc = format_plain; |
88d0c3f0 | 3462 | |
954b452a MS |
3463 | if (dump_file) |
3464 | { | |
a6fd2cd7 MS |
3465 | fprintf (dump_file, " Directive %u at offset " |
3466 | HOST_WIDE_INT_PRINT_UNSIGNED ": \"%.*s\", " | |
3467 | "length = " HOST_WIDE_INT_PRINT_UNSIGNED "\n", | |
9976a810 | 3468 | dir.dirno, |
a6fd2cd7 | 3469 | (unsigned HOST_WIDE_INT)(size_t)(dir.beg - info.fmtstr), |
a5a4a9df | 3470 | (int)dir.len, dir.beg, (unsigned HOST_WIDE_INT) dir.len); |
954b452a MS |
3471 | } |
3472 | ||
3473 | return len - !*str; | |
3474 | } | |
88d0c3f0 | 3475 | |
937a86b4 MS |
3476 | /* Set the directive argument's number to correspond to its position |
3477 | in the formatted function call's argument list. */ | |
3478 | dir.argno = *argno; | |
3479 | ||
954b452a MS |
3480 | const char *pf = pcnt + 1; |
3481 | ||
3482 | /* POSIX numbered argument index or zero when none. */ | |
d127ae9f | 3483 | HOST_WIDE_INT dollar = 0; |
954b452a MS |
3484 | |
3485 | /* With and precision. -1 when not specified, HOST_WIDE_INT_MIN | |
3486 | when given by a va_list argument, and a non-negative value | |
3487 | when specified in the format string itself. */ | |
3488 | HOST_WIDE_INT width = -1; | |
3489 | HOST_WIDE_INT precision = -1; | |
88d0c3f0 | 3490 | |
d127ae9f MS |
3491 | /* Pointers to the beginning of the width and precision decimal |
3492 | string (if any) within the directive. */ | |
3493 | const char *pwidth = 0; | |
3494 | const char *pprec = 0; | |
3495 | ||
3496 | /* When the value of the decimal string that specifies width or | |
3497 | precision is out of range, points to the digit that causes | |
3498 | the value to exceed the limit. */ | |
3499 | const char *werange = NULL; | |
3500 | const char *perange = NULL; | |
3501 | ||
954b452a MS |
3502 | /* Width specified via the asterisk. Need not be INTEGER_CST. |
3503 | For vararg functions set to void_node. */ | |
3504 | tree star_width = NULL_TREE; | |
3505 | ||
3506 | /* Width specified via the asterisk. Need not be INTEGER_CST. | |
3507 | For vararg functions set to void_node. */ | |
3508 | tree star_precision = NULL_TREE; | |
3509 | ||
d127ae9f | 3510 | if (ISDIGIT (target_to_host (*pf))) |
88d0c3f0 | 3511 | { |
954b452a MS |
3512 | /* This could be either a POSIX positional argument, the '0' |
3513 | flag, or a width, depending on what follows. Store it as | |
3514 | width and sort it out later after the next character has | |
3515 | been seen. */ | |
d127ae9f | 3516 | pwidth = pf; |
837865bd | 3517 | width = target_strtowi (&pf, &werange); |
954b452a | 3518 | } |
d127ae9f | 3519 | else if (target_to_host (*pf) == '*') |
954b452a MS |
3520 | { |
3521 | /* Similarly to the block above, this could be either a POSIX | |
3522 | positional argument or a width, depending on what follows. */ | |
3523 | if (*argno < gimple_call_num_args (info.callstmt)) | |
3524 | star_width = gimple_call_arg (info.callstmt, (*argno)++); | |
3525 | else | |
3526 | star_width = void_node; | |
3527 | ++pf; | |
3528 | } | |
88d0c3f0 | 3529 | |
d127ae9f | 3530 | if (target_to_host (*pf) == '$') |
954b452a MS |
3531 | { |
3532 | /* Handle the POSIX dollar sign which references the 1-based | |
3533 | positional argument number. */ | |
3534 | if (width != -1) | |
3535 | dollar = width + info.argidx; | |
3536 | else if (star_width | |
e602bbfc MS |
3537 | && TREE_CODE (star_width) == INTEGER_CST |
3538 | && (TYPE_PRECISION (TREE_TYPE (star_width)) | |
3539 | <= TYPE_PRECISION (integer_type_node))) | |
954b452a | 3540 | dollar = width + tree_to_shwi (star_width); |
88d0c3f0 | 3541 | |
954b452a MS |
3542 | /* Bail when the numbered argument is out of range (it will |
3543 | have already been diagnosed by -Wformat). */ | |
3544 | if (dollar == 0 | |
d127ae9f | 3545 | || dollar == (int)info.argidx |
954b452a MS |
3546 | || dollar > gimple_call_num_args (info.callstmt)) |
3547 | return false; | |
3548 | ||
3549 | --dollar; | |
88d0c3f0 | 3550 | |
954b452a MS |
3551 | star_width = NULL_TREE; |
3552 | width = -1; | |
3553 | ++pf; | |
3554 | } | |
88d0c3f0 | 3555 | |
954b452a MS |
3556 | if (dollar || !star_width) |
3557 | { | |
3558 | if (width != -1) | |
88d0c3f0 | 3559 | { |
954b452a MS |
3560 | if (width == 0) |
3561 | { | |
3562 | /* The '0' that has been interpreted as a width above is | |
3563 | actually a flag. Reset HAVE_WIDTH, set the '0' flag, | |
3564 | and continue processing other flags. */ | |
3565 | width = -1; | |
3566 | dir.set_flag ('0'); | |
3567 | } | |
3568 | else if (!dollar) | |
3569 | { | |
3570 | /* (Non-zero) width has been seen. The next character | |
3571 | is either a period or a digit. */ | |
3572 | goto start_precision; | |
3573 | } | |
88d0c3f0 | 3574 | } |
954b452a MS |
3575 | /* When either '$' has been seen, or width has not been seen, |
3576 | the next field is the optional flags followed by an optional | |
3577 | width. */ | |
3578 | for ( ; ; ) { | |
d127ae9f | 3579 | switch (target_to_host (*pf)) |
954b452a MS |
3580 | { |
3581 | case ' ': | |
3582 | case '0': | |
3583 | case '+': | |
3584 | case '-': | |
3585 | case '#': | |
d127ae9f | 3586 | dir.set_flag (target_to_host (*pf++)); |
954b452a MS |
3587 | break; |
3588 | ||
3589 | default: | |
3590 | goto start_width; | |
3591 | } | |
3592 | } | |
88d0c3f0 | 3593 | |
954b452a | 3594 | start_width: |
d127ae9f | 3595 | if (ISDIGIT (target_to_host (*pf))) |
88d0c3f0 | 3596 | { |
d127ae9f MS |
3597 | werange = 0; |
3598 | pwidth = pf; | |
837865bd | 3599 | width = target_strtowi (&pf, &werange); |
88d0c3f0 | 3600 | } |
d127ae9f | 3601 | else if (target_to_host (*pf) == '*') |
88d0c3f0 | 3602 | { |
954b452a MS |
3603 | if (*argno < gimple_call_num_args (info.callstmt)) |
3604 | star_width = gimple_call_arg (info.callstmt, (*argno)++); | |
5b4f088d | 3605 | else |
954b452a MS |
3606 | { |
3607 | /* This is (likely) a va_list. It could also be an invalid | |
3608 | call with insufficient arguments. */ | |
3609 | star_width = void_node; | |
3610 | } | |
88d0c3f0 MS |
3611 | ++pf; |
3612 | } | |
d127ae9f | 3613 | else if (target_to_host (*pf) == '\'') |
88d0c3f0 | 3614 | { |
954b452a MS |
3615 | /* The POSIX apostrophe indicating a numeric grouping |
3616 | in the current locale. Even though it's possible to | |
3617 | estimate the upper bound on the size of the output | |
3618 | based on the number of digits it probably isn't worth | |
3619 | continuing. */ | |
3620 | return 0; | |
3621 | } | |
3622 | } | |
88d0c3f0 | 3623 | |
954b452a | 3624 | start_precision: |
d127ae9f | 3625 | if (target_to_host (*pf) == '.') |
954b452a MS |
3626 | { |
3627 | ++pf; | |
88d0c3f0 | 3628 | |
d127ae9f | 3629 | if (ISDIGIT (target_to_host (*pf))) |
954b452a | 3630 | { |
d127ae9f | 3631 | pprec = pf; |
837865bd | 3632 | precision = target_strtowi (&pf, &perange); |
88d0c3f0 | 3633 | } |
d127ae9f | 3634 | else if (target_to_host (*pf) == '*') |
88d0c3f0 | 3635 | { |
954b452a MS |
3636 | if (*argno < gimple_call_num_args (info.callstmt)) |
3637 | star_precision = gimple_call_arg (info.callstmt, (*argno)++); | |
3638 | else | |
88d0c3f0 | 3639 | { |
954b452a MS |
3640 | /* This is (likely) a va_list. It could also be an invalid |
3641 | call with insufficient arguments. */ | |
3642 | star_precision = void_node; | |
88d0c3f0 | 3643 | } |
954b452a | 3644 | ++pf; |
88d0c3f0 | 3645 | } |
954b452a MS |
3646 | else |
3647 | { | |
3648 | /* The decimal precision or the asterisk are optional. | |
d2ae6d5c | 3649 | When neither is specified it's taken to be zero. */ |
954b452a MS |
3650 | precision = 0; |
3651 | } | |
3652 | } | |
88d0c3f0 | 3653 | |
d127ae9f | 3654 | switch (target_to_host (*pf)) |
954b452a MS |
3655 | { |
3656 | case 'h': | |
d127ae9f | 3657 | if (target_to_host (pf[1]) == 'h') |
88d0c3f0 MS |
3658 | { |
3659 | ++pf; | |
954b452a | 3660 | dir.modifier = FMT_LEN_hh; |
88d0c3f0 | 3661 | } |
954b452a MS |
3662 | else |
3663 | dir.modifier = FMT_LEN_h; | |
3664 | ++pf; | |
3665 | break; | |
3666 | ||
3667 | case 'j': | |
3668 | dir.modifier = FMT_LEN_j; | |
3669 | ++pf; | |
3670 | break; | |
88d0c3f0 | 3671 | |
954b452a MS |
3672 | case 'L': |
3673 | dir.modifier = FMT_LEN_L; | |
3674 | ++pf; | |
3675 | break; | |
3676 | ||
3677 | case 'l': | |
d127ae9f | 3678 | if (target_to_host (pf[1]) == 'l') |
88d0c3f0 | 3679 | { |
88d0c3f0 | 3680 | ++pf; |
954b452a MS |
3681 | dir.modifier = FMT_LEN_ll; |
3682 | } | |
3683 | else | |
3684 | dir.modifier = FMT_LEN_l; | |
3685 | ++pf; | |
3686 | break; | |
88d0c3f0 | 3687 | |
954b452a MS |
3688 | case 't': |
3689 | dir.modifier = FMT_LEN_t; | |
3690 | ++pf; | |
3691 | break; | |
88d0c3f0 | 3692 | |
954b452a MS |
3693 | case 'z': |
3694 | dir.modifier = FMT_LEN_z; | |
3695 | ++pf; | |
3696 | break; | |
3697 | } | |
88d0c3f0 | 3698 | |
d127ae9f | 3699 | switch (target_to_host (*pf)) |
954b452a MS |
3700 | { |
3701 | /* Handle a sole '%' character the same as "%%" but since it's | |
3702 | undefined prevent the result from being folded. */ | |
3703 | case '\0': | |
3704 | --pf; | |
5d93da1d | 3705 | res->range.min = res->range.max = HOST_WIDE_INT_M1U; |
954b452a MS |
3706 | /* FALLTHRU */ |
3707 | case '%': | |
3708 | dir.fmtfunc = format_percent; | |
3709 | break; | |
88d0c3f0 | 3710 | |
954b452a MS |
3711 | case 'a': |
3712 | case 'A': | |
3713 | case 'e': | |
3714 | case 'E': | |
3715 | case 'f': | |
3716 | case 'F': | |
3717 | case 'g': | |
3718 | case 'G': | |
3719 | res->floating = true; | |
3720 | dir.fmtfunc = format_floating; | |
3721 | break; | |
88d0c3f0 | 3722 | |
954b452a MS |
3723 | case 'd': |
3724 | case 'i': | |
3725 | case 'o': | |
3726 | case 'u': | |
3727 | case 'x': | |
3728 | case 'X': | |
3729 | dir.fmtfunc = format_integer; | |
3730 | break; | |
3731 | ||
aa353bd9 FD |
3732 | case 'b': |
3733 | case 'B': | |
3734 | dir.fmtfunc = format_integer; | |
3735 | break; | |
3736 | ||
954b452a MS |
3737 | case 'p': |
3738 | /* The %p output is implementation-defined. It's possible | |
d2ae6d5c | 3739 | to determine this format but due to extensions (especially |
954b452a MS |
3740 | those of the Linux kernel -- see bug 78512) the first %p |
3741 | in the format string disables any further processing. */ | |
3742 | return false; | |
3743 | ||
3744 | case 'n': | |
3745 | /* %n has side-effects even when nothing is actually printed to | |
3746 | any buffer. */ | |
3747 | info.nowrite = false; | |
3748 | dir.fmtfunc = format_none; | |
3749 | break; | |
3750 | ||
bbcbd744 | 3751 | case 'C': |
954b452a | 3752 | case 'c': |
bbcbd744 | 3753 | /* POSIX wide character and C/POSIX narrow character. */ |
954b452a MS |
3754 | dir.fmtfunc = format_character; |
3755 | break; | |
3756 | ||
3757 | case 'S': | |
3758 | case 's': | |
bbcbd744 | 3759 | /* POSIX wide string and C/POSIX narrow character string. */ |
954b452a MS |
3760 | dir.fmtfunc = format_string; |
3761 | break; | |
3762 | ||
3763 | default: | |
3764 | /* Unknown conversion specification. */ | |
3765 | return 0; | |
3766 | } | |
3767 | ||
d127ae9f MS |
3768 | dir.specifier = target_to_host (*pf++); |
3769 | ||
3770 | /* Store the length of the format directive. */ | |
3771 | dir.len = pf - pcnt; | |
3772 | ||
3773 | /* Buffer for the directive in the host character set (used when | |
3774 | the source character set is different). */ | |
3775 | char hostdir[32]; | |
954b452a MS |
3776 | |
3777 | if (star_width) | |
3778 | { | |
3812f460 | 3779 | if (INTEGRAL_TYPE_P (TREE_TYPE (star_width))) |
f5299992 | 3780 | dir.set_width (star_width, query); |
954b452a MS |
3781 | else |
3782 | { | |
3783 | /* Width specified by a va_list takes on the range [0, -INT_MIN] | |
3784 | (width is the absolute value of that specified). */ | |
31c87a43 MS |
3785 | dir.width[0] = 0; |
3786 | dir.width[1] = target_int_max () + 1; | |
88d0c3f0 | 3787 | } |
954b452a MS |
3788 | } |
3789 | else | |
d127ae9f | 3790 | { |
837865bd | 3791 | if (width == HOST_WIDE_INT_MAX && werange) |
d127ae9f MS |
3792 | { |
3793 | size_t begin = dir.beg - info.fmtstr + (pwidth - pcnt); | |
3794 | size_t caret = begin + (werange - pcnt); | |
3795 | size_t end = pf - info.fmtstr - 1; | |
3796 | ||
3797 | /* Create a location for the width part of the directive, | |
3798 | pointing the caret at the first out-of-range digit. */ | |
3799 | substring_loc dirloc (info.fmtloc, TREE_TYPE (info.format), | |
3800 | caret, begin, end); | |
3801 | ||
346114b5 JJ |
3802 | fmtwarn (dirloc, UNKNOWN_LOCATION, NULL, info.warnopt (), |
3803 | "%<%.*s%> directive width out of range", (int) dir.len, | |
3804 | target_to_host (hostdir, sizeof hostdir, dir.beg)); | |
d127ae9f MS |
3805 | } |
3806 | ||
3807 | dir.set_width (width); | |
3808 | } | |
88d0c3f0 | 3809 | |
954b452a MS |
3810 | if (star_precision) |
3811 | { | |
3812f460 | 3812 | if (INTEGRAL_TYPE_P (TREE_TYPE (star_precision))) |
f5299992 | 3813 | dir.set_precision (star_precision, query); |
954b452a | 3814 | else |
88d0c3f0 | 3815 | { |
954b452a MS |
3816 | /* Precision specified by a va_list takes on the range [-1, INT_MAX] |
3817 | (unlike width, negative precision is ignored). */ | |
31c87a43 MS |
3818 | dir.prec[0] = -1; |
3819 | dir.prec[1] = target_int_max (); | |
954b452a MS |
3820 | } |
3821 | } | |
3822 | else | |
d127ae9f | 3823 | { |
837865bd | 3824 | if (precision == HOST_WIDE_INT_MAX && perange) |
d127ae9f MS |
3825 | { |
3826 | size_t begin = dir.beg - info.fmtstr + (pprec - pcnt) - 1; | |
3827 | size_t caret = dir.beg - info.fmtstr + (perange - pcnt) - 1; | |
3828 | size_t end = pf - info.fmtstr - 2; | |
3829 | ||
3830 | /* Create a location for the precision part of the directive, | |
3831 | including the leading period, pointing the caret at the first | |
3832 | out-of-range digit . */ | |
3833 | substring_loc dirloc (info.fmtloc, TREE_TYPE (info.format), | |
3834 | caret, begin, end); | |
3835 | ||
346114b5 JJ |
3836 | fmtwarn (dirloc, UNKNOWN_LOCATION, NULL, info.warnopt (), |
3837 | "%<%.*s%> directive precision out of range", (int) dir.len, | |
3838 | target_to_host (hostdir, sizeof hostdir, dir.beg)); | |
d127ae9f MS |
3839 | } |
3840 | ||
3841 | dir.set_precision (precision); | |
3842 | } | |
88d0c3f0 | 3843 | |
954b452a MS |
3844 | /* Extract the argument if the directive takes one and if it's |
3845 | available (e.g., the function doesn't take a va_list). Treat | |
3846 | missing arguments the same as va_list, even though they will | |
3847 | have likely already been diagnosed by -Wformat. */ | |
3848 | if (dir.specifier != '%' | |
3849 | && *argno < gimple_call_num_args (info.callstmt)) | |
3850 | dir.arg = gimple_call_arg (info.callstmt, dollar ? dollar : (*argno)++); | |
88d0c3f0 | 3851 | |
954b452a MS |
3852 | if (dump_file) |
3853 | { | |
a6fd2cd7 MS |
3854 | fprintf (dump_file, |
3855 | " Directive %u at offset " HOST_WIDE_INT_PRINT_UNSIGNED | |
3856 | ": \"%.*s\"", | |
3857 | dir.dirno, | |
3858 | (unsigned HOST_WIDE_INT)(size_t)(dir.beg - info.fmtstr), | |
954b452a MS |
3859 | (int)dir.len, dir.beg); |
3860 | if (star_width) | |
31c87a43 MS |
3861 | { |
3862 | if (dir.width[0] == dir.width[1]) | |
a6fd2cd7 MS |
3863 | fprintf (dump_file, ", width = " HOST_WIDE_INT_PRINT_DEC, |
3864 | dir.width[0]); | |
31c87a43 | 3865 | else |
a6fd2cd7 MS |
3866 | fprintf (dump_file, |
3867 | ", width in range [" HOST_WIDE_INT_PRINT_DEC | |
3868 | ", " HOST_WIDE_INT_PRINT_DEC "]", | |
3869 | dir.width[0], dir.width[1]); | |
31c87a43 | 3870 | } |
88d0c3f0 | 3871 | |
954b452a | 3872 | if (star_precision) |
31c87a43 MS |
3873 | { |
3874 | if (dir.prec[0] == dir.prec[1]) | |
a6fd2cd7 MS |
3875 | fprintf (dump_file, ", precision = " HOST_WIDE_INT_PRINT_DEC, |
3876 | dir.prec[0]); | |
31c87a43 | 3877 | else |
a6fd2cd7 MS |
3878 | fprintf (dump_file, |
3879 | ", precision in range [" HOST_WIDE_INT_PRINT_DEC | |
3880 | HOST_WIDE_INT_PRINT_DEC "]", | |
3881 | dir.prec[0], dir.prec[1]); | |
31c87a43 | 3882 | } |
954b452a MS |
3883 | fputc ('\n', dump_file); |
3884 | } | |
3885 | ||
3886 | return dir.len; | |
3887 | } | |
3888 | ||
937a86b4 MS |
3889 | /* Diagnose overlap between destination and %s directive arguments. */ |
3890 | ||
3891 | static void | |
3892 | maybe_warn_overlap (call_info &info, format_result *res) | |
3893 | { | |
3894 | /* Two vectors of 1-based indices corresponding to either certainly | |
3895 | or possibly aliasing arguments. */ | |
3896 | auto_vec<int, 16> aliasarg[2]; | |
3897 | ||
3898 | /* Go through the array of potentially aliasing directives and collect | |
3899 | argument numbers of those that do or may overlap the destination | |
3900 | object given the full result. */ | |
3901 | for (unsigned i = 0; i != res->alias_count; ++i) | |
3902 | { | |
3903 | const format_result::alias_info &alias = res->aliases[i]; | |
3904 | ||
3905 | enum { possible = -1, none = 0, certain = 1 } overlap = none; | |
3906 | ||
3907 | /* If the precision is zero there is no overlap. (This only | |
3908 | considers %s directives and ignores %n.) */ | |
3909 | if (alias.dir.prec[0] == 0 && alias.dir.prec[1] == 0) | |
3910 | continue; | |
3911 | ||
3912 | if (alias.offset == HOST_WIDE_INT_MAX | |
3913 | || info.dst_offset == HOST_WIDE_INT_MAX) | |
3914 | overlap = possible; | |
3915 | else if (alias.offset == info.dst_offset) | |
3916 | overlap = alias.dir.prec[0] == 0 ? possible : certain; | |
3917 | else | |
3918 | { | |
3919 | /* Determine overlap from the range of output and offsets | |
3920 | into the same destination as the source, and rule out | |
3921 | impossible overlap. */ | |
3922 | unsigned HOST_WIDE_INT albeg = alias.offset; | |
3923 | unsigned HOST_WIDE_INT dstbeg = info.dst_offset; | |
3924 | ||
3925 | unsigned HOST_WIDE_INT alend = albeg + alias.range.min; | |
3926 | unsigned HOST_WIDE_INT dstend = dstbeg + res->range.min - 1; | |
3927 | ||
3928 | if ((albeg <= dstbeg && alend > dstbeg) | |
3929 | || (albeg >= dstbeg && albeg < dstend)) | |
3930 | overlap = certain; | |
3931 | else | |
3932 | { | |
3933 | alend = albeg + alias.range.max; | |
3934 | if (alend < albeg) | |
3935 | alend = HOST_WIDE_INT_M1U; | |
3936 | ||
3937 | dstend = dstbeg + res->range.max - 1; | |
3938 | if (dstend < dstbeg) | |
3939 | dstend = HOST_WIDE_INT_M1U; | |
3940 | ||
3941 | if ((albeg >= dstbeg && albeg <= dstend) | |
3942 | || (alend >= dstbeg && alend <= dstend)) | |
3943 | overlap = possible; | |
3944 | } | |
3945 | } | |
3946 | ||
3947 | if (overlap == none) | |
3948 | continue; | |
3949 | ||
3950 | /* Append the 1-based argument number. */ | |
3951 | aliasarg[overlap != certain].safe_push (alias.dir.argno + 1); | |
3952 | ||
3953 | /* Disable any kind of optimization. */ | |
3954 | res->range.unlikely = HOST_WIDE_INT_M1U; | |
3955 | } | |
3956 | ||
3957 | tree arg0 = gimple_call_arg (info.callstmt, 0); | |
3958 | location_t loc = gimple_location (info.callstmt); | |
3959 | ||
3960 | bool aliaswarn = false; | |
3961 | ||
3962 | unsigned ncertain = aliasarg[0].length (); | |
3963 | unsigned npossible = aliasarg[1].length (); | |
3964 | if (ncertain && npossible) | |
3965 | { | |
3966 | /* If there are multiple arguments that overlap, some certainly | |
3967 | and some possibly, handle both sets in a single diagnostic. */ | |
3968 | aliaswarn | |
3969 | = warning_at (loc, OPT_Wrestrict, | |
3970 | "%qE arguments %Z and maybe %Z overlap destination " | |
3971 | "object %qE", | |
3972 | info.func, aliasarg[0].address (), ncertain, | |
3973 | aliasarg[1].address (), npossible, | |
3974 | info.dst_origin); | |
3975 | } | |
3976 | else if (ncertain) | |
3977 | { | |
3978 | /* There is only one set of two or more arguments and they all | |
3979 | certainly overlap the destination. */ | |
3980 | aliaswarn | |
3981 | = warning_n (loc, OPT_Wrestrict, ncertain, | |
3982 | "%qE argument %Z overlaps destination object %qE", | |
3983 | "%qE arguments %Z overlap destination object %qE", | |
3984 | info.func, aliasarg[0].address (), ncertain, | |
3985 | info.dst_origin); | |
3986 | } | |
3987 | else if (npossible) | |
3988 | { | |
3989 | /* There is only one set of two or more arguments and they all | |
3990 | may overlap (but need not). */ | |
3991 | aliaswarn | |
3992 | = warning_n (loc, OPT_Wrestrict, npossible, | |
3993 | "%qE argument %Z may overlap destination object %qE", | |
3994 | "%qE arguments %Z may overlap destination object %qE", | |
3995 | info.func, aliasarg[1].address (), npossible, | |
3996 | info.dst_origin); | |
3997 | } | |
3998 | ||
3999 | if (aliaswarn) | |
4000 | { | |
4001 | res->warned = true; | |
4002 | ||
4003 | if (info.dst_origin != arg0) | |
4004 | { | |
4005 | /* If its location is different from the first argument of the call | |
4006 | point either at the destination object itself or at the expression | |
4007 | that was used to determine the overlap. */ | |
4008 | loc = (DECL_P (info.dst_origin) | |
4009 | ? DECL_SOURCE_LOCATION (info.dst_origin) | |
4010 | : EXPR_LOCATION (info.dst_origin)); | |
4011 | if (loc != UNKNOWN_LOCATION) | |
4012 | inform (loc, | |
4013 | "destination object referenced by %<restrict%>-qualified " | |
4014 | "argument 1 was declared here"); | |
4015 | } | |
4016 | } | |
4017 | } | |
4018 | ||
954b452a MS |
4019 | /* Compute the length of the output resulting from the call to a formatted |
4020 | output function described by INFO and store the result of the call in | |
4021 | *RES. Issue warnings for detected past the end writes. Return true | |
4022 | if the complete format string has been processed and *RES can be relied | |
4023 | on, false otherwise (e.g., when a unknown or unhandled directive was seen | |
4024 | that caused the processing to be terminated early). */ | |
4025 | ||
22fca489 | 4026 | static bool |
3c9f762a MS |
4027 | compute_format_length (call_info &info, format_result *res, |
4028 | pointer_query &ptr_qry) | |
954b452a | 4029 | { |
02f1ef40 MS |
4030 | if (dump_file) |
4031 | { | |
4032 | location_t callloc = gimple_location (info.callstmt); | |
4033 | fprintf (dump_file, "%s:%i: ", | |
4034 | LOCATION_FILE (callloc), LOCATION_LINE (callloc)); | |
4035 | print_generic_expr (dump_file, info.func, dump_flags); | |
4036 | ||
a6fd2cd7 MS |
4037 | fprintf (dump_file, |
4038 | ": objsize = " HOST_WIDE_INT_PRINT_UNSIGNED | |
4039 | ", fmtstr = \"%s\"\n", | |
4040 | info.objsize, info.fmtstr); | |
02f1ef40 MS |
4041 | } |
4042 | ||
31c87a43 | 4043 | /* Reset the minimum and maximum byte counters. */ |
5d93da1d | 4044 | res->range.min = res->range.max = 0; |
954b452a MS |
4045 | |
4046 | /* No directive has been seen yet so the length of output is bounded | |
bbcbd744 MS |
4047 | by the known range [0, 0] (with no conversion resulting in a failure |
4048 | or producing more than 4K bytes) until determined otherwise. */ | |
954b452a | 4049 | res->knownrange = true; |
954b452a MS |
4050 | res->floating = false; |
4051 | res->warned = false; | |
4052 | ||
4053 | /* 1-based directive counter. */ | |
4054 | unsigned dirno = 1; | |
4055 | ||
4056 | /* The variadic argument counter. */ | |
4057 | unsigned argno = info.argidx; | |
4058 | ||
937a86b4 MS |
4059 | bool success = true; |
4060 | ||
954b452a MS |
4061 | for (const char *pf = info.fmtstr; ; ++dirno) |
4062 | { | |
937a86b4 | 4063 | directive dir (&info, dirno); |
88d0c3f0 | 4064 | |
3c9f762a | 4065 | size_t n = parse_directive (info, dir, res, pf, &argno, ptr_qry.rvals); |
88d0c3f0 | 4066 | |
5d93da1d | 4067 | /* Return failure if the format function fails. */ |
3c9f762a | 4068 | if (!format_directive (info, res, dir, ptr_qry)) |
5d93da1d | 4069 | return false; |
88d0c3f0 | 4070 | |
937a86b4 MS |
4071 | /* Return success when the directive is zero bytes long and it's |
4072 | the last thing in the format string (i.e., it's the terminating | |
954b452a MS |
4073 | nul, which isn't really a directive but handling it as one makes |
4074 | things simpler). */ | |
4075 | if (!n) | |
937a86b4 MS |
4076 | { |
4077 | success = *pf == '\0'; | |
4078 | break; | |
4079 | } | |
88d0c3f0 | 4080 | |
954b452a | 4081 | pf += n; |
88d0c3f0 | 4082 | } |
01a4551c | 4083 | |
937a86b4 MS |
4084 | maybe_warn_overlap (info, res); |
4085 | ||
5d93da1d | 4086 | /* The complete format string was processed (with or without warnings). */ |
937a86b4 | 4087 | return success; |
88d0c3f0 MS |
4088 | } |
4089 | ||
9a27acc3 MS |
4090 | /* Return the size of the object referenced by the expression DEST in |
4091 | statement STMT, if available, or the maximum possible size otherwise. */ | |
88d0c3f0 MS |
4092 | |
4093 | static unsigned HOST_WIDE_INT | |
9a27acc3 | 4094 | get_destination_size (tree dest, gimple *stmt, pointer_query &ptr_qry) |
88d0c3f0 | 4095 | { |
eb319c50 | 4096 | /* When there is no destination return the maximum. */ |
91e3ec29 | 4097 | if (!dest) |
eb319c50 | 4098 | return HOST_WIDE_INT_MAX; |
91e3ec29 | 4099 | |
d02c41dd MS |
4100 | /* Use compute_objsize to determine the size of the destination object. */ |
4101 | access_ref aref; | |
9a27acc3 | 4102 | if (!ptr_qry.get_ref (dest, stmt, &aref)) |
d02c41dd | 4103 | return HOST_WIDE_INT_MAX; |
02f1ef40 | 4104 | |
d02c41dd MS |
4105 | offset_int remsize = aref.size_remaining (); |
4106 | if (!wi::fits_uhwi_p (remsize)) | |
4107 | return HOST_WIDE_INT_MAX; | |
88d0c3f0 | 4108 | |
d02c41dd | 4109 | return remsize.to_uhwi (); |
88d0c3f0 MS |
4110 | } |
4111 | ||
a104bd88 MS |
4112 | /* Return true if the call described by INFO with result RES safe to |
4113 | optimize (i.e., no undefined behavior), and set RETVAL to the range | |
4114 | of its return values. */ | |
88d0c3f0 | 4115 | |
3eefa646 | 4116 | static bool |
22fca489 | 4117 | is_call_safe (const call_info &info, |
a104bd88 MS |
4118 | const format_result &res, bool under4k, |
4119 | unsigned HOST_WIDE_INT retval[2]) | |
88d0c3f0 | 4120 | { |
bbcbd744 | 4121 | if (under4k && !res.posunder4k) |
a104bd88 | 4122 | return false; |
a151e93b | 4123 | |
5d93da1d | 4124 | /* The minimum return value. */ |
a104bd88 | 4125 | retval[0] = res.range.min; |
a151e93b | 4126 | |
5d93da1d MS |
4127 | /* The maximum return value is in most cases bounded by RES.RANGE.MAX |
4128 | but in cases involving multibyte characters could be as large as | |
4129 | RES.RANGE.UNLIKELY. */ | |
a104bd88 | 4130 | retval[1] |
5d93da1d | 4131 | = res.range.unlikely < res.range.max ? res.range.max : res.range.unlikely; |
a151e93b MS |
4132 | |
4133 | /* Adjust the number of bytes which includes the terminating nul | |
4134 | to reflect the return value of the function which does not. | |
4135 | Because the valid range of the function is [INT_MIN, INT_MAX], | |
4136 | a valid range before the adjustment below is [0, INT_MAX + 1] | |
4137 | (the functions only return negative values on error or undefined | |
4138 | behavior). */ | |
a104bd88 MS |
4139 | if (retval[0] <= target_int_max () + 1) |
4140 | --retval[0]; | |
4141 | if (retval[1] <= target_int_max () + 1) | |
4142 | --retval[1]; | |
a151e93b | 4143 | |
88d0c3f0 MS |
4144 | /* Avoid the return value optimization when the behavior of the call |
4145 | is undefined either because any directive may have produced 4K or | |
4146 | more of output, or the return value exceeds INT_MAX, or because | |
4147 | the output overflows the destination object (but leave it enabled | |
4148 | when the function is bounded because then the behavior is well- | |
4149 | defined). */ | |
a104bd88 MS |
4150 | if (retval[0] == retval[1] |
4151 | && (info.bounded || retval[0] < info.objsize) | |
4152 | && retval[0] <= target_int_max ()) | |
4153 | return true; | |
4154 | ||
4155 | if ((info.bounded || retval[1] < info.objsize) | |
4156 | && (retval[0] < target_int_max () | |
4157 | && retval[1] < target_int_max ())) | |
4158 | return true; | |
4159 | ||
4160 | if (!under4k && (info.bounded || retval[0] < info.objsize)) | |
4161 | return true; | |
4162 | ||
4163 | return false; | |
4164 | } | |
4165 | ||
4166 | /* Given a suitable result RES of a call to a formatted output function | |
4167 | described by INFO, substitute the result for the return value of | |
4168 | the call. The result is suitable if the number of bytes it represents | |
4169 | is known and exact. A result that isn't suitable for substitution may | |
4170 | have its range set to the range of return values, if that is known. | |
4171 | Return true if the call is removed and gsi_next should not be performed | |
4172 | in the caller. */ | |
4173 | ||
4174 | static bool | |
4175 | try_substitute_return_value (gimple_stmt_iterator *gsi, | |
22fca489 | 4176 | const call_info &info, |
a104bd88 MS |
4177 | const format_result &res) |
4178 | { | |
4179 | tree lhs = gimple_get_lhs (info.callstmt); | |
4180 | ||
4181 | /* Set to true when the entire call has been removed. */ | |
4182 | bool removed = false; | |
4183 | ||
4184 | /* The minimum and maximum return value. */ | |
1657178f | 4185 | unsigned HOST_WIDE_INT retval[2] = {0}; |
a104bd88 MS |
4186 | bool safe = is_call_safe (info, res, true, retval); |
4187 | ||
4188 | if (safe | |
4189 | && retval[0] == retval[1] | |
cd123354 JJ |
4190 | /* Not prepared to handle possibly throwing calls here; they shouldn't |
4191 | appear in non-artificial testcases, except when the __*_chk routines | |
4192 | are badly declared. */ | |
4193 | && !stmt_ends_bb_p (info.callstmt)) | |
88d0c3f0 | 4194 | { |
a73468e8 JJ |
4195 | tree cst = build_int_cst (lhs ? TREE_TYPE (lhs) : integer_type_node, |
4196 | retval[0]); | |
d7b0fcaa | 4197 | |
a73468e8 | 4198 | if (lhs == NULL_TREE && info.nowrite) |
a151e93b MS |
4199 | { |
4200 | /* Remove the call to the bounded function with a zero size | |
4201 | (e.g., snprintf(0, 0, "%i", 123)) if there is no lhs. */ | |
4202 | unlink_stmt_vdef (info.callstmt); | |
4203 | gsi_remove (gsi, true); | |
4204 | removed = true; | |
4205 | } | |
4206 | else if (info.nowrite) | |
d7b0fcaa MS |
4207 | { |
4208 | /* Replace the call to the bounded function with a zero size | |
4209 | (e.g., snprintf(0, 0, "%i", 123) with the constant result | |
a151e93b | 4210 | of the function. */ |
52a5515e | 4211 | gimplify_and_update_call_from_tree (gsi, cst); |
d7b0fcaa MS |
4212 | gimple *callstmt = gsi_stmt (*gsi); |
4213 | update_stmt (callstmt); | |
4214 | } | |
a151e93b | 4215 | else if (lhs) |
d7b0fcaa MS |
4216 | { |
4217 | /* Replace the left-hand side of the call with the constant | |
a151e93b | 4218 | result of the formatted function. */ |
d7b0fcaa MS |
4219 | gimple_call_set_lhs (info.callstmt, NULL_TREE); |
4220 | gimple *g = gimple_build_assign (lhs, cst); | |
4221 | gsi_insert_after (gsi, g, GSI_NEW_STMT); | |
4222 | update_stmt (info.callstmt); | |
4223 | } | |
88d0c3f0 MS |
4224 | |
4225 | if (dump_file) | |
4226 | { | |
a151e93b MS |
4227 | if (removed) |
4228 | fprintf (dump_file, " Removing call statement."); | |
4229 | else | |
4230 | { | |
4231 | fprintf (dump_file, " Substituting "); | |
4232 | print_generic_expr (dump_file, cst, dump_flags); | |
4233 | fprintf (dump_file, " for %s.\n", | |
4234 | info.nowrite ? "statement" : "return value"); | |
4235 | } | |
88d0c3f0 MS |
4236 | } |
4237 | } | |
a73468e8 | 4238 | else if (lhs && types_compatible_p (TREE_TYPE (lhs), integer_type_node)) |
3eefa646 | 4239 | { |
a151e93b | 4240 | bool setrange = false; |
88d0c3f0 | 4241 | |
a104bd88 MS |
4242 | if (safe |
4243 | && (info.bounded || retval[1] < info.objsize) | |
4244 | && (retval[0] < target_int_max () | |
4245 | && retval[1] < target_int_max ())) | |
88d0c3f0 MS |
4246 | { |
4247 | /* If the result is in a valid range bounded by the size of | |
4248 | the destination set it so that it can be used for subsequent | |
4249 | optimizations. */ | |
4250 | int prec = TYPE_PRECISION (integer_type_node); | |
4251 | ||
a104bd88 MS |
4252 | wide_int min = wi::shwi (retval[0], prec); |
4253 | wide_int max = wi::shwi (retval[1], prec); | |
6ccc4356 AH |
4254 | value_range r (TREE_TYPE (lhs), min, max); |
4255 | set_range_info (lhs, r); | |
a151e93b MS |
4256 | |
4257 | setrange = true; | |
88d0c3f0 MS |
4258 | } |
4259 | ||
4260 | if (dump_file) | |
4261 | { | |
4262 | const char *inbounds | |
a104bd88 MS |
4263 | = (retval[0] < info.objsize |
4264 | ? (retval[1] < info.objsize | |
88d0c3f0 MS |
4265 | ? "in" : "potentially out-of") |
4266 | : "out-of"); | |
4267 | ||
a151e93b | 4268 | const char *what = setrange ? "Setting" : "Discarding"; |
a104bd88 | 4269 | if (retval[0] != retval[1]) |
88d0c3f0 | 4270 | fprintf (dump_file, |
a6fd2cd7 MS |
4271 | " %s %s-bounds return value range [" |
4272 | HOST_WIDE_INT_PRINT_UNSIGNED ", " | |
4273 | HOST_WIDE_INT_PRINT_UNSIGNED "].\n", | |
4274 | what, inbounds, retval[0], retval[1]); | |
88d0c3f0 | 4275 | else |
a6fd2cd7 MS |
4276 | fprintf (dump_file, " %s %s-bounds return value " |
4277 | HOST_WIDE_INT_PRINT_UNSIGNED ".\n", | |
4278 | what, inbounds, retval[0]); | |
88d0c3f0 MS |
4279 | } |
4280 | } | |
3eefa646 | 4281 | |
02f1ef40 MS |
4282 | if (dump_file) |
4283 | fputc ('\n', dump_file); | |
4284 | ||
4285 | return removed; | |
88d0c3f0 MS |
4286 | } |
4287 | ||
a104bd88 MS |
4288 | /* Try to simplify a s{,n}printf call described by INFO with result |
4289 | RES by replacing it with a simpler and presumably more efficient | |
4290 | call (such as strcpy). */ | |
4291 | ||
4292 | static bool | |
4293 | try_simplify_call (gimple_stmt_iterator *gsi, | |
22fca489 | 4294 | const call_info &info, |
a104bd88 MS |
4295 | const format_result &res) |
4296 | { | |
4297 | unsigned HOST_WIDE_INT dummy[2]; | |
4298 | if (!is_call_safe (info, res, info.retval_used (), dummy)) | |
4299 | return false; | |
4300 | ||
4301 | switch (info.fncode) | |
4302 | { | |
4303 | case BUILT_IN_SNPRINTF: | |
4304 | return gimple_fold_builtin_snprintf (gsi); | |
4305 | ||
4306 | case BUILT_IN_SPRINTF: | |
4307 | return gimple_fold_builtin_sprintf (gsi); | |
4308 | ||
4309 | default: | |
4310 | ; | |
4311 | } | |
4312 | ||
4313 | return false; | |
4314 | } | |
4315 | ||
91e3ec29 MS |
4316 | /* Return the zero-based index of the format string argument of a printf |
4317 | like function and set *IDX_ARGS to the first format argument. When | |
4318 | no such index exists return UINT_MAX. */ | |
4319 | ||
4320 | static unsigned | |
4321 | get_user_idx_format (tree fndecl, unsigned *idx_args) | |
4322 | { | |
4323 | tree attrs = lookup_attribute ("format", DECL_ATTRIBUTES (fndecl)); | |
4324 | if (!attrs) | |
4325 | attrs = lookup_attribute ("format", TYPE_ATTRIBUTES (TREE_TYPE (fndecl))); | |
4326 | ||
4327 | if (!attrs) | |
4328 | return UINT_MAX; | |
4329 | ||
4330 | attrs = TREE_VALUE (attrs); | |
4331 | ||
4332 | tree archetype = TREE_VALUE (attrs); | |
4333 | if (strcmp ("printf", IDENTIFIER_POINTER (archetype))) | |
4334 | return UINT_MAX; | |
4335 | ||
4336 | attrs = TREE_CHAIN (attrs); | |
4337 | tree fmtarg = TREE_VALUE (attrs); | |
4338 | ||
4339 | attrs = TREE_CHAIN (attrs); | |
4340 | tree elliparg = TREE_VALUE (attrs); | |
4341 | ||
4342 | /* Attribute argument indices are 1-based but we use zero-based. */ | |
4343 | *idx_args = tree_to_uhwi (elliparg) - 1; | |
4344 | return tree_to_uhwi (fmtarg) - 1; | |
4345 | } | |
4346 | ||
22fca489 MS |
4347 | } /* Unnamed namespace. */ |
4348 | ||
4349 | /* Determine if a GIMPLE call at *GSI is to one of the sprintf-like built-in | |
4350 | functions and if so, handle it. Return true if the call is removed and | |
4351 | gsi_next should not be performed in the caller. */ | |
88d0c3f0 | 4352 | |
3eefa646 | 4353 | bool |
d02c41dd | 4354 | handle_printf_call (gimple_stmt_iterator *gsi, pointer_query &ptr_qry) |
88d0c3f0 | 4355 | { |
22fca489 MS |
4356 | init_target_to_host_charmap (); |
4357 | ||
88d0c3f0 MS |
4358 | call_info info = call_info (); |
4359 | ||
d7b0fcaa | 4360 | info.callstmt = gsi_stmt (*gsi); |
91e3ec29 MS |
4361 | info.func = gimple_call_fndecl (info.callstmt); |
4362 | if (!info.func) | |
3eefa646 | 4363 | return false; |
88d0c3f0 | 4364 | |
91e3ec29 MS |
4365 | /* Format string argument number (valid for all functions). */ |
4366 | unsigned idx_format = UINT_MAX; | |
d6f7829a JJ |
4367 | if (gimple_call_builtin_p (info.callstmt, BUILT_IN_NORMAL)) |
4368 | info.fncode = DECL_FUNCTION_CODE (info.func); | |
4369 | else | |
91e3ec29 MS |
4370 | { |
4371 | unsigned idx_args; | |
4372 | idx_format = get_user_idx_format (info.func, &idx_args); | |
d6f7829a JJ |
4373 | if (idx_format == UINT_MAX |
4374 | || idx_format >= gimple_call_num_args (info.callstmt) | |
4375 | || idx_args > gimple_call_num_args (info.callstmt) | |
4376 | || !POINTER_TYPE_P (TREE_TYPE (gimple_call_arg (info.callstmt, | |
4377 | idx_format)))) | |
91e3ec29 | 4378 | return false; |
d6f7829a | 4379 | info.fncode = BUILT_IN_NONE; |
91e3ec29 MS |
4380 | info.argidx = idx_args; |
4381 | } | |
4382 | ||
88d0c3f0 MS |
4383 | /* The size of the destination as in snprintf(dest, size, ...). */ |
4384 | unsigned HOST_WIDE_INT dstsize = HOST_WIDE_INT_M1U; | |
4385 | ||
4386 | /* The size of the destination determined by __builtin_object_size. */ | |
4387 | unsigned HOST_WIDE_INT objsize = HOST_WIDE_INT_M1U; | |
4388 | ||
91e3ec29 MS |
4389 | /* Zero-based buffer size argument number (snprintf and vsnprintf). */ |
4390 | unsigned idx_dstsize = UINT_MAX; | |
88d0c3f0 MS |
4391 | |
4392 | /* Object size argument number (snprintf_chk and vsnprintf_chk). */ | |
91e3ec29 | 4393 | unsigned idx_objsize = UINT_MAX; |
88d0c3f0 | 4394 | |
91e3ec29 MS |
4395 | /* Destinaton argument number (valid for sprintf functions only). */ |
4396 | unsigned idx_dstptr = 0; | |
88d0c3f0 MS |
4397 | |
4398 | switch (info.fncode) | |
4399 | { | |
91e3ec29 MS |
4400 | case BUILT_IN_NONE: |
4401 | // User-defined function with attribute format (printf). | |
4402 | idx_dstptr = -1; | |
4403 | break; | |
4404 | ||
4405 | case BUILT_IN_FPRINTF: | |
4406 | // Signature: | |
4407 | // __builtin_fprintf (FILE*, format, ...) | |
4408 | idx_format = 1; | |
4409 | info.argidx = 2; | |
4410 | idx_dstptr = -1; | |
4411 | break; | |
4412 | ||
4413 | case BUILT_IN_FPRINTF_CHK: | |
4414 | // Signature: | |
4415 | // __builtin_fprintf_chk (FILE*, ost, format, ...) | |
4416 | idx_format = 2; | |
4417 | info.argidx = 3; | |
4418 | idx_dstptr = -1; | |
4419 | break; | |
4420 | ||
4421 | case BUILT_IN_FPRINTF_UNLOCKED: | |
4422 | // Signature: | |
4423 | // __builtin_fprintf_unnlocked (FILE*, format, ...) | |
4424 | idx_format = 1; | |
4425 | info.argidx = 2; | |
4426 | idx_dstptr = -1; | |
4427 | break; | |
4428 | ||
4429 | case BUILT_IN_PRINTF: | |
4430 | // Signature: | |
4431 | // __builtin_printf (format, ...) | |
4432 | idx_format = 0; | |
4433 | info.argidx = 1; | |
4434 | idx_dstptr = -1; | |
4435 | break; | |
4436 | ||
4437 | case BUILT_IN_PRINTF_CHK: | |
4438 | // Signature: | |
eb319c50 | 4439 | // __builtin_printf_chk (ost, format, ...) |
91e3ec29 MS |
4440 | idx_format = 1; |
4441 | info.argidx = 2; | |
4442 | idx_dstptr = -1; | |
4443 | break; | |
4444 | ||
4445 | case BUILT_IN_PRINTF_UNLOCKED: | |
4446 | // Signature: | |
4447 | // __builtin_printf (format, ...) | |
4448 | idx_format = 0; | |
4449 | info.argidx = 1; | |
4450 | idx_dstptr = -1; | |
4451 | break; | |
4452 | ||
88d0c3f0 MS |
4453 | case BUILT_IN_SPRINTF: |
4454 | // Signature: | |
4455 | // __builtin_sprintf (dst, format, ...) | |
4456 | idx_format = 1; | |
4457 | info.argidx = 2; | |
4458 | break; | |
4459 | ||
5fe39ec0 JJ |
4460 | case BUILT_IN_SPRINTF_CHK: |
4461 | // Signature: | |
4462 | // __builtin___sprintf_chk (dst, ost, objsize, format, ...) | |
4463 | idx_objsize = 2; | |
4464 | idx_format = 3; | |
4465 | info.argidx = 4; | |
4466 | break; | |
4467 | ||
88d0c3f0 MS |
4468 | case BUILT_IN_SNPRINTF: |
4469 | // Signature: | |
4470 | // __builtin_snprintf (dst, size, format, ...) | |
4471 | idx_dstsize = 1; | |
4472 | idx_format = 2; | |
4473 | info.argidx = 3; | |
4474 | info.bounded = true; | |
4475 | break; | |
4476 | ||
4477 | case BUILT_IN_SNPRINTF_CHK: | |
4478 | // Signature: | |
5fe39ec0 | 4479 | // __builtin___snprintf_chk (dst, size, ost, objsize, format, ...) |
88d0c3f0 MS |
4480 | idx_dstsize = 1; |
4481 | idx_objsize = 3; | |
4482 | idx_format = 4; | |
4483 | info.argidx = 5; | |
4484 | info.bounded = true; | |
4485 | break; | |
4486 | ||
91e3ec29 MS |
4487 | case BUILT_IN_VFPRINTF: |
4488 | // Signature: | |
4489 | // __builtin_vprintf (FILE*, format, va_list) | |
4490 | idx_format = 1; | |
4491 | info.argidx = -1; | |
4492 | idx_dstptr = -1; | |
4493 | break; | |
4494 | ||
4495 | case BUILT_IN_VFPRINTF_CHK: | |
4496 | // Signature: | |
4497 | // __builtin___vfprintf_chk (FILE*, ost, format, va_list) | |
4498 | idx_format = 2; | |
4499 | info.argidx = -1; | |
4500 | idx_dstptr = -1; | |
4501 | break; | |
4502 | ||
4503 | case BUILT_IN_VPRINTF: | |
4504 | // Signature: | |
4505 | // __builtin_vprintf (format, va_list) | |
4506 | idx_format = 0; | |
4507 | info.argidx = -1; | |
4508 | idx_dstptr = -1; | |
4509 | break; | |
4510 | ||
4511 | case BUILT_IN_VPRINTF_CHK: | |
4512 | // Signature: | |
4513 | // __builtin___vprintf_chk (ost, format, va_list) | |
4514 | idx_format = 1; | |
4515 | info.argidx = -1; | |
4516 | idx_dstptr = -1; | |
4517 | break; | |
4518 | ||
88d0c3f0 MS |
4519 | case BUILT_IN_VSNPRINTF: |
4520 | // Signature: | |
4521 | // __builtin_vsprintf (dst, size, format, va) | |
4522 | idx_dstsize = 1; | |
4523 | idx_format = 2; | |
4524 | info.argidx = -1; | |
4525 | info.bounded = true; | |
4526 | break; | |
4527 | ||
4528 | case BUILT_IN_VSNPRINTF_CHK: | |
4529 | // Signature: | |
4530 | // __builtin___vsnprintf_chk (dst, size, ost, objsize, format, va) | |
4531 | idx_dstsize = 1; | |
2b1b5938 MS |
4532 | idx_objsize = 3; |
4533 | idx_format = 4; | |
88d0c3f0 MS |
4534 | info.argidx = -1; |
4535 | info.bounded = true; | |
4536 | break; | |
4537 | ||
4538 | case BUILT_IN_VSPRINTF: | |
4539 | // Signature: | |
4540 | // __builtin_vsprintf (dst, format, va) | |
4541 | idx_format = 1; | |
4542 | info.argidx = -1; | |
4543 | break; | |
4544 | ||
4545 | case BUILT_IN_VSPRINTF_CHK: | |
4546 | // Signature: | |
4547 | // __builtin___vsprintf_chk (dst, ost, objsize, format, va) | |
4548 | idx_format = 3; | |
4549 | idx_objsize = 2; | |
4550 | info.argidx = -1; | |
4551 | break; | |
4552 | ||
4553 | default: | |
3eefa646 | 4554 | return false; |
88d0c3f0 MS |
4555 | } |
4556 | ||
02f1ef40 MS |
4557 | /* Set the global warning level for this function. */ |
4558 | warn_level = info.bounded ? warn_format_trunc : warn_format_overflow; | |
4559 | ||
91e3ec29 MS |
4560 | /* For all string functions the first argument is a pointer to |
4561 | the destination. */ | |
4562 | tree dstptr = (idx_dstptr < gimple_call_num_args (info.callstmt) | |
4563 | ? gimple_call_arg (info.callstmt, 0) : NULL_TREE); | |
2c8c83ac | 4564 | |
88d0c3f0 MS |
4565 | info.format = gimple_call_arg (info.callstmt, idx_format); |
4566 | ||
ee75687b MS |
4567 | /* True when the destination size is constant as opposed to the lower |
4568 | or upper bound of a range. */ | |
4569 | bool dstsize_cst_p = true; | |
3e6837c2 | 4570 | bool posunder4k = true; |
ee75687b | 4571 | |
91e3ec29 | 4572 | if (idx_dstsize == UINT_MAX) |
88d0c3f0 | 4573 | { |
de6aa933 MS |
4574 | /* For non-bounded functions like sprintf, determine the size |
4575 | of the destination from the object or pointer passed to it | |
4576 | as the first argument. */ | |
9a27acc3 | 4577 | dstsize = get_destination_size (dstptr, info.callstmt, ptr_qry); |
88d0c3f0 MS |
4578 | } |
4579 | else if (tree size = gimple_call_arg (info.callstmt, idx_dstsize)) | |
4580 | { | |
4581 | /* For bounded functions try to get the size argument. */ | |
4582 | ||
4583 | if (TREE_CODE (size) == INTEGER_CST) | |
4584 | { | |
4585 | dstsize = tree_to_uhwi (size); | |
1eb4547b | 4586 | /* No object can be larger than SIZE_MAX bytes (half the address |
ee92e7ba | 4587 | space) on the target. |
de6aa933 MS |
4588 | The functions are defined only for output of at most INT_MAX |
4589 | bytes. Specifying a bound in excess of that limit effectively | |
4590 | defeats the bounds checking (and on some implementations such | |
4591 | as Solaris cause the function to fail with EINVAL). */ | |
ee92e7ba MS |
4592 | if (dstsize > target_size_max () / 2) |
4593 | { | |
4594 | /* Avoid warning if -Wstringop-overflow is specified since | |
4595 | it also warns for the same thing though only for the | |
4596 | checking built-ins. */ | |
91e3ec29 | 4597 | if ((idx_objsize == UINT_MAX |
ee92e7ba | 4598 | || !warn_stringop_overflow)) |
efcc8d38 | 4599 | warning_at (gimple_location (info.callstmt), info.warnopt (), |
ee92e7ba MS |
4600 | "specified bound %wu exceeds maximum object size " |
4601 | "%wu", | |
4602 | dstsize, target_size_max () / 2); | |
3e6837c2 MS |
4603 | /* POSIX requires snprintf to fail if DSTSIZE is greater |
4604 | than INT_MAX. Even though not all POSIX implementations | |
4605 | conform to the requirement, avoid folding in this case. */ | |
4606 | posunder4k = false; | |
ee92e7ba | 4607 | } |
de6aa933 | 4608 | else if (dstsize > target_int_max ()) |
3e6837c2 MS |
4609 | { |
4610 | warning_at (gimple_location (info.callstmt), info.warnopt (), | |
4611 | "specified bound %wu exceeds %<INT_MAX%>", | |
4612 | dstsize); | |
4613 | /* POSIX requires snprintf to fail if DSTSIZE is greater | |
4614 | than INT_MAX. Avoid folding in that case. */ | |
4615 | posunder4k = false; | |
4616 | } | |
88d0c3f0 MS |
4617 | } |
4618 | else if (TREE_CODE (size) == SSA_NAME) | |
4619 | { | |
4620 | /* Try to determine the range of values of the argument | |
ee75687b MS |
4621 | and use the greater of the two at level 1 and the smaller |
4622 | of them at level 2. */ | |
f5299992 | 4623 | value_range vr; |
d02c41dd | 4624 | ptr_qry.rvals->range_of_expr (vr, size, info.callstmt); |
22fca489 | 4625 | |
f5299992 | 4626 | if (!vr.undefined_p ()) |
3e6837c2 | 4627 | { |
c79d8335 | 4628 | tree type = TREE_TYPE (size); |
f5299992 AH |
4629 | tree tmin = wide_int_to_tree (type, vr.lower_bound ()); |
4630 | tree tmax = wide_int_to_tree (type, vr.upper_bound ()); | |
c79d8335 AH |
4631 | unsigned HOST_WIDE_INT minsize = TREE_INT_CST_LOW (tmin); |
4632 | unsigned HOST_WIDE_INT maxsize = TREE_INT_CST_LOW (tmax); | |
3e6837c2 MS |
4633 | dstsize = warn_level < 2 ? maxsize : minsize; |
4634 | ||
4635 | if (minsize > target_int_max ()) | |
4636 | warning_at (gimple_location (info.callstmt), info.warnopt (), | |
4637 | "specified bound range [%wu, %wu] exceeds " | |
4638 | "%<INT_MAX%>", | |
4639 | minsize, maxsize); | |
4640 | ||
4641 | /* POSIX requires snprintf to fail if DSTSIZE is greater | |
4642 | than INT_MAX. Avoid folding if that's possible. */ | |
4643 | if (maxsize > target_int_max ()) | |
4644 | posunder4k = false; | |
4645 | } | |
ee75687b MS |
4646 | |
4647 | /* The destination size is not constant. If the function is | |
4648 | bounded (e.g., snprintf) a lower bound of zero doesn't | |
4649 | necessarily imply it can be eliminated. */ | |
4650 | dstsize_cst_p = false; | |
88d0c3f0 MS |
4651 | } |
4652 | } | |
4653 | ||
91e3ec29 | 4654 | if (idx_objsize != UINT_MAX) |
3eefa646 JJ |
4655 | if (tree size = gimple_call_arg (info.callstmt, idx_objsize)) |
4656 | if (tree_fits_uhwi_p (size)) | |
4657 | objsize = tree_to_uhwi (size); | |
88d0c3f0 MS |
4658 | |
4659 | if (info.bounded && !dstsize) | |
4660 | { | |
4661 | /* As a special case, when the explicitly specified destination | |
4662 | size argument (to a bounded function like snprintf) is zero | |
4663 | it is a request to determine the number of bytes on output | |
4664 | without actually producing any. Pretend the size is | |
4665 | unlimited in this case. */ | |
4666 | info.objsize = HOST_WIDE_INT_MAX; | |
ee75687b | 4667 | info.nowrite = dstsize_cst_p; |
88d0c3f0 MS |
4668 | } |
4669 | else | |
4670 | { | |
2c8c83ac MS |
4671 | /* For calls to non-bounded functions or to those of bounded |
4672 | functions with a non-zero size, warn if the destination | |
4673 | pointer is null. */ | |
91e3ec29 | 4674 | if (dstptr && integer_zerop (dstptr)) |
2c8c83ac MS |
4675 | { |
4676 | /* This is diagnosed with -Wformat only when the null is a constant | |
4677 | pointer. The warning here diagnoses instances where the pointer | |
4678 | is not constant. */ | |
4679 | location_t loc = gimple_location (info.callstmt); | |
4680 | warning_at (EXPR_LOC_OR_LOC (dstptr, loc), | |
6d3bab5d | 4681 | info.warnopt (), "null destination pointer"); |
3eefa646 | 4682 | return false; |
2c8c83ac MS |
4683 | } |
4684 | ||
88d0c3f0 MS |
4685 | /* Set the object size to the smaller of the two arguments |
4686 | of both have been specified and they're not equal. */ | |
4687 | info.objsize = dstsize < objsize ? dstsize : objsize; | |
4688 | ||
4689 | if (info.bounded | |
ee92e7ba MS |
4690 | && dstsize < target_size_max () / 2 && objsize < dstsize |
4691 | /* Avoid warning if -Wstringop-overflow is specified since | |
4692 | it also warns for the same thing though only for the | |
4693 | checking built-ins. */ | |
91e3ec29 | 4694 | && (idx_objsize == UINT_MAX |
ee92e7ba | 4695 | || !warn_stringop_overflow)) |
88d0c3f0 | 4696 | { |
efcc8d38 | 4697 | warning_at (gimple_location (info.callstmt), info.warnopt (), |
ee92e7ba | 4698 | "specified bound %wu exceeds the size %wu " |
88d0c3f0 MS |
4699 | "of the destination object", dstsize, objsize); |
4700 | } | |
4701 | } | |
4702 | ||
91e3ec29 MS |
4703 | /* Determine if the format argument may be null and warn if not |
4704 | and if the argument is null. */ | |
4705 | if (integer_zerop (info.format) | |
4706 | && gimple_call_builtin_p (info.callstmt, BUILT_IN_NORMAL)) | |
88d0c3f0 | 4707 | { |
2c8c83ac MS |
4708 | location_t loc = gimple_location (info.callstmt); |
4709 | warning_at (EXPR_LOC_OR_LOC (info.format, loc), | |
6d3bab5d | 4710 | info.warnopt (), "null format string"); |
3eefa646 | 4711 | return false; |
88d0c3f0 MS |
4712 | } |
4713 | ||
4714 | info.fmtstr = get_format_string (info.format, &info.fmtloc); | |
4715 | if (!info.fmtstr) | |
3eefa646 | 4716 | return false; |
88d0c3f0 | 4717 | |
937a86b4 MS |
4718 | if (warn_restrict) |
4719 | { | |
4720 | /* Compute the origin of the destination pointer and its offset | |
4721 | from the base object/pointer if possible. */ | |
4722 | info.dst_offset = 0; | |
4723 | info.dst_origin = get_origin_and_offset (dstptr, &info.dst_field, | |
4724 | &info.dst_offset); | |
4725 | } | |
4726 | ||
88d0c3f0 MS |
4727 | /* The result is the number of bytes output by the formatted function, |
4728 | including the terminating NUL. */ | |
937a86b4 | 4729 | format_result res; |
01a4551c | 4730 | |
91e3ec29 MS |
4731 | /* I/O functions with no destination argument (i.e., all forms of fprintf |
4732 | and printf) may fail under any conditions. Others (i.e., all forms of | |
4733 | sprintf) may only fail under specific conditions determined for each | |
4734 | directive. Clear POSUNDER4K for the former set of functions and set | |
4735 | it to true for the latter (it can only be cleared later, but it is | |
4736 | never set to true again). */ | |
3e6837c2 | 4737 | res.posunder4k = posunder4k && dstptr; |
91e3ec29 | 4738 | |
3c9f762a | 4739 | bool success = compute_format_length (info, &res, ptr_qry); |
a41cc59d | 4740 | if (res.warned) |
e9e2bad7 | 4741 | suppress_warning (info.callstmt, info.warnopt ()); |
88d0c3f0 MS |
4742 | |
4743 | /* When optimizing and the printf return value optimization is enabled, | |
4744 | attempt to substitute the computed result for the return value of | |
4745 | the call. Avoid this optimization when -frounding-math is in effect | |
4746 | and the format string contains a floating point directive. */ | |
a104bd88 MS |
4747 | bool call_removed = false; |
4748 | if (success && optimize > 0) | |
4749 | { | |
4750 | /* Save a copy of the iterator pointing at the call. The iterator | |
4751 | may change to point past the call in try_substitute_return_value | |
4752 | but the original value is needed in try_simplify_call. */ | |
4753 | gimple_stmt_iterator gsi_call = *gsi; | |
02f1ef40 | 4754 | |
a104bd88 MS |
4755 | if (flag_printf_return_value |
4756 | && (!flag_rounding_math || !res.floating)) | |
4757 | call_removed = try_substitute_return_value (gsi, info, res); | |
4758 | ||
4759 | if (!call_removed) | |
4760 | try_simplify_call (&gsi_call, info, res); | |
4761 | } | |
4762 | ||
4763 | return call_removed; | |
88d0c3f0 | 4764 | } |