I think it really doesn't make sense to instrument static initializers; we
wouldn't be able to error at run-time anyway.  Besides, it causes a compile-time
error.  Generally, I think *compiling* with -fsanitize=undefined shouldn't add
any new compile-time errors.  Yes, I know it does in some other cases; this is
just an incremental improvement.

Bootstrapped/regtested on x86_64-linux, ok for trunk?

2015-09-04  Marek Polacek  <pola...@redhat.com>

        PR sanitizer/67279
        * c-typeck.c (build_binary_op): Don't instrument static initializers.

        * gcc.dg/ubsan/pr67279.c: New test.

diff --git gcc/c/c-typeck.c gcc/c/c-typeck.c
index c622a90..dc22396 100644
--- gcc/c/c-typeck.c
+++ gcc/c/c-typeck.c
@@ -11292,7 +11292,8 @@ build_binary_op (location_t location, enum tree_code 
code,
   if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
                        | SANITIZE_FLOAT_DIVIDE))
       && do_ubsan_in_current_function ()
-      && (doing_div_or_mod || doing_shift))
+      && (doing_div_or_mod || doing_shift)
+      && !require_constant_value)
     {
       /* OP0 and/or OP1 might have side-effects.  */
       op0 = c_save_expr (op0);
diff --git gcc/testsuite/gcc.dg/ubsan/pr67279.c 
gcc/testsuite/gcc.dg/ubsan/pr67279.c
index e69de29..5b5db42 100644
--- gcc/testsuite/gcc.dg/ubsan/pr67279.c
+++ gcc/testsuite/gcc.dg/ubsan/pr67279.c
@@ -0,0 +1,14 @@
+/* PR sanitizer/67279 */
+/* { dg-do compile } */
+/* { dg-options "-fsanitize=undefined -w" } */
+
+#define INT_MIN (-__INT_MAX__ - 1)
+
+void
+foo (void)
+{
+  static int a1 = 1 << 31;
+  static int a2 = 10 << 30;
+  static int a3 = 100 << 28;
+  static int a4 = INT_MIN / -1;
+}

        Marek

Reply via email to