flow: set members to zero by default
authorDan Carpenter <dan.carpenter@oracle.com>
Mon, 16 Sep 2013 10:25:17 +0000 (16 13:25 +0300)
committerDan Carpenter <dan.carpenter@oracle.com>
Mon, 16 Sep 2013 10:25:17 +0000 (16 13:25 +0300)
If a struct it partially initialized then the not-initialized members are
set to zero.

Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
smatch_flow.c

index 57bd049..0d5c570 100644 (file)
@@ -842,6 +842,29 @@ static struct ident *number_to_member(struct expression *expr, int num)
 
 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
 
+static void set_members_to_zero(struct expression *symbol)
+{
+       struct expression *deref, *assign;
+       struct symbol *type;
+       struct symbol *member, *member_type;
+
+       type = get_type(symbol);
+       if (!type)
+               return;
+
+       FOR_EACH_PTR(type->symbol_list, member) {
+               if (!member->ident)
+                       continue;
+               member_type = get_real_base_type(member);
+               if (!member_type || member_type->type == SYM_ARRAY)
+                       continue;
+               deref = member_expression(symbol, '.', member->ident);
+               assign = assign_expression(deref, zero_expr());
+               __split_expr(assign);
+       } END_FOR_EACH_PTR(member);
+
+}
+
 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
 {
        struct expression *deref, *assign, *tmp;
@@ -853,6 +876,8 @@ static void fake_member_assigns_helper(struct expression *symbol, struct express
        if (!type || (type->type != SYM_STRUCT && type->type != SYM_UNION))
                return;
 
+       set_members_to_zero(symbol);
+
        FOR_EACH_PTR(members, tmp) {
                member = number_to_member(symbol, member_idx);
                while (tmp->type == EXPR_IDENTIFIER) {