diff options
author | Martin Sebor <msebor@redhat.com> | 2017-01-05 22:32:09 +0000 |
---|---|---|
committer | Martin Sebor <msebor@gcc.gnu.org> | 2017-01-05 15:32:09 -0700 |
commit | 5b8999d67ec8c7ab68dc85fabf3445102a2e4b06 (patch) | |
tree | aa71123de95c075e66fe8598afb85c01002ef5f0 /gcc/gimple-ssa-sprintf.c | |
parent | 4e89adf9709dba511949a65a211d9bf702fe5753 (diff) | |
download | gcc-5b8999d67ec8c7ab68dc85fabf3445102a2e4b06.zip gcc-5b8999d67ec8c7ab68dc85fabf3445102a2e4b06.tar.gz gcc-5b8999d67ec8c7ab68dc85fabf3445102a2e4b06.tar.bz2 |
PR tree-optimization/78910 - Wrong print-return-value for a negative number
gcc/ChangeLog:
PR tree-optimization/78910
* gimple-ssa-sprintf.c (tree_digits): Add an argument.
(format_integer): Correct off-by-one error in the handling
of precision with negative numbers in signed conversions..
gcc/testsuite/ChangeLog:
PR tree-optimization/78910
* gcc.dg/tree-ssa/builtin-sprintf-warn-7.c: Adjust text of expected
diagnostics.
* gcc.dg/tree-ssa/builtin-sprintf.c: Add test cases.
* gcc.dg/tree-ssa/pr78910.c: New test.
From-SVN: r244116
Diffstat (limited to 'gcc/gimple-ssa-sprintf.c')
-rw-r--r-- | gcc/gimple-ssa-sprintf.c | 28 |
1 files changed, 15 insertions, 13 deletions
diff --git a/gcc/gimple-ssa-sprintf.c b/gcc/gimple-ssa-sprintf.c index d468cd7..6a9f679 100644 --- a/gcc/gimple-ssa-sprintf.c +++ b/gcc/gimple-ssa-sprintf.c @@ -549,17 +549,18 @@ ilog (unsigned HOST_WIDE_INT x, int base) } /* Return the number of bytes resulting from converting into a string - the INTEGER_CST tree node X in BASE. PLUS indicates whether 1 for - a plus sign should be added for positive numbers, and PREFIX whether - the length of an octal ('O') or hexadecimal ('0x') prefix should be - added for nonzero numbers. Return -1 if X cannot be represented. */ - -static int -tree_digits (tree x, int base, bool plus, bool prefix) + the INTEGER_CST tree node X in BASE with a minimum of PREC digits. + PLUS indicates whether 1 for a plus sign should be added for positive + numbers, and PREFIX whether the length of an octal ('O') or hexadecimal + ('0x') prefix should be added for nonzero numbers. Return -1 if X cannot + be represented. */ + +static HOST_WIDE_INT +tree_digits (tree x, int base, HOST_WIDE_INT prec, bool plus, bool prefix) { unsigned HOST_WIDE_INT absval; - int res; + HOST_WIDE_INT res; if (TYPE_UNSIGNED (TREE_TYPE (x))) { @@ -591,7 +592,9 @@ tree_digits (tree x, int base, bool plus, bool prefix) return -1; } - res += ilog (absval, base); + int ndigs = ilog (absval, base); + + res += prec < ndigs ? ndigs : prec; if (prefix && absval) { @@ -1022,10 +1025,9 @@ format_integer (const conversion_spec &spec, tree arg) /* True when a conversion is preceded by a prefix indicating the base of the argument (octal or hexadecimal). */ bool maybebase = spec.get_flag ('#'); - len = tree_digits (arg, base, maybesign, maybebase); - - if (len < prec) - len = prec; + len = tree_digits (arg, base, prec, maybesign, maybebase); + if (len < 1) + len = HOST_WIDE_INT_MAX; } if (len < width) |