From 12021b53b8d626bc05393fcb82c39d067c2fbdfa Mon Sep 17 00:00:00 2001 From: "Bi-Ruei, Chiu" Date: Fri, 4 Nov 2016 11:59:45 +0800 Subject: [PATCH 1/7] Free memory pointed by tag2el, tag2el_cxer and source_file_name --- libasn1compiler/asn1c_C.c | 7 +++++++ libasn1parser/asn1p_module.c | 1 + 2 files changed, 8 insertions(+) diff --git a/libasn1compiler/asn1c_C.c b/libasn1compiler/asn1c_C.c index b4cd736c..dc74bc81 100644 --- a/libasn1compiler/asn1c_C.c +++ b/libasn1compiler/asn1c_C.c @@ -479,6 +479,8 @@ asn1c_lang_C_type_SEQUENCE_def(arg_t *arg) { REDIR(OT_TYPE_DECLS); + if(tag2el) free(tag2el); + return 0; } /* _SEQUENCE_def() */ @@ -694,6 +696,9 @@ asn1c_lang_C_type_SET_def(arg_t *arg) { REDIR(OT_TYPE_DECLS); + if (tag2el) free(tag2el); + if (tag2el_cxer) free(tag2el_cxer); + return 0; } /* _SET_def() */ @@ -1028,6 +1033,8 @@ asn1c_lang_C_type_CHOICE_def(arg_t *arg) { REDIR(OT_TYPE_DECLS); + if (tag2el) free(tag2el); + return 0; } /* _CHOICE_def() */ diff --git a/libasn1parser/asn1p_module.c b/libasn1parser/asn1p_module.c index e7afb056..95ac50de 100644 --- a/libasn1parser/asn1p_module.c +++ b/libasn1parser/asn1p_module.c @@ -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); From 9b78083824074fc7b49904da27925cecefbe29fb Mon Sep 17 00:00:00 2001 From: "Bi-Ruei, Chiu" Date: Mon, 7 Nov 2016 22:19:10 +0800 Subject: [PATCH 2/7] Free memory allocated in asn1c_compiled_output() and asn1c_lang_C_type_SEx_OF_def() --- libasn1compiler/asn1c_C.c | 2 ++ libasn1compiler/asn1compiler.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/libasn1compiler/asn1c_C.c b/libasn1compiler/asn1c_C.c index dc74bc81..246dd661 100644 --- a/libasn1compiler/asn1c_C.c +++ b/libasn1compiler/asn1c_C.c @@ -813,6 +813,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"); diff --git a/libasn1compiler/asn1compiler.c b/libasn1compiler/asn1compiler.c index 18b2c008..42d55e69 100644 --- a/libasn1compiler/asn1compiler.c +++ b/libasn1compiler/asn1compiler.c @@ -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; From 64505efde45750e5c545869e74f2f7f11d9e28c6 Mon Sep 17 00:00:00 2001 From: "Bi-Ruei, Chiu" Date: Tue, 8 Nov 2016 13:59:29 +0800 Subject: [PATCH 3/7] Free memory pointed by r_value and r_size --- libasn1compiler/asn1c_constraint.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/libasn1compiler/asn1c_constraint.c b/libasn1compiler/asn1c_constraint.c index 477edf2d..e67590a4 100644 --- a/libasn1compiler/asn1c_constraint.c +++ b/libasn1compiler/asn1c_constraint.c @@ -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 From 6f34894ca3b14238150564a0a14e88a270ca75ad Mon Sep 17 00:00:00 2001 From: "Bi-Ruei, Chiu" Date: Tue, 8 Nov 2016 15:41:23 +0800 Subject: [PATCH 4/7] Free memory allocated in asn1c_read_file_dependencies() and asn1c_deps_makelist() --- libasn1compiler/asn1c_fdeps.c | 34 ++++++++++++++++++++++++++++++++-- libasn1compiler/asn1c_fdeps.h | 1 + libasn1compiler/asn1c_save.c | 3 +++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/libasn1compiler/asn1c_fdeps.c b/libasn1compiler/asn1c_fdeps.c index 231a47cc..1ecc313f 100644 --- a/libasn1compiler/asn1c_fdeps.c +++ b/libasn1compiler/asn1c_fdeps.c @@ -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); + } +} diff --git a/libasn1compiler/asn1c_fdeps.h b/libasn1compiler/asn1c_fdeps.h index 7986e34f..cb5b12cf 100644 --- a/libasn1compiler/asn1c_fdeps.h +++ b/libasn1compiler/asn1c_fdeps.h @@ -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 */ diff --git a/libasn1compiler/asn1c_save.c b/libasn1compiler/asn1c_save.c index f8348cc6..4a422b5b 100644 --- a/libasn1compiler/asn1c_save.c +++ b/libasn1compiler/asn1c_save.c @@ -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)) { From 0bfcefc368c7f5f0379d123af340c882c44f74d0 Mon Sep 17 00:00:00 2001 From: "Bi-Ruei, Chiu" Date: Tue, 8 Nov 2016 22:37:39 +0800 Subject: [PATCH 5/7] Free memory pointed by oid->arcs --- libasn1parser/asn1p_oid.c | 1 + 1 file changed, 1 insertion(+) diff --git a/libasn1parser/asn1p_oid.c b/libasn1parser/asn1p_oid.c index 80d41e2d..14b96aa9 100644 --- a/libasn1parser/asn1p_oid.c +++ b/libasn1parser/asn1p_oid.c @@ -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); } From b9adfc5f91f56ebabbef4fc85fef92608209bb21 Mon Sep 17 00:00:00 2001 From: "Bi-Ruei, Chiu" Date: Wed, 9 Nov 2016 00:17:25 +0800 Subject: [PATCH 6/7] Free memory allocated in various functions 1. Add 'ref_cnt' field to asn1p_expr_t. 2. Initialize 'ref_cnt' field to zero when asn1p_expr_t is allocated. 3. Increase 'ref_cnt' field when asn1p_expr_t is cloned by asn1p_value_fromtype(). 4. If 'ref_cnt' field of asn1p_expr_t is larger than zero, then asn1p_expr_free() only decrease its value. 5. Free memory pointed by fields of asn1p_expr_t and itself when 'ref_cnt' is zero and asn1p_expr_free() called. 6. Call asn1p_delete(asn) in main(). --- asn1c/asn1c.c | 2 ++ libasn1parser/asn1p_expr.c | 7 +++++++ libasn1parser/asn1p_expr.h | 1 + libasn1parser/asn1p_value.c | 2 ++ 4 files changed, 12 insertions(+) diff --git a/asn1c/asn1c.c b/asn1c/asn1c.c index 8a6f785e..2f5194c6 100644 --- a/asn1c/asn1c.c +++ b/asn1c/asn1c.c @@ -330,6 +330,8 @@ main(int ac, char **av) { exit(EX_SOFTWARE); } + asn1p_delete(asn); + return 0; } diff --git a/libasn1parser/asn1p_expr.c b/libasn1parser/asn1p_expr.c index b7a03ecb..c2261e67 100644 --- a/libasn1parser/asn1p_expr.c +++ b/libasn1parser/asn1p_expr.c @@ -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) diff --git a/libasn1parser/asn1p_expr.h b/libasn1parser/asn1p_expr.h index 89d939a0..dfeaa25d 100644 --- a/libasn1parser/asn1p_expr.h +++ b/libasn1parser/asn1p_expr.h @@ -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. diff --git a/libasn1parser/asn1p_value.c b/libasn1parser/asn1p_value.c index 63c80c7e..0c898880 100644 --- a/libasn1parser/asn1p_value.c +++ b/libasn1parser/asn1p_value.c @@ -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); } } From 4661dae4157cd4e24eff474e2cb234dee8d17260 Mon Sep 17 00:00:00 2001 From: "Bi-Ruei, Chiu" Date: Wed, 9 Nov 2016 00:59:41 +0800 Subject: [PATCH 7/7] Free memory allocated in importStandardModules() --- asn1c/asn1c.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/asn1c/asn1c.c b/asn1c/asn1c.c index 2f5194c6..5a52cea4 100644 --- a/asn1c/asn1c.c +++ b/asn1c/asn1c.c @@ -425,6 +425,11 @@ importStandardModules(asn1p_t *asn, const char *skeletons_dir) { closedir(dir); #endif +#ifdef _WIN32 + free(pattern); +#endif + free(target_dir); + return ret; }