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>
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)))))
--- /dev/null
+/* { 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]),
+};