Merge pull request #126 from brchiu/fix_some_leak_reported_by_valgrind

Fix many memory leaks reported by valgrind
This commit is contained in:
Lev Walkin 2017-03-26 05:55:00 -07:00 committed by GitHub
commit fbbe4edc8e
12 changed files with 102 additions and 5 deletions

View File

@ -330,6 +330,8 @@ main(int ac, char **av) {
exit(EX_SOFTWARE);
}
asn1p_delete(asn);
return 0;
}
@ -423,6 +425,11 @@ importStandardModules(asn1p_t *asn, const char *skeletons_dir) {
closedir(dir);
#endif
#ifdef _WIN32
free(pattern);
#endif
free(target_dir);
return ret;
}

View File

@ -507,6 +507,8 @@ asn1c_lang_C_type_SEQUENCE_def(arg_t *arg) {
REDIR(saved_target);
if(tag2el) free(tag2el);
return 0;
} /* _SEQUENCE_def() */
@ -741,6 +743,9 @@ asn1c_lang_C_type_SET_def(arg_t *arg) {
REDIR(saved_target);
if (tag2el) free(tag2el);
if (tag2el_cxer) free(tag2el_cxer);
return 0;
} /* _SET_def() */
@ -874,6 +879,8 @@ asn1c_lang_C_type_SEx_OF_def(arg_t *arg, int seq_of) {
arg->embed++;
emit_member_table(arg, v);
arg->embed--;
free(v->Identifier);
v->Identifier = (char *)NULL;
INDENT(-1);
OUT("};\n");
@ -1113,6 +1120,8 @@ asn1c_lang_C_type_CHOICE_def(arg_t *arg) {
REDIR(saved_target);
if (tag2el) free(tag2el);
return 0;
} /* _CHOICE_def() */

View File

@ -35,6 +35,7 @@ asn1c_emit_constraint_checking_code(arg_t *arg) {
int alphabet_table_compiled;
int produce_st = 0;
int ulong_optimize = 0;
int ret = 0;
ct = expr->combined_constraints;
if(ct == NULL)
@ -158,7 +159,7 @@ asn1c_emit_constraint_checking_code(arg_t *arg) {
OUT("\n");
OUT("/* Constraint check succeeded */\n");
OUT("return 0;\n");
return 0;
goto end;
}
/*
@ -197,7 +198,8 @@ asn1c_emit_constraint_checking_code(arg_t *arg) {
INDENTED(OUT("/* Nothing is here. See below */\n"));
OUT("}\n");
OUT("\n");
return 1;
ret = 1;
goto end;
}
INDENT(-1);
OUT(") {\n");
@ -222,7 +224,11 @@ asn1c_emit_constraint_checking_code(arg_t *arg) {
INDENT(-1);
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

View File

@ -140,6 +140,16 @@ asn1c_new_dep(const char *filename) {
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
asn1c_dep_add(asn1c_fdeps_t *deps, asn1c_fdeps_t *d) {
int n;
@ -180,7 +190,9 @@ asn1c_deps_makelist(asn1c_fdeps_t *deps) {
if(deps->filename && deps->usage != FDEP_NOTUSED) {
d = asn1c_new_dep(deps->filename);
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++) {
@ -188,10 +200,28 @@ asn1c_deps_makelist(asn1c_fdeps_t *deps) {
d = asn1c_deps_makelist(deps->elements[i]);
assert(!d->filename);
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;
}
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);
}
}

View File

@ -25,5 +25,6 @@ int asn1c_activate_dependency(asn1c_fdeps_t *deps, asn1c_fdeps_t *cur,
const char *data);
asn1c_fdeps_t *asn1c_deps_makelist(asn1c_fdeps_t *deps);
void asn1c_deps_freelist(asn1c_fdeps_t *deps);
#endif /* ASN1C_FDEPS_H */

View File

@ -149,6 +149,9 @@ asn1c_save_compiled_output(arg_t *arg, const char *datadir,
safe_fprintf(mkf, "ASN_%s_%s+=%s\n",
what_class, what_kind, fname);
}
asn1c_deps_freelist(deps);
asn1c_deps_freelist(dlist);
}
if(need_to_generate_pdu_collection(arg)) {

View File

@ -6,6 +6,7 @@
static void default_logger_cb(int, const char *fmt, ...);
static int asn1c_compile_expr(arg_t *arg);
static int asn1c_attach_streams(asn1p_expr_t *expr);
static int asn1c_detach_streams(asn1p_expr_t *expr);
int
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))
return -1;
TQ_FOR(mod, &(asn->modules), mod_next) {
TQ_FOR(arg->expr, &(mod->members), next) {
asn1c_detach_streams(arg->expr);
}
}
return 0;
}
@ -159,6 +166,28 @@ asn1c_attach_streams(asn1p_expr_t *expr) {
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
default_logger_cb(int _severity, const char *fmt, ...) {
va_list ap;

View File

@ -22,6 +22,7 @@ asn1p_expr_new(int _lineno, asn1p_module_t *mod) {
expr->spec_index = -1;
expr->module = mod;
expr->_lineno = _lineno;
expr->ref_cnt = 0;
}
return expr;
@ -235,6 +236,12 @@ asn1p_expr_free(asn1p_expr_t *expr) {
if(expr) {
asn1p_expr_t *tm;
if (expr->ref_cnt) {
/* Decrease reference count only */
expr->ref_cnt--;
return;
}
/* Remove all children */
while((tm = TQ_REMOVE(&(expr->members), next))) {
if(tm->parent_expr != expr)

View File

@ -216,6 +216,7 @@ typedef struct asn1p_expr_s {
asn1p_value_t *default_value; /* For EM_DEFAULT case */
} marker;
int unique; /* UNIQUE */
int ref_cnt; /* reference count */
/*
* Whether automatic tagging may be applied for subtypes.

View File

@ -28,6 +28,7 @@ asn1p_module_free(asn1p_module_t *mod) {
asn1p_expr_t *expr;
free(mod->ModuleName);
free(mod->source_file_name);
asn1p_oid_free(mod->module_oid);

View File

@ -52,6 +52,7 @@ asn1p_oid_free(asn1p_oid_t *oid) {
while(oid->arcs_count--) {
free(oid->arcs[oid->arcs_count].name);
}
free(oid->arcs);
}
free(oid);
}

View File

@ -138,6 +138,7 @@ asn1p_value_fromtype(asn1p_expr_t *expr) {
if(v) {
v->value.v_type = expr;
v->type = ATV_TYPE;
expr->ref_cnt++;
}
return v;
}
@ -258,6 +259,7 @@ asn1p_value_free(asn1p_value_t *v) {
asn1p_value_free(v->value.choice_identifier.value);
break;
}
memset(v, 0, sizeof(*v));
free(v);
}
}