From 770d803d2bedefeb0bb4e41bf6efce21296c595d Mon Sep 17 00:00:00 2001 From: Kent Hansen Date: Sun, 7 Dec 2008 12:25:06 +0100 Subject: [PATCH] fix memleak with ifdef and ifndef --- astnode.c | 9 ++++++--- astproc.c | 12 ++++++------ tests/ifndef.asm | 4 ++++ 3 files changed, 16 insertions(+), 9 deletions(-) create mode 100644 tests/ifndef.asm diff --git a/astnode.c b/astnode.c index 3c658d8..8c44049 100644 --- a/astnode.c +++ b/astnode.c @@ -421,11 +421,13 @@ astnode *astnode_create(astnode_type type, location loc) */ void astnode_finalize(astnode *n) { + astnode *c; /* Remove the node from the tree it's in. */ astnode_remove(n); /* Finalize all its children recursively. */ - while (astnode_get_first_child(n) != NULL) { - astnode_finalize(astnode_remove_child_at(n, 0)); + while ((c = astnode_get_first_child(n)) != NULL) { + astnode_remove_child(n, c); + astnode_finalize(c); } /* Free up memory. */ switch (astnode_get_type(n)) { @@ -1534,7 +1536,6 @@ astnode *astnode_create_local_id(const char *s, location loc) astnode *astnode_create_list(astnode *l) { astnode *n; - location dummyloc; /* Create the node */ if (l != NULL) { n = astnode_create(LIST_NODE, l->loc); @@ -1543,6 +1544,8 @@ astnode *astnode_create_list(astnode *l) } else { /* Make a node with zero children */ + location dummyloc; + dummyloc.file = 0; n = astnode_create(LIST_NODE, dummyloc); } /* Return the newly created node (or NULL) */ diff --git a/astproc.c b/astproc.c index 203df55..f8f0cda 100644 --- a/astproc.c +++ b/astproc.c @@ -1843,7 +1843,7 @@ static int process_data(astnode *n, void *arg, astnode **next) /* If it's a string, replace by array of integers */ /* (makes it easier to process later... favour regularity) */ if (astnode_is_type(expr, STRING_NODE)) { - astnode_remove_child_at(n, j); /* Remove string */ + astnode_remove_child(n, expr); /* Remove string */ for (k=strlen(expr->string)-1; k>=0; k--) { /* Check if we should map character from custom charmap */ if (type->datatype == CHAR_DATATYPE) { @@ -2000,13 +2000,13 @@ static int process_ifdef(astnode *n, void *arg, astnode **next) if (e != NULL) { /* Symbol is defined. */ /* Replace IFDEF node by the true-branch statement list */ - stmts = astnode_remove_children( astnode_remove_child_at(n, 1)); + stmts = astnode_remove_children(astnode_get_child(n, 1)); astnode_replace(n, stmts); *next = stmts; } else { /* Symbol is not defined. */ /* Replace IFDEF node by the false-branch statement list (if any) */ - stmts = astnode_remove_children( astnode_remove_child_at(n, 2)); + stmts = astnode_remove_children( astnode_get_child(n, 2)); if (stmts != NULL) { astnode_replace(n, stmts); *next = stmts; @@ -2035,13 +2035,13 @@ static int process_ifndef(astnode *n, void *arg, astnode **next) if (e == NULL) { /* Symbol is not defined. */ /* Replace IFNDEF node by the true-branch statement list */ - stmts = astnode_remove_children( astnode_remove_child_at(n, 1)); + stmts = astnode_remove_children(astnode_get_child(n, 1)); astnode_replace(n, stmts); *next = stmts; } else { /* Symbol is defined. */ /* Replace IFNDEF node by the false-branch statement list, if any */ - stmts = astnode_remove_children( astnode_remove_child_at(n, 2)); + stmts = astnode_remove_children(astnode_get_child(n, 2)); if (stmts != NULL) { astnode_replace(n, stmts); *next = stmts; @@ -2080,7 +2080,7 @@ static int process_if(astnode *n, void *arg, astnode **next) /* Non-zero is true, zero is false */ if (expr->integer) { /* Replace IF node by the true-branch statement list */ - stmts = astnode_remove_children( astnode_remove_child_at(c, 1) ); + stmts = astnode_remove_children( astnode_get_child(c, 1) ); astnode_replace(n, stmts); astnode_finalize(n); *next = stmts; diff --git a/tests/ifndef.asm b/tests/ifndef.asm new file mode 100644 index 0000000..adf05b3 --- /dev/null +++ b/tests/ifndef.asm @@ -0,0 +1,4 @@ +.ifndef NOT_DEFINED +lda #10 +.endif +.end -- 2.11.4.GIT