Fix memory leaks

This commit is contained in:
bʰedoh₂ swé 2024-06-11 17:38:59 +05:00
parent b762ad154e
commit c543c00a9c
2 changed files with 24 additions and 20 deletions

View File

@ -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, "}}");
}

View File

@ -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;