Commit fbbe4edc authored by Lev Walkin's avatar Lev Walkin Committed by GitHub

Merge pull request #126 from brchiu/fix_some_leak_reported_by_valgrind

Fix many memory leaks reported by valgrind
parents 46fc2bd8 4661dae4
...@@ -330,6 +330,8 @@ main(int ac, char **av) { ...@@ -330,6 +330,8 @@ main(int ac, char **av) {
exit(EX_SOFTWARE); exit(EX_SOFTWARE);
} }
asn1p_delete(asn);
return 0; return 0;
} }
...@@ -423,6 +425,11 @@ importStandardModules(asn1p_t *asn, const char *skeletons_dir) { ...@@ -423,6 +425,11 @@ importStandardModules(asn1p_t *asn, const char *skeletons_dir) {
closedir(dir); closedir(dir);
#endif #endif
#ifdef _WIN32
free(pattern);
#endif
free(target_dir);
return ret; return ret;
} }
......
...@@ -507,6 +507,8 @@ asn1c_lang_C_type_SEQUENCE_def(arg_t *arg) { ...@@ -507,6 +507,8 @@ asn1c_lang_C_type_SEQUENCE_def(arg_t *arg) {
REDIR(saved_target); REDIR(saved_target);
if(tag2el) free(tag2el);
return 0; return 0;
} /* _SEQUENCE_def() */ } /* _SEQUENCE_def() */
...@@ -741,6 +743,9 @@ asn1c_lang_C_type_SET_def(arg_t *arg) { ...@@ -741,6 +743,9 @@ asn1c_lang_C_type_SET_def(arg_t *arg) {
REDIR(saved_target); REDIR(saved_target);
if (tag2el) free(tag2el);
if (tag2el_cxer) free(tag2el_cxer);
return 0; return 0;
} /* _SET_def() */ } /* _SET_def() */
...@@ -874,6 +879,8 @@ asn1c_lang_C_type_SEx_OF_def(arg_t *arg, int seq_of) { ...@@ -874,6 +879,8 @@ asn1c_lang_C_type_SEx_OF_def(arg_t *arg, int seq_of) {
arg->embed++; arg->embed++;
emit_member_table(arg, v); emit_member_table(arg, v);
arg->embed--; arg->embed--;
free(v->Identifier);
v->Identifier = (char *)NULL;
INDENT(-1); INDENT(-1);
OUT("};\n"); OUT("};\n");
...@@ -1113,6 +1120,8 @@ asn1c_lang_C_type_CHOICE_def(arg_t *arg) { ...@@ -1113,6 +1120,8 @@ asn1c_lang_C_type_CHOICE_def(arg_t *arg) {
REDIR(saved_target); REDIR(saved_target);
if (tag2el) free(tag2el);
return 0; return 0;
} /* _CHOICE_def() */ } /* _CHOICE_def() */
......
...@@ -35,6 +35,7 @@ asn1c_emit_constraint_checking_code(arg_t *arg) { ...@@ -35,6 +35,7 @@ asn1c_emit_constraint_checking_code(arg_t *arg) {
int alphabet_table_compiled; int alphabet_table_compiled;
int produce_st = 0; int produce_st = 0;
int ulong_optimize = 0; int ulong_optimize = 0;
int ret = 0;
ct = expr->combined_constraints; ct = expr->combined_constraints;
if(ct == NULL) if(ct == NULL)
...@@ -158,7 +159,7 @@ asn1c_emit_constraint_checking_code(arg_t *arg) { ...@@ -158,7 +159,7 @@ asn1c_emit_constraint_checking_code(arg_t *arg) {
OUT("\n"); OUT("\n");
OUT("/* Constraint check succeeded */\n"); OUT("/* Constraint check succeeded */\n");
OUT("return 0;\n"); OUT("return 0;\n");
return 0; goto end;
} }
/* /*
...@@ -197,7 +198,8 @@ asn1c_emit_constraint_checking_code(arg_t *arg) { ...@@ -197,7 +198,8 @@ asn1c_emit_constraint_checking_code(arg_t *arg) {
INDENTED(OUT("/* Nothing is here. See below */\n")); INDENTED(OUT("/* Nothing is here. See below */\n"));
OUT("}\n"); OUT("}\n");
OUT("\n"); OUT("\n");
return 1; ret = 1;
goto end;
} }
INDENT(-1); INDENT(-1);
OUT(") {\n"); OUT(") {\n");
...@@ -222,7 +224,11 @@ asn1c_emit_constraint_checking_code(arg_t *arg) { ...@@ -222,7 +224,11 @@ asn1c_emit_constraint_checking_code(arg_t *arg) {
INDENT(-1); INDENT(-1);
OUT("}\n"); OUT("}\n");
return 0; end:
if (r_value) asn1constraint_range_free(r_value);
if (r_size) asn1constraint_range_free(r_size);
return ret;
} }
static int static int
......
...@@ -140,6 +140,16 @@ asn1c_new_dep(const char *filename) { ...@@ -140,6 +140,16 @@ asn1c_new_dep(const char *filename) {
return d; return d;
} }
static void
asn1c_free_dep(asn1c_fdeps_t *d) {
if(d) {
if(d->filename) free(d->filename);
d->filename = 0;
free(d);
}
}
static int static int
asn1c_dep_add(asn1c_fdeps_t *deps, asn1c_fdeps_t *d) { asn1c_dep_add(asn1c_fdeps_t *deps, asn1c_fdeps_t *d) {
int n; int n;
...@@ -180,7 +190,9 @@ asn1c_deps_makelist(asn1c_fdeps_t *deps) { ...@@ -180,7 +190,9 @@ asn1c_deps_makelist(asn1c_fdeps_t *deps) {
if(deps->filename && deps->usage != FDEP_NOTUSED) { if(deps->filename && deps->usage != FDEP_NOTUSED) {
d = asn1c_new_dep(deps->filename); d = asn1c_new_dep(deps->filename);
d->usage = deps->usage; d->usage = deps->usage;
asn1c_dep_add(dlist, d); if(!asn1c_dep_add(dlist, d)) {
asn1c_free_dep(d);
}
} }
for(i = 0; i < deps->el_count; i++) { for(i = 0; i < deps->el_count; i++) {
...@@ -188,10 +200,28 @@ asn1c_deps_makelist(asn1c_fdeps_t *deps) { ...@@ -188,10 +200,28 @@ asn1c_deps_makelist(asn1c_fdeps_t *deps) {
d = asn1c_deps_makelist(deps->elements[i]); d = asn1c_deps_makelist(deps->elements[i]);
assert(!d->filename); assert(!d->filename);
for(j = 0; j < d->el_count; j++) { for(j = 0; j < d->el_count; j++) {
asn1c_dep_add(dlist, d->elements[j]); if(asn1c_dep_add(dlist, d->elements[j])) {
d->elements[j] = 0;
}
} }
asn1c_deps_freelist(d);
} }
return dlist; return dlist;
} }
void
asn1c_deps_freelist(asn1c_fdeps_t *deps) {
if(deps) {
int i;
if(deps->elements) {
for(i = 0; i < deps->el_count; i++) {
asn1c_deps_freelist(deps->elements[i]);
deps->elements[i] = 0;
}
free(deps->elements);
deps->elements = 0;
}
asn1c_free_dep(deps);
}
}
...@@ -25,5 +25,6 @@ int asn1c_activate_dependency(asn1c_fdeps_t *deps, asn1c_fdeps_t *cur, ...@@ -25,5 +25,6 @@ int asn1c_activate_dependency(asn1c_fdeps_t *deps, asn1c_fdeps_t *cur,
const char *data); const char *data);
asn1c_fdeps_t *asn1c_deps_makelist(asn1c_fdeps_t *deps); asn1c_fdeps_t *asn1c_deps_makelist(asn1c_fdeps_t *deps);
void asn1c_deps_freelist(asn1c_fdeps_t *deps);
#endif /* ASN1C_FDEPS_H */ #endif /* ASN1C_FDEPS_H */
...@@ -149,6 +149,9 @@ asn1c_save_compiled_output(arg_t *arg, const char *datadir, ...@@ -149,6 +149,9 @@ asn1c_save_compiled_output(arg_t *arg, const char *datadir,
safe_fprintf(mkf, "ASN_%s_%s+=%s\n", safe_fprintf(mkf, "ASN_%s_%s+=%s\n",
what_class, what_kind, fname); what_class, what_kind, fname);
} }
asn1c_deps_freelist(deps);
asn1c_deps_freelist(dlist);
} }
if(need_to_generate_pdu_collection(arg)) { if(need_to_generate_pdu_collection(arg)) {
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
static void default_logger_cb(int, const char *fmt, ...); static void default_logger_cb(int, const char *fmt, ...);
static int asn1c_compile_expr(arg_t *arg); static int asn1c_compile_expr(arg_t *arg);
static int asn1c_attach_streams(asn1p_expr_t *expr); static int asn1c_attach_streams(asn1p_expr_t *expr);
static int asn1c_detach_streams(asn1p_expr_t *expr);
int int
asn1_compile(asn1p_t *asn, const char *datadir, enum asn1c_flags flags, asn1_compile(asn1p_t *asn, const char *datadir, enum asn1c_flags flags,
...@@ -61,6 +62,12 @@ asn1_compile(asn1p_t *asn, const char *datadir, enum asn1c_flags flags, ...@@ -61,6 +62,12 @@ asn1_compile(asn1p_t *asn, const char *datadir, enum asn1c_flags flags,
if(asn1c_save_compiled_output(arg, datadir, argc, optc, argv)) if(asn1c_save_compiled_output(arg, datadir, argc, optc, argv))
return -1; return -1;
TQ_FOR(mod, &(asn->modules), mod_next) {
TQ_FOR(arg->expr, &(mod->members), next) {
asn1c_detach_streams(arg->expr);
}
}
return 0; return 0;
} }
...@@ -159,6 +166,28 @@ asn1c_attach_streams(asn1p_expr_t *expr) { ...@@ -159,6 +166,28 @@ asn1c_attach_streams(asn1p_expr_t *expr) {
return 0; return 0;
} }
int
asn1c_detach_streams(asn1p_expr_t *expr) {
compiler_streams_t *cs;
out_chunk_t *m;
int i;
if(!expr->data)
return 0; /* Already detached? */
cs = expr->data;
for(i = 0; i < OT_MAX; i++) {
while((m = TQ_REMOVE(&(cs->destination[i].chunks), next))) {
free(m->buf);
free(m);
}
}
free(expr->data);
expr->data = (void *)NULL;
return 0;
}
static void static void
default_logger_cb(int _severity, const char *fmt, ...) { default_logger_cb(int _severity, const char *fmt, ...) {
va_list ap; va_list ap;
......
...@@ -22,6 +22,7 @@ asn1p_expr_new(int _lineno, asn1p_module_t *mod) { ...@@ -22,6 +22,7 @@ asn1p_expr_new(int _lineno, asn1p_module_t *mod) {
expr->spec_index = -1; expr->spec_index = -1;
expr->module = mod; expr->module = mod;
expr->_lineno = _lineno; expr->_lineno = _lineno;
expr->ref_cnt = 0;
} }
return expr; return expr;
...@@ -235,6 +236,12 @@ asn1p_expr_free(asn1p_expr_t *expr) { ...@@ -235,6 +236,12 @@ asn1p_expr_free(asn1p_expr_t *expr) {
if(expr) { if(expr) {
asn1p_expr_t *tm; asn1p_expr_t *tm;
if (expr->ref_cnt) {
/* Decrease reference count only */
expr->ref_cnt--;
return;
}
/* Remove all children */ /* Remove all children */
while((tm = TQ_REMOVE(&(expr->members), next))) { while((tm = TQ_REMOVE(&(expr->members), next))) {
if(tm->parent_expr != expr) if(tm->parent_expr != expr)
......
...@@ -216,6 +216,7 @@ typedef struct asn1p_expr_s { ...@@ -216,6 +216,7 @@ typedef struct asn1p_expr_s {
asn1p_value_t *default_value; /* For EM_DEFAULT case */ asn1p_value_t *default_value; /* For EM_DEFAULT case */
} marker; } marker;
int unique; /* UNIQUE */ int unique; /* UNIQUE */
int ref_cnt; /* reference count */
/* /*
* Whether automatic tagging may be applied for subtypes. * Whether automatic tagging may be applied for subtypes.
......
...@@ -28,6 +28,7 @@ asn1p_module_free(asn1p_module_t *mod) { ...@@ -28,6 +28,7 @@ asn1p_module_free(asn1p_module_t *mod) {
asn1p_expr_t *expr; asn1p_expr_t *expr;
free(mod->ModuleName); free(mod->ModuleName);
free(mod->source_file_name);
asn1p_oid_free(mod->module_oid); asn1p_oid_free(mod->module_oid);
......
...@@ -52,6 +52,7 @@ asn1p_oid_free(asn1p_oid_t *oid) { ...@@ -52,6 +52,7 @@ asn1p_oid_free(asn1p_oid_t *oid) {
while(oid->arcs_count--) { while(oid->arcs_count--) {
free(oid->arcs[oid->arcs_count].name); free(oid->arcs[oid->arcs_count].name);
} }
free(oid->arcs);
} }
free(oid); free(oid);
} }
......
...@@ -138,6 +138,7 @@ asn1p_value_fromtype(asn1p_expr_t *expr) { ...@@ -138,6 +138,7 @@ asn1p_value_fromtype(asn1p_expr_t *expr) {
if(v) { if(v) {
v->value.v_type = expr; v->value.v_type = expr;
v->type = ATV_TYPE; v->type = ATV_TYPE;
expr->ref_cnt++;
} }
return v; return v;
} }
...@@ -258,6 +259,7 @@ asn1p_value_free(asn1p_value_t *v) { ...@@ -258,6 +259,7 @@ asn1p_value_free(asn1p_value_t *v) {
asn1p_value_free(v->value.choice_identifier.value); asn1p_value_free(v->value.choice_identifier.value);
break; break;
} }
memset(v, 0, sizeof(*v));
free(v); free(v);
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment