From: Martin Sebor <msebor@gmail.com>
To: Jeff Law <law@redhat.com>, Gcc Patch List <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] correct %g handling with unknown arguments in -fprintf-return-value (PR 78696)
Date: Tue, 13 Dec 2016 00:06:00 -0000 [thread overview]
Message-ID: <8df81cae-ca9e-18ea-e52a-ff542484e786@gmail.com> (raw)
In-Reply-To: <bb73c8ed-1f72-e3a0-b2d8-f7be5e9bdeb4@redhat.com>
>> + /* The lower bound when precision isn't specified is 8 bytes
>> + ("1.23456" since precision is taken to be 6). When precision
>> + is zero, the lower bound is 1 byte (e.g., "1"). Otherwise,
>> + when precision is greater than zero, then the lower bound
>> + is 2 plus precision (plus flags). */
>> + res.range.min = (flagmin
>> + + (prec != INT_MIN) /* for decimal point */
>> + + (prec == INT_MIN
>> + ? 0 : prec < 0 ? 6 : prec ? prec : -1));
> Note for the future, nest/chained ternary operators can sometimes just
> be hard to visually parse when they're squashed on a single line.
> Formatting like this has often been used in the past to help clarify the
> intent:
>
> (flagmin
> + (prec != INT_MIN)
> + (prec == INT_MIN ? 0
> : prec < 0 ? 6
> : prec ? prec
> : -1)
Okay.
>
> If we ignore the flagmin component, I get the following evaluations for
> PREC.
>
> PREC RESULT
> INTMIN 0
> 0 0
> negative (but not INTMIN) 7
> positive prec + 1
>
> That doesn't seem in-line with the comment.
Sorry, I think I need a hint. Which part doesn't seem in line with
which part of the comment? The numbers you have look correct to me
and I don't see anything wrong with the comment either.
Flagmin is always at least 1 and so RESULT above is 1 when precision
is used and either zero or unknown (because printf ("%.0f", 0) returns
1), and it's 8 when precision is negative because it's taken as if had
been omitted (i.e., it's 6 and printf ("%f", 0) formats "0.000000" and
returns 8), and it's prec + 2 when precision is positive because the 2
accounts for the leading "0." (when argument is zero) and precision is
the number of fractional digits.
Martin
PS While testing this I came across a bug in MPFR. When precision is
large it allocates huge amounts of memory and appears to get stuck in
some sort of a loop (e.g., with sprintf(d, "%.*g", INT_MAX)). I've
tested the diff below to work around it.
diff --git a/gcc/gimple-ssa-sprintf.c b/gcc/gimple-ssa-sprintf.c
index 3d34342..c39b318 100644
--- a/gcc/gimple-ssa-sprintf.c
+++ b/gcc/gimple-ssa-sprintf.c
@@ -1170,8 +1170,27 @@ format_floating_max (tree type, char spec, int prec)
if (-1 < prec)
{
- const char fmt[] = { '%', '.', '*', 'R', spec, '\0' };
- n = mpfr_snprintf (NULL, 0, fmt, prec, x);
+ /* Prevent MPFR from allocating too much memory. */
+ if (TOUPPER (spec) == 'G')
+ {
+ /* For G/g, precision gives the maximum number of significant
+ digits which is bounded by LDBL_MAX_10_EXP, or, for a 128
+ bit IEEE extended precision, 4932. Using twice as much
+ here should be more than sufficient for any format. */
+ int p = 9864 < prec ? 9864 : prec;
+ const char fmt[] = { '%', '.', '*', 'R', spec, '\0' };
+ n = mpfr_snprintf (NULL, 0, fmt, p, x);
+ }
+ else
+ {
+ /* Cap precision at 1KB and add the difference to the MPFR
+ result. */
+ int p = 1024 < prec ? 1024 : prec;
+ const char fmt[] = { '%', '.', '*', 'R', spec, '\0' };
+ n = mpfr_snprintf (NULL, 0, fmt, p, x);
+ if (p < prec)
+ n += prec - p;
+ }
}
else
{
next prev parent reply other threads:[~2016-12-13 0:06 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-12-09 18:08 Martin Sebor
2016-12-12 21:13 ` Jeff Law
2016-12-13 0:06 ` Martin Sebor [this message]
2016-12-19 22:26 ` Jeff Law
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=8df81cae-ca9e-18ea-e52a-ff542484e786@gmail.com \
--to=msebor@gmail.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=law@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).