]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
re PR sanitizer/82072 (sanitizer does not detect an overflow from LLONG_MIN)
authorMarek Polacek <polacek@redhat.com>
Tue, 5 Sep 2017 15:55:04 +0000 (15:55 +0000)
committerMarek Polacek <mpolacek@gcc.gnu.org>
Tue, 5 Sep 2017 15:55:04 +0000 (15:55 +0000)
PR sanitizer/82072
* convert.c (convert_to_integer_1) <case NEGATE_EXPR>: Move the ubsan
check earlier.

* c-c++-common/ubsan/pr82072-2.c: New test.

From-SVN: r251717

gcc/ChangeLog
gcc/convert.c
gcc/testsuite/ChangeLog
gcc/testsuite/c-c++-common/ubsan/pr82072-2.c [new file with mode: 0644]

index 58d162972ba9de64dc5d002be2fb4c51ad78d380..2f6983ffa9ecdfa6cb6d5609f297406eb295c786 100644 (file)
@@ -1,3 +1,9 @@
+2017-09-05  Marek Polacek  <polacek@redhat.com>
+
+       PR sanitizer/82072
+       * convert.c (convert_to_integer_1) <case NEGATE_EXPR>: Move the ubsan
+       check earlier.
+
 2017-09-05  Wilco Dijkstra  <wdijkstr@arm.com>
 
        * explow.c (get_dynamic_stack_size): Improve dynamic alignment.
index 139d790fd981c4635a4f4d0cba93e5f9fbe0cde9..bfe18fb0f432b1d6cd52f04de4ffcec96299339a 100644 (file)
@@ -886,6 +886,12 @@ convert_to_integer_1 (tree type, tree expr, bool dofold)
            break;
 
          case NEGATE_EXPR:
+           /* Using unsigned arithmetic for signed types may hide overflow
+              bugs.  */
+           if (!TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (expr, 0)))
+               && sanitize_flags_p (SANITIZE_SI_OVERFLOW))
+             break;
+           /* Fall through.  */
          case BIT_NOT_EXPR:
            /* This is not correct for ABS_EXPR,
               since we must test the sign before truncation.  */
@@ -902,12 +908,7 @@ convert_to_integer_1 (tree type, tree expr, bool dofold)
                                                    TYPE_UNSIGNED (typex));
 
              if (!TYPE_UNSIGNED (typex))
-               {
-                 /* Using unsigned arithmetic may hide overflow bugs.  */
-                 if (sanitize_flags_p (SANITIZE_SI_OVERFLOW))
-                   break;
-                 typex = unsigned_type_for (typex);
-               }
+               typex = unsigned_type_for (typex);
              return convert (type,
                              fold_build1 (ex_form, typex,
                                           convert (typex,
index 690bc5bacce16836fb35d52b80f7637294256381..623e41417f9f8043af4296e63072cefd9e613c8d 100644 (file)
@@ -1,3 +1,8 @@
+2017-09-05  Marek Polacek  <polacek@redhat.com>
+
+       PR sanitizer/82072
+       * c-c++-common/ubsan/pr82072-2.c: New test.
+
 2017-09-05  Paolo Carlini  <paolo.carlini@oracle.com>
 
        PR c++/81942
diff --git a/gcc/testsuite/c-c++-common/ubsan/pr82072-2.c b/gcc/testsuite/c-c++-common/ubsan/pr82072-2.c
new file mode 100644 (file)
index 0000000..ff8aca4
--- /dev/null
@@ -0,0 +1,15 @@
+/* PR sanitizer/82072 */
+/* { dg-do run } */
+/* { dg-options "-fsanitize=signed-integer-overflow" } */
+
+int
+main ()
+{
+  long long int l = -__LONG_LONG_MAX__ - 1;
+  unsigned int u;
+  u = -l;
+  asm volatile ("" : "+r" (u));
+  return 0;
+}
+
+/* { dg-output "negation of -9223372036854775808 cannot be represented in type 'long long int'\[^\n\r]*; cast to an unsigned type to negate this value to itself" } */