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