Skip to content

Commit

Permalink
Fix more leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Noam Preil committed Jun 18, 2020
1 parent 068073f commit bd221db
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 16 deletions.
1 change: 1 addition & 0 deletions assembler/assembler.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ void transform_relative_labels(tokenized_expression_t *expression, int last_rela
const char *fmtstring = "relative@%d";
int len = log10_u64(relative_label);
const int size = strlen(fmtstring) - 2 + len + 1;
free(token->symbol);
token->symbol = malloc(size);
if (snprintf(token->symbol, size, fmtstring, relative_label) != size - 1) {
scas_log(L_ERROR, "UNREACHABLE");
Expand Down
4 changes: 3 additions & 1 deletion assembler/directives.c
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,7 @@ int handle_equ(struct assembler_state *state, char **argv, int argc) {
return 1;
} else {
result = evaluate_expression(expression, state->equates, &error, &symbol);
free_expression(expression);
}
if (error == EXPRESSION_BAD_SYMBOL) {
ERROR(ERROR_UNKNOWN_SYMBOL, state->column, symbol);
Expand All @@ -747,7 +748,6 @@ int handle_equ(struct assembler_state *state, char **argv, int argc) {
list_add(state->equates, sym);
scas_log(L_DEBUG, "Added equate '%s' with value 0x%08X", sym->name, sym->value);
}
free_expression(expression);
return 1;
}

Expand Down Expand Up @@ -923,6 +923,7 @@ int handle_if(struct assembler_state *state, char **argv, int argc) {
return 1;
} else {
result = evaluate_expression(expression, state->equates, &error, &symbol);
free_expression(expression);
}
if (error == EXPRESSION_BAD_SYNTAX) {
ERROR(ERROR_INVALID_SYNTAX, state->column);
Expand Down Expand Up @@ -1239,6 +1240,7 @@ int handle_org(struct assembler_state *state, char **argv, int argc) {
return 1;
} else {
result = evaluate_expression(expression, state->equates, &error, &symbol);
free_expression(expression);
}
if (error == EXPRESSION_BAD_SYMBOL) {
ERROR(ERROR_UNKNOWN_SYMBOL, state->column, symbol);
Expand Down
3 changes: 1 addition & 2 deletions assembler/privatize.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ void rename_symbol(area_t *a, const char *original, const char *new) {
if (tok->type == SYMBOL) {
if (strcasecmp(tok->symbol, original) == 0) {
free(tok->symbol);
tok->symbol = malloc(strlen(new) + 1);
strcpy(tok->symbol, new);
tok->symbol = strdup(new);
}
}
}
Expand Down
22 changes: 9 additions & 13 deletions common/expression.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ uint64_t evaluate_expression(tokenized_expression_t *expression, list_t
*symbols, int *error, char **symbol) {
stack_type *stack = create_stack();
list_t *to_free = create_list();
expression_token_t *token;
expression_token_t *resolved;
operator_t op;
uint64_t res = 0;
Expand Down Expand Up @@ -158,7 +157,7 @@ uint64_t evaluate_expression(tokenized_expression_t *expression, list_t
if (stack->length == 0) {
*error = 1;
} else {
token = stack_pop(stack);
expression_token_t *token = stack_pop(stack);
if (token->type != NUMBER) {
*error = 1;
} else {
Expand Down Expand Up @@ -279,10 +278,7 @@ void free_expression(tokenized_expression_t *expression) {
free_expression_token((expression_token_t*)expression->tokens->items[i]);
}
list_free(expression->tokens);
for (int i = 0; i < expression->symbols->length; i += 1) {
free_expression_token((expression_token_t*)expression->symbols->items[i]);
}
list_free(expression->symbols);
free_flat_list(expression->symbols);
free(expression);
}

Expand Down Expand Up @@ -313,7 +309,7 @@ tokenized_expression_t *parse_expression(const char *str) {
break;
} else if (isdigit(*current) || *current == '\'') {
if (tokenizer_state == STATE_VALUE) {
free(list);
free_expression(list);
list = NULL;
goto exit;
}
Expand All @@ -326,7 +322,7 @@ tokenized_expression_t *parse_expression(const char *str) {
tokenizer_state = STATE_VALUE;
} else if (*current == '(') {
if (tokenizer_state == STATE_VALUE) {
free(list);
free_expression(list);
list = NULL;
goto exit;
}
Expand All @@ -338,7 +334,7 @@ tokenized_expression_t *parse_expression(const char *str) {
continue;
} else if (*current == ')') {
if (stack->length == 0 || tokenizer_state == STATE_OPERATOR) {
free(list);
free_expression(list);
list = NULL;
goto exit;
}
Expand All @@ -349,7 +345,7 @@ tokenized_expression_t *parse_expression(const char *str) {

if (stack->length <= 0) {
expr = 0;
free(list);
free_expression(list);
list = NULL;
goto exit;
}
Expand All @@ -365,14 +361,14 @@ tokenized_expression_t *parse_expression(const char *str) {
} else if(strchr(operator_cache, *current)) {
expr = parse_operator(&current, tokenizer_state == STATE_OPERATOR);
if (expr == 0) {
free(list);
free_expression(list);
list = NULL;
goto exit;
}
tokenizer_state = STATE_OPERATOR;
} else {
if (tokenizer_state == STATE_VALUE) {
free(list);
free_expression(list);
list = NULL;
goto exit;
}
Expand All @@ -382,7 +378,7 @@ tokenized_expression_t *parse_expression(const char *str) {
}

if (!expr) {
free(list);
free_expression(list);
list = NULL;
goto exit;
}
Expand Down

0 comments on commit bd221db

Please sign in to comment.