PR tree-optimization/78608 - gimple-ssa-sprintf.c:570:17: runtime error: negation...
authorMartin Sebor <msebor@redhat.com>
Tue, 17 Jan 2017 00:14:52 +0000 (00:14 +0000)
committerMartin Sebor <msebor@gcc.gnu.org>
Tue, 17 Jan 2017 00:14:52 +0000 (17:14 -0700)
gcc/ChangeLog:
* gimple-ssa-sprintf.c (tree_digits): Avoid negating TYPE_MIN.

From-SVN: r244511

gcc/ChangeLog
gcc/gimple-ssa-sprintf.c

index 3ef73ce180e3b87d1c1942fdcab8d9b29160618e..ead236ff5fb567667a9753ff810f870054ba500c 100644 (file)
@@ -1,3 +1,8 @@
+2017-01-16  Martin Sebor  <msebor@redhat.com>
+
+       PR tree-optimization/78608
+       * gimple-ssa-sprintf.c (tree_digits): Avoid negating TYPE_MIN.
+
 2017-01-16  Jeff Law  <law@redhat.com>
 
        Revert:
index 262857c64ded53ff7b5c3ccc076ebdb1bb2b1681..76e851231d6bccb9007b2e941f82ac036ce08945 100644 (file)
@@ -577,16 +577,22 @@ tree_digits (tree x, int base, HOST_WIDE_INT prec, bool plus, bool prefix)
       if (tree_fits_shwi_p (x))
        {
          HOST_WIDE_INT i = tree_to_shwi (x);
-         if (i < 0)
-           {
-             absval = -i;
-             res = 1;
-           }
-         else
-           {
-             absval = i;
-             res = plus;
-           }
+         if (HOST_WIDE_INT_MIN == i)
+           {
+             /* Avoid undefined behavior due to negating a minimum.  */
+             absval = HOST_WIDE_INT_MAX;
+             res = 1;
+           }
+         else if (i < 0)
+          {
+            absval = -i;
+            res = 1;
+          }
+        else
+          {
+            absval = i;
+            res = plus;
+          }
        }
       else
        return -1;