Fix the static initializer build with UBSan 56/94856/2
authorSlava Barinov <v.barinov@samsung.com>
Wed, 28 Sep 2016 14:44:47 +0000 (17:44 +0300)
committerDongkyun Son <dongkyun.s@samsung.com>
Wed, 16 Nov 2016 04:32:08 +0000 (20:32 -0800)
The idea is borrowed from the upstream 56ac70e (trunk@212499).
Without the check initializer is not fold to constant and therefore structure
cannot be initialized.

* gcc/fold-const.c (tree_single_nonzero_warnv_p): Check if local object
is WEAK to remove then NULL check.
* gcc.dg/ubsan/static-init-null.c: New testcase.

Change-Id: I895708e5ae8b1cffcdbdf060ce8894a029937e75
Signed-off-by: Slava Barinov <v.barinov@samsung.com>
gcc/fold-const.c
gcc/testsuite/gcc.dg/ubsan/static-init-null.c [new file with mode: 0644]

index e637717..e1b016e 100644 (file)
@@ -16265,7 +16265,7 @@ tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
           so protect with -fdelete-null-pointer-checks; but not variables
           allocated on the stack.  */
        if (DECL_P (base)
-           && (flag_delete_null_pointer_checks
+           && ((flag_delete_null_pointer_checks || !DECL_WEAK (base))
                || (DECL_CONTEXT (base)
                    && TREE_CODE (DECL_CONTEXT (base)) == FUNCTION_DECL
                    && auto_var_in_fn_p (base, DECL_CONTEXT (base)))))
diff --git a/gcc/testsuite/gcc.dg/ubsan/static-init-null.c b/gcc/testsuite/gcc.dg/ubsan/static-init-null.c
new file mode 100644 (file)
index 0000000..678e33c
--- /dev/null
@@ -0,0 +1,25 @@
+/* { dg-do compile } */
+/* { dg-options "-fsanitize=null" } */
+
+typedef struct a_s a_t;
+typedef struct b_s b_t;
+
+struct b_s
+{
+  int n;
+};
+
+struct a_s
+{
+  int n;
+};
+
+static a_t arr[] = {
+  {
+    .n = 1,
+  },
+};
+
+__attribute__ ((used)) static b_t inited_struct = {
+  .n = arr == 0 ? 0 : 16 / sizeof (arr[0]),
+};