fix: heap-use-after-free and memory leaks on erorr cases
This commit is contained in:
parent
f0b39535fb
commit
5740195cb3
4 changed files with 18 additions and 11 deletions
|
|
@ -238,16 +238,20 @@ struct ast *parse_input(struct lexer_context *ctx)
|
|||
}
|
||||
|
||||
struct ast *ast = parse_list(ctx);
|
||||
if (ast == NULL)
|
||||
return NULL;
|
||||
|
||||
token = PEEK_TOKEN();
|
||||
if (token->type == TOKEN_NEWLINE || token->type == TOKEN_EOF)
|
||||
|
||||
if (ast == NULL)
|
||||
{
|
||||
if (token->type == TOKEN_NEWLINE)
|
||||
if (token != NULL && token->type == TOKEN_EOF)
|
||||
{
|
||||
POP_TOKEN();
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (token->type == TOKEN_NEWLINE || token->type == TOKEN_EOF)
|
||||
{
|
||||
POP_TOKEN();
|
||||
return ast;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -329,12 +329,13 @@ struct ast *parse_if_rule(struct lexer_context *ctx)
|
|||
struct ast *condition_content = parse_compound_list(ctx);
|
||||
|
||||
// Then keyword
|
||||
token = POP_TOKEN();
|
||||
token = PEEK_TOKEN();
|
||||
if (token->type != TOKEN_THEN)
|
||||
{
|
||||
perror("Expected the 'then' keyword but token has different type");
|
||||
return err_if_rule(&condition_content, NULL, NULL);
|
||||
}
|
||||
POP_TOKEN();
|
||||
|
||||
// Then content
|
||||
struct ast *then_content = parse_compound_list(ctx);
|
||||
|
|
@ -344,6 +345,7 @@ struct ast *parse_if_rule(struct lexer_context *ctx)
|
|||
}
|
||||
|
||||
struct ast *else_content = NULL;
|
||||
token = PEEK_TOKEN();
|
||||
// Eventual else/elif clause(s)
|
||||
if (is_first(*token, RULE_ELSE_CLAUSE))
|
||||
{
|
||||
|
|
@ -355,12 +357,13 @@ struct ast *parse_if_rule(struct lexer_context *ctx)
|
|||
}
|
||||
|
||||
// Fi keyword
|
||||
token = POP_TOKEN();
|
||||
token = PEEK_TOKEN();
|
||||
if (token->type != TOKEN_FI)
|
||||
{
|
||||
perror("Expected the 'fi' keyword but token has different type");
|
||||
return err_if_rule(&condition_content, &then_content, &else_content);
|
||||
}
|
||||
POP_TOKEN();
|
||||
|
||||
// Result
|
||||
struct ast *result =
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue