From c543c00a9c45601780afc4e97e53dbb5653c4d4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?b=CA=B0edoh=E2=82=82=20sw=C3=A9?= Date: Tue, 11 Jun 2024 17:38:59 +0500 Subject: [PATCH] Fix memory leaks --- src/compile.c | 38 +++++++++++++++++++------------------- src/main.c | 6 +++++- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/compile.c b/src/compile.c index 7bcea2f..51eae14 100644 --- a/src/compile.c +++ b/src/compile.c @@ -97,7 +97,7 @@ void compile_genexec(char** result, tSyntaxElement* se){ if (token != NULL) { - string_append(result,compile_expression(token)); + string_append_free(result,compile_expression(token)); if (token->next != NULL) string_append(result,","); @@ -120,7 +120,7 @@ char* compile_gensplit(tSyntaxElement* se, char* operator) { if (token->next != NULL) { if (token->next->type == TOKEN) - string_append(&result,token->next->content.string); + string_append_free(&result,token->next->content.string); else string_append(&result,compile_expression_wrapped(token->next)); @@ -174,30 +174,30 @@ char* compile_expression(tSyntaxElement* syntaxelement) { if (strcmp(token->content.string, "if") == 0) { - string_append(&result,compile_expression_wrapped(token->next)); + string_append_free(&result,compile_expression_wrapped(token->next)); string_append(&result,"?"); - string_append(&result,compile_expression_wrapped(token->next->next)); + string_append_free(&result,compile_expression_wrapped(token->next->next)); string_append(&result,":"); - string_append(&result,compile_expression_wrapped(token->next->next->next)); + string_append_free(&result,compile_expression_wrapped(token->next->next->next)); } else if (strcmp(token->content.string, "get") == 0) { - string_append(&result,compile_expression(token->next)); + string_append_free(&result,compile_expression(token->next)); string_append(&result,"["); - string_append(&result,compile_expression(token->next->next)); + string_append_free(&result,compile_expression(token->next->next)); string_append(&result,"]"); } else if (strcmp(token->content.string, "neg") == 0) { string_append(&result,"-"); - string_append(&result,compile_expression_wrapped(token->next)); + string_append_free(&result,compile_expression_wrapped(token->next)); } else if (strcmp(token->content.string, "not") == 0) { string_append(&result,"not"); - string_append(&result,compile_expression_wrapped(token->next)); + string_append_free(&result,compile_expression_wrapped(token->next)); } else if (strcmp(token->content.string, "list") == 0) { @@ -211,9 +211,9 @@ char* compile_expression(tSyntaxElement* syntaxelement) { for (tSyntaxElement* i = token->next; i != NULL; i = i->next) { - string_append(&result, compile_expression(i->content.syntax)); + string_append_free(&result, compile_expression(i->content.syntax)); string_append(&result, ":"); - string_append(&result, compile_expression(i->content.syntax->next)); + string_append_free(&result, compile_expression(i->content.syntax->next)); if (i->next != NULL) string_append(&result,","); @@ -235,7 +235,7 @@ char* compile_expression(tSyntaxElement* syntaxelement) { case TREE: { tSyntaxElement* token = syntaxelement->content.syntax; - string_append(&result, compile_expression(syntaxelement->content.syntax)); + string_append_free(&result, compile_expression(syntaxelement->content.syntax)); compile_genexec(&result, token); break; } @@ -281,7 +281,7 @@ char* compile(tSyntaxElement* syntaxtree) { string_append(&result, "set "); string_append(&result,token->next->content.string); string_append(&result,"="); - string_append(&result,compile_expression_wrapped(token->next->next)); + string_append_free(&result,compile_expression_wrapped(token->next->next)); compile_exit_tag(&result); @@ -292,7 +292,7 @@ char* compile(tSyntaxElement* syntaxtree) { string_append(&result, "global "); string_append(&result,token->next->content.string); string_append(&result,"="); - string_append(&result,compile_expression_wrapped(token->next->next)); + string_append_free(&result,compile_expression_wrapped(token->next->next)); compile_exit_tag(&result); @@ -301,7 +301,7 @@ char* compile(tSyntaxElement* syntaxtree) { compile_enter_tag(&result); string_append(&result, "do "); - string_append(&result,compile_expression(token->next)); + string_append_free(&result,compile_expression(token->next)); compile_exit_tag(&result); @@ -310,11 +310,11 @@ char* compile(tSyntaxElement* syntaxtree) { compile_enter_tag(&result); string_append(&result, "if "); - string_append(&result,compile_expression(token->next)); + string_append_free(&result,compile_expression(token->next)); compile_exit_tag(&result); - string_append(&result,compile(token->next->next)); + string_append_free(&result,compile(token->next->next)); if (token->next->next->next != NULL) { @@ -322,7 +322,7 @@ char* compile(tSyntaxElement* syntaxtree) { string_append(&result, "else"); compile_exit_tag(&result); - string_append(&result,compile(token->next->next->next)); + string_append_free(&result,compile(token->next->next->next)); } @@ -337,7 +337,7 @@ char* compile(tSyntaxElement* syntaxtree) { for(tSyntaxElement* i = token->next; i != NULL; i = i->next) { string_append(&result, "{{"); - string_append(&result,compile_expression(i)); + string_append_free(&result,compile_expression(i)); string_append(&result, "}}"); } diff --git a/src/main.c b/src/main.c index 1445287..5aa3dba 100644 --- a/src/main.c +++ b/src/main.c @@ -33,7 +33,11 @@ int main(int argc, char *argv[]) { process(code, NULL); - printf("%s\n", compile(code)); + char* compiled = compile(code); + printf("%s\n", compiled); + + free(compiled); + compiled = NULL; se_free(code); code = NULL;