re PR sanitizer/66908 (Uninitialized variable when compiled with UBsan)
authorMarek Polacek <polacek@redhat.com>
Thu, 23 Jul 2015 13:54:06 +0000 (13:54 +0000)
committerMarek Polacek <mpolacek@gcc.gnu.org>
Thu, 23 Jul 2015 13:54:06 +0000 (13:54 +0000)
PR sanitizer/66908
* c-ubsan.c: Include gimplify.h.
(ubsan_instrument_division): Unshare OP0 and OP1.
(ubsan_instrument_shift): Likewise.

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

From-SVN: r226110

gcc/c-family/ChangeLog
gcc/c-family/c-ubsan.c
gcc/testsuite/ChangeLog
gcc/testsuite/c-c++-common/ubsan/pr66908.c [new file with mode: 0644]

index 9751ba97550917da1438cbfbed08f2b6308c510d..04c2fd6b1b3859e47419afcd2f3a602b282ab16f 100644 (file)
@@ -1,3 +1,10 @@
+2015-07-23  Marek Polacek  <polacek@redhat.com>
+
+       PR sanitizer/66908
+       * c-ubsan.c: Include gimplify.h.
+       (ubsan_instrument_division): Unshare OP0 and OP1.
+       (ubsan_instrument_shift): Likewise.
+
 2015-07-20  Marek Polacek  <polacek@redhat.com>
            Richard Sandiford  <richard.sandiford@arm.com>
 
index 0baf118bc0b39438d1cee1d5a2258327c8f4b6c5..386951113f2d05089c219a158b96bd56585b332e 100644 (file)
@@ -38,6 +38,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "internal-fn.h"
 #include "stor-layout.h"
 #include "builtins.h"
+#include "gimplify.h"
 
 /* Instrument division by zero and INT_MIN / -1.  If not instrumenting,
    return NULL_TREE.  */
@@ -54,6 +55,9 @@ ubsan_instrument_division (location_t loc, tree op0, tree op1)
   gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (op0))
              == TYPE_MAIN_VARIANT (TREE_TYPE (op1)));
 
+  op0 = unshare_expr (op0);
+  op1 = unshare_expr (op1);
+
   if (TREE_CODE (type) == INTEGER_TYPE
       && (flag_sanitize & SANITIZE_DIVIDE))
     t = fold_build2 (EQ_EXPR, boolean_type_node,
@@ -134,6 +138,9 @@ ubsan_instrument_shift (location_t loc, enum tree_code code,
   HOST_WIDE_INT op0_prec = TYPE_PRECISION (type0);
   tree uprecm1 = build_int_cst (op1_utype, op0_prec - 1);
 
+  op0 = unshare_expr (op0);
+  op1 = unshare_expr (op1);
+
   t = fold_convert_loc (loc, op1_utype, op1);
   t = fold_build2 (GT_EXPR, boolean_type_node, t, uprecm1);
 
index 3a6374cfadb68a7ce97301aa818569abb04e77b1..9743f948b1ac6a5cd5af144c7fe268c2c658de1c 100644 (file)
@@ -1,3 +1,8 @@
+2015-07-23  Marek Polacek  <polacek@redhat.com>
+
+       PR sanitizer/66908
+       * c-c++-common/ubsan/pr66908.c: New test.
+
 2015-07-23  Tom de Vries  <tom@codesourcery.com>
 
        * gcc.dg/autopar/outer-4.c: Add xfail.
diff --git a/gcc/testsuite/c-c++-common/ubsan/pr66908.c b/gcc/testsuite/c-c++-common/ubsan/pr66908.c
new file mode 100644 (file)
index 0000000..5f731f0
--- /dev/null
@@ -0,0 +1,15 @@
+/* PR sanitizer/66908 */
+/* { dg-do compile } */
+/* { dg-options "-fsanitize=shift,bounds -O2 -Werror=maybe-uninitialized" } */
+/* { dg-additional-options "-std=gnu90" { target c } } */
+
+struct S { int a[22]; };
+static int const e[22] = { };
+
+void
+foo (struct S const *s, unsigned int m, unsigned int *res)
+{
+  unsigned int i;
+  for (i = 0; i < 22; ++i)
+    res[i] = ((s->a[i] + e[i]) << m);
+}