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