]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
backport: re PR middle-end/56917 (-ftrapv detects a overflow wrongly.)
authorMarek Polacek <polacek@redhat.com>
Wed, 11 Mar 2015 10:37:38 +0000 (10:37 +0000)
committerMarek Polacek <mpolacek@gcc.gnu.org>
Wed, 11 Mar 2015 10:37:38 +0000 (10:37 +0000)
Backported from mainline
2014-12-04  Marek Polacek  <polacek@redhat.com>

PR middle-end/56917
* fold-const.c (fold_unary_loc): Perform the negation in A's type
when transforming ~ (A - 1) or ~ (A + -1) to -A.

* c-c++-common/ubsan/pr56917.c: New test.

From-SVN: r221346

gcc/ChangeLog
gcc/fold-const.c
gcc/testsuite/ChangeLog
gcc/testsuite/c-c++-common/ubsan/pr56917.c [new file with mode: 0644]

index 9a152916076d6f0cbe598ae61975c98e63ef141e..59ee3fd80cda217225f88255acf747bbd4da0667 100644 (file)
@@ -1,3 +1,12 @@
+2015-03-11  Marek Polacek  <polacek@redhat.com>
+
+       Backported from mainline
+       2014-12-04  Marek Polacek  <polacek@redhat.com>
+
+       PR middle-end/56917
+       * fold-const.c (fold_unary_loc): Perform the negation in A's type
+       when transforming ~ (A - 1) or ~ (A + -1) to -A.
+
 2015-03-10  Yvan Roux  <yvan.roux@linaro.org>
 
        Backport from trunk r220489.
index 901204f3ea5bf5aa1e559f6947fa47113071174b..71e1e0a5785a6b2403b04c4491ba111129977a20 100644 (file)
@@ -8324,9 +8324,14 @@ fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
                    && integer_onep (TREE_OPERAND (arg0, 1)))
                   || (TREE_CODE (arg0) == PLUS_EXPR
                       && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
-       return fold_build1_loc (loc, NEGATE_EXPR, type,
-                           fold_convert_loc (loc, type,
-                                             TREE_OPERAND (arg0, 0)));
+       {
+         /* Perform the negation in ARG0's type and only then convert
+            to TYPE as to avoid introducing undefined behavior.  */
+         tree t = fold_build1_loc (loc, NEGATE_EXPR,
+                                   TREE_TYPE (TREE_OPERAND (arg0, 0)),
+                                   TREE_OPERAND (arg0, 0));
+         return fold_convert_loc (loc, type, t);
+       }
       /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
               && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
index a25355ad0fa11468950000ec17893174e1ea6b16..ed9fda97b30f4a9a154e36c2cf81b3c31cd9fc38 100644 (file)
@@ -1,3 +1,11 @@
+2015-03-11  Marek Polacek  <polacek@redhat.com>
+
+       Backported from mainline
+       2014-12-04  Marek Polacek  <polacek@redhat.com>
+
+       PR middle-end/56917
+       * c-c++-common/ubsan/pr56917.c: New test.
+
 2015-03-10  Paul Thomas  <pault@gcc.gnu.org>
 
        Backported from mainline
diff --git a/gcc/testsuite/c-c++-common/ubsan/pr56917.c b/gcc/testsuite/c-c++-common/ubsan/pr56917.c
new file mode 100644 (file)
index 0000000..0eb8492
--- /dev/null
@@ -0,0 +1,43 @@
+/* PR middle-end/56917 */
+/* { dg-do run } */
+/* { dg-options "-fsanitize=undefined" } */
+
+#include <stdio.h>
+
+#define INT_MIN (-__INT_MAX__ - 1)
+#define LONG_MIN (-__LONG_MAX__ - 1L)
+#define LLONG_MIN (-__LONG_LONG_MAX__ - 1LL)
+
+int __attribute__ ((noinline,noclone))
+fn1 (unsigned int u)
+{
+  return (-(int) (u - 1U)) - 1;
+}
+
+long __attribute__ ((noinline,noclone))
+fn2 (unsigned long int ul)
+{
+  return (-(long) (ul - 1UL)) - 1L;
+}
+
+long long __attribute__ ((noinline,noclone))
+fn3 (unsigned long long int ull)
+{
+  return (-(long long) (ull - 1ULL)) - 1LL;
+}
+
+int
+main (void)
+{
+  fputs ("UBSAN TEST START\n", stderr);
+
+  if (fn1 (__INT_MAX__ + 1U) != INT_MIN
+      || fn2 (__LONG_MAX__ + 1UL) != LONG_MIN
+      || fn3 (__LONG_LONG_MAX__ + 1ULL) != LLONG_MIN)
+    __builtin_abort ();
+
+  fputs ("UBSAN TEST END\n", stderr);
+  return 0;
+}
+
+/* { dg-output "UBSAN TEST START(\n|\r\n|\r)UBSAN TEST END" } */