validate: fix type stack and leaks
[m6w6/ext-psi] / src / types / decl_struct.c
index 9ba53e9..ffd0efc 100644 (file)
@@ -90,7 +90,6 @@ bool psi_decl_struct_validate(struct psi_data *data, struct psi_decl_struct *s,
        if (psi_validate_stack_has_struct(type_stack, s->name)) {
                return true;
        }
-       psi_validate_stack_add_struct(type_stack, s->name, s);
 
        if (!s->size && !psi_plist_count(s->args)) {
                data->error(data, s->token, PSI_WARNING,
@@ -98,10 +97,13 @@ bool psi_decl_struct_validate(struct psi_data *data, struct psi_decl_struct *s,
                return false;
        }
 
+       psi_validate_stack_add_struct(type_stack, s->name, s);
+
        for (i = 0; psi_plist_get(s->args, i, &darg); ++i) {
                darg->var->arg = darg;
 
                if (!psi_decl_arg_validate(data, darg, type_stack)) {
+                       psi_validate_stack_del_struct(type_stack, s->name);
                        return false;
                }
 
@@ -113,6 +115,7 @@ bool psi_decl_struct_validate(struct psi_data *data, struct psi_decl_struct *s,
                                data->error(data, darg->token, PSI_WARNING,
                                                "Computed zero alignment of %s.%s of type '%s'",
                                                len, s->name, darg->var->name, darg->type->name);
+                               psi_validate_stack_del_struct(type_stack, s->name);
                                return false;
                        }
 
@@ -148,6 +151,7 @@ bool psi_decl_struct_validate(struct psi_data *data, struct psi_decl_struct *s,
                                        default:
                                                data->error(data, darg->token, PSI_WARNING,
                                                                "Unsupported type for bit field: %s", real->name);
+                                               psi_validate_stack_del_struct(type_stack, s->name);
                                                return false;
                                        }
                                        darg->layout->bfw->pos = prev_arg->layout->bfw->pos + prev_arg->layout->bfw->len;
@@ -196,6 +200,8 @@ bool psi_decl_struct_validate(struct psi_data *data, struct psi_decl_struct *s,
                }
        }
 
+       assert(s->size);
+
        return true;
 }