re PR sanitizer/81097 (UBSAN: false positive for not existing negation operator and...
authorRichard Biener <rguenther@suse.de>
Tue, 20 Jun 2017 12:46:46 +0000 (12:46 +0000)
committerRichard Biener <rguenth@gcc.gnu.org>
Tue, 20 Jun 2017 12:46:46 +0000 (12:46 +0000)
2017-06-20  Richard Biener  <rguenther@suse.de>

PR middle-end/81097
* fold-const.c (split_tree): Fold to type before negating.

* c-c++-common/ubsan/pr81097.c: New testcase.

From-SVN: r249407

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

index 4293a05242df0636f93d160a5b6007a95782d66c..8210d273a5a805459f634b951ed656defbe50803 100644 (file)
@@ -1,3 +1,8 @@
+2017-06-20  Richard Biener  <rguenther@suse.de>
+
+       PR middle-end/81097
+       * fold-const.c (split_tree): Fold to type before negating.
+
 2017-06-20  David Malcolm  <dmalcolm@redhat.com>
 
        * diagnostic-show-locus.c
index 8559b1d073188844704fda8a917a90d3749d856e..379a30ea285acf6d78042bc1a3a5c152539b2b75 100644 (file)
@@ -853,9 +853,9 @@ split_tree (location_t loc, tree in, tree type, enum tree_code code,
           && code == PLUS_EXPR)
     {
       /* -X - 1 is folded to ~X, undo that here.  Do _not_ do this
-         when IN is constant.  */
-      *minus_litp = build_one_cst (TREE_TYPE (in));
-      var = negate_expr (TREE_OPERAND (in, 0));
+         when IN is constant.  Convert to TYPE before negating.  */
+      *minus_litp = build_one_cst (type);
+      var = negate_expr (fold_convert_loc (loc, type, TREE_OPERAND (in, 0)));
     }
   else
     var = in;
index 9077fe17a9e388ef0d9f03992a709bd050b436f4..28c97de8f3b6ccc5abf66aed9dfc244b2dc138b8 100644 (file)
@@ -1,3 +1,8 @@
+2017-06-20  Richard Biener  <rguenther@suse.de>
+
+       PR middle-end/81097
+       * c-c++-common/ubsan/pr81097.c: New testcase.
+
 2017-06-20  Richard Biener  <rguenther@suse.de>
 
        * gcc.dg/vect/pr65947-9.c: Adjust.
diff --git a/gcc/testsuite/c-c++-common/ubsan/pr81097.c b/gcc/testsuite/c-c++-common/ubsan/pr81097.c
new file mode 100644 (file)
index 0000000..cd323ea
--- /dev/null
@@ -0,0 +1,12 @@
+/* { dg-do run } */
+/* { dg-options "-fsanitize=undefined -fsanitize-undefined-trap-on-error" } */
+
+unsigned int a = 3309568;
+unsigned int b = -1204857327;
+short c = -10871;
+short x;
+int main()
+{
+  x = ((short)(~a) | ~c) +  ((short)(~b) | ~c);
+  return 0;
+}