From 2f47cbc3b6fa999fca3809b032a9be9ad1475f13 Mon Sep 17 00:00:00 2001 From: fennecdjay Date: Mon, 21 Oct 2019 22:25:53 +0200 Subject: [PATCH] :bomb: Split gwi_func_end to remove leaks --- src/lib/import.c | 54 ++++++++++++++++++++++------------- tests/import/fptr_tmpl_fail.c | 21 ++++++++++++++ tests/sh/import.sh | 2 +- 3 files changed, 56 insertions(+), 21 deletions(-) create mode 100644 tests/import/fptr_tmpl_fail.c diff --git a/src/lib/import.c b/src/lib/import.c index 6ff6896c..0cbb93fe 100644 --- a/src/lib/import.c +++ b/src/lib/import.c @@ -532,18 +532,6 @@ ANN static Func_Def make_dll_as_fun(const Gwi gwi, DL_Func * dl_fun, ae_flag fla return func_def; } -ANN2(1) static Func_Def template_fdef(const Gwi gwi, const m_str name, const ID_List list) { - const Arg_List arg_list = make_dll_arg_list(gwi, &gwi->func); - m_uint depth; - Type_Decl *td = str2decl(gwi->gwion->env, gwi->func.type, &depth, gwi->loc); - const Func_Def fdef = new_func_def(gwi->gwion->mp, new_func_base(gwi->gwion->mp, - td, insert_symbol(gwi->gwion->st, name), arg_list), NULL, ae_flag_builtin, loc_cpy(gwi->gwion->mp, gwi->loc)); - fdef->base->tmpl = new_tmpl(gwi->gwion->mp, list, -1); - fdef->d.dl_func_ptr = (void*)(m_uint)gwi->func.addr; - SET_FLAG(fdef, template | ae_flag_builtin); - return fdef; -} - ANN static m_bool section_fdef(const Gwi gwi, const Func_Def fdef) { Section* section = new_section_func_def(gwi->gwion->mp, fdef); const Class_Body body = new_class_body(gwi->gwion->mp, section, NULL); @@ -557,14 +545,29 @@ ANN static m_bool error_fdef(const Gwi gwi, const Func_Def fdef) { return GW_ERROR; } -ANN m_int gwi_func_end(const Gwi gwi, const ae_flag flag) { - const ID_List tmpl = tmpl_valid(gwi, gwi->func.name); - if(tmpl == (ID_List)GW_ERROR) - return GW_ERROR; - const m_str name = !tmpl ? gwi->func.name : strchr(gwi->func.name, '>') + 1; - CHECK_BB(name_valid(gwi, name)); - DECL_OB(Func_Def, fdef, = !tmpl ? - make_dll_as_fun(gwi, &gwi->func, flag) : template_fdef(gwi, name, tmpl)) +struct func_checker { + m_str name; + const ID_List tmpl; + const ae_flag flag; +}; + +ANN2(1) static Func_Def template_fdef(const Gwi gwi, const struct func_checker *ck) { + const Arg_List arg_list = make_dll_arg_list(gwi, &gwi->func); + m_uint depth; + Type_Decl *td = str2decl(gwi->gwion->env, gwi->func.type, &depth, gwi->loc); + const Func_Def fdef = new_func_def(gwi->gwion->mp, new_func_base(gwi->gwion->mp, + td, insert_symbol(gwi->gwion->st, ck->name), arg_list), NULL, ae_flag_builtin, loc_cpy(gwi->gwion->mp, gwi->loc)); + fdef->base->tmpl = new_tmpl(gwi->gwion->mp, ck->tmpl, -1); + fdef->d.dl_func_ptr = (void*)(m_uint)gwi->func.addr; + SET_FLAG(fdef, template | ae_flag_builtin); + return fdef; +} + +ANN m_int gwi_func_valid(const Gwi gwi, const struct func_checker *ck) { + const m_str name = !ck->tmpl ? gwi->func.name : strchr(gwi->func.name, '>') + 1; + CHECK_BB(name_valid(gwi, name)) + DECL_OB(Func_Def, fdef, = !ck->tmpl ? + make_dll_as_fun(gwi, &gwi->func, ck->flag) : template_fdef(gwi, ck)) if(gwi->gwion->env->class_def && GET_FLAG(gwi->gwion->env->class_def, template)) return section_fdef(gwi, fdef); if(traverse_func_def(gwi->gwion->env, fdef) < 0) @@ -572,6 +575,17 @@ ANN m_int gwi_func_end(const Gwi gwi, const ae_flag flag) { return GW_OK; } +ANN m_int gwi_func_end(const Gwi gwi, const ae_flag flag) { + const ID_List tmpl = tmpl_valid(gwi, gwi->func.name); + if(tmpl == (ID_List)GW_ERROR) + return GW_ERROR; + struct func_checker ck = { .tmpl=tmpl, .flag=flag }; + if(gwi_func_valid(gwi, &ck) > 0) + return GW_OK; + free_id_list(gwi->gwion->mp, tmpl); + return GW_ERROR; +} + ANN2(1,3) static Type _get_type(const Env env, const m_str str, const loc_t pos) { m_uint depth = 0; const ID_List list = (str && str != (m_str)OP_ANY_TYPE) ? str2list(env, str, &depth, pos) : NULL; diff --git a/tests/import/fptr_tmpl_fail.c b/tests/import/fptr_tmpl_fail.c new file mode 100644 index 00000000..696ed983 --- /dev/null +++ b/tests/import/fptr_tmpl_fail.c @@ -0,0 +1,21 @@ +#include "gwion_util.h" +#include "gwion_ast.h" +#include "oo.h" +#include "vm.h" +#include "env.h" +#include "type.h" +#include "object.h" +#include "instr.h" +#include "gwion.h" +#include "value.h" +#include "operator.h" +#include "import.h" + +static MFUN(test_func) { puts("test"); } +GWION_IMPORT(typedef_test) { + Type t_func_typedef; + GWI_OB((t_func_typedef = gwi_mk_type(gwi, "FuncTypedef", SZ_INT , NULL))) + GWI_BB(gwi_fptr_ini(gwi, "int", "<~A.~>test")) + GWI_OB(gwi_fptr_end(gwi, 0)) + return GW_OK; +} diff --git a/tests/sh/import.sh b/tests/sh/import.sh index 2221b7dd..921183dc 100644 --- a/tests/sh/import.sh +++ b/tests/sh/import.sh @@ -1,5 +1,5 @@ #!/bin/bash -# [test] #57 +# [test] #59 n=0 [ "$1" ] && n="$1" -- 2.43.0