On 03/13/2017 06:33 PM, Martin Sebor wrote:
The output of a floating point directive whose precision is
specified by an asterisk with an argument that's in a range
that includes both negative and positive values less than
six may include between zero and six fractional digits plus
a decimal point. For example,
printf ("%.*e", p, x);
results in the 14 bytes
-1.797693e+308
when p == -1 and x == -DBL_MIN because a negative precision
is ignored and the directive assumes the default 6, and in
just the one byte
0
when p == 0 and x == 0.0.
Current trunk doesn't handle this case correctly when the
floating argument isn't known and uses the upper bound of
the specified precision as the maximum number of fractional
digits. As a result, it sets the range on the return value
in this case as [5, 7] (plus 5 for the longest multibyte
decimal decimal point) when the correct range is [5, 14] as
explained above (plus 5 again).
The attached patch corrects the handling of such precision
ranges to avoid this unlikely bug.
Martin
gcc-79800.diff
PR tree-optimization/79800 - wrong snprintf result range with precision in a
narrow negative-positive range
gcc/ChangeLog:
PR tree-optimization/79800
* gimple-ssa-sprintf.c (format_floating: Add argument. Handle
precision in negative-positive range.
(format_floating): Call non-const overload with adjusted precision.
gcc/testsuite/ChangeLog:
PR tree-optimization/79800
* gcc.dg/tree-ssa/builtin-sprintf-warn-15.c: Add test cases.
* gcc.dg/tree-ssa/pr79800.c: New test.
Thanks. Installed.
jeff