3
0
Fork 0
mirror of https://github.com/YosysHQ/yosys synced 2025-06-29 09:28:46 +00:00

verilog: fix parser "if" memory errors.

Fix buggy memory allocation introduced in #5152:

1) clean up ast_stack to reflect AST node rearrangement when necessary,
to avoid dangling pointer;
2) call free_attr() on unused attribute list when no new syntax node is
created, to avoid leaking it.
This commit is contained in:
Gary Wong 2025-06-20 17:26:20 -06:00 committed by Zachary Snow
parent 170933ecb0
commit 34a2abeddb

View file

@ -2874,6 +2874,7 @@ behavioral_stmt:
} | } |
if_attr TOK_IF '(' expr ')' { if_attr TOK_IF '(' expr ')' {
AstNode *node = 0; AstNode *node = 0;
AstNode *block = new AstNode(AST_BLOCK);
AstNode *context = ast_stack.back(); AstNode *context = ast_stack.back();
if (context && context->type == AST_BLOCK && context->get_bool_attribute(ID::promoted_if)) { if (context && context->type == AST_BLOCK && context->get_bool_attribute(ID::promoted_if)) {
AstNode *outer = ast_stack[ast_stack.size() - 2]; AstNode *outer = ast_stack[ast_stack.size() - 2];
@ -2882,8 +2883,10 @@ behavioral_stmt:
// parallel "else if": append condition to outer "if" // parallel "else if": append condition to outer "if"
node = outer; node = outer;
log_assert (node->children.size()); log_assert (node->children.size());
ast_stack.pop_back();
delete node->children.back(); delete node->children.back();
node->children.pop_back(); node->children.pop_back();
ast_stack.push_back(block);
} else if (outer->get_bool_attribute(ID::full_case)) } else if (outer->get_bool_attribute(ID::full_case))
(*$1)[ID::full_case] = AstNode::mkconst_int(1, false); (*$1)[ID::full_case] = AstNode::mkconst_int(1, false);
} }
@ -2894,8 +2897,8 @@ behavioral_stmt:
append_attr(node, $1); append_attr(node, $1);
ast_stack.back()->children.push_back(node); ast_stack.back()->children.push_back(node);
node->children.push_back(node->get_bool_attribute(ID::parallel_case) ? AstNode::mkconst_int(1, false, 1) : expr); node->children.push_back(node->get_bool_attribute(ID::parallel_case) ? AstNode::mkconst_int(1, false, 1) : expr);
} } else
AstNode *block = new AstNode(AST_BLOCK); free_attr($1);
AstNode *cond = new AstNode(AST_COND, node->get_bool_attribute(ID::parallel_case) ? expr : AstNode::mkconst_int(1, false, 1), block); AstNode *cond = new AstNode(AST_COND, node->get_bool_attribute(ID::parallel_case) ? expr : AstNode::mkconst_int(1, false, 1), block);
SET_AST_NODE_LOC(cond, @4, @4); SET_AST_NODE_LOC(cond, @4, @4);
node->children.push_back(cond); node->children.push_back(cond);