From d048b633dd02014f8bff1b512ce4d5ae2b8b7903 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Fri, 25 Sep 2026 10:59:05 +0200 Subject: Fix move related memory leaks in AST helpers --- boot/ast.cc | 105 +++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 72 insertions(+), 33 deletions(-) (limited to 'boot/ast.cc') diff --git a/boot/ast.cc b/boot/ast.cc index 2d8e90d..cd2ac55 100644 --- a/boot/ast.cc +++ b/boot/ast.cc @@ -270,10 +270,10 @@ namespace elna::boot statement->branch().prerequisite().accept(this); traverse_block(statement->branch().body); - for (conditional_statements *branch : statement->branches) + for (conditional_statements& branch : statement->branches) { - branch->prerequisite().accept(this); - traverse_block(branch->body); + branch.prerequisite().accept(this); + traverse_block(branch.body); } if (statement->alternative != nullptr) { @@ -286,10 +286,10 @@ namespace elna::boot statement->branch().prerequisite().accept(this); traverse_block(statement->branch().body); - for (conditional_statements *branch : statement->branches) + for (conditional_statements& branch : statement->branches) { - branch->prerequisite().accept(this); - traverse_block(branch->body); + branch.prerequisite().accept(this); + traverse_block(branch.body); } } @@ -590,11 +590,6 @@ namespace elna::boot { } - attribute::attribute(attribute&& that) noexcept - : m_name(std::move(that.m_name)), m_arguments(std::move(that.m_arguments)) - { - } - attribute::~attribute() { for (const expression *argument : this->arguments()) @@ -863,17 +858,14 @@ namespace elna::boot } field_initializer::field_initializer(field_initializer&& other) noexcept - : m_name(std::move(other.m_name)), m_value(other.m_value) + : m_name(std::move(other.m_name)), m_value(std::exchange(other.m_value, nullptr)) { - other.m_value = nullptr; } field_initializer& field_initializer::operator=(field_initializer&& other) noexcept { - delete this->m_value; + std::swap(this->m_value, other.m_value); this->m_name = std::move(other.m_name); - this->m_value = other.m_value; - other.m_value = nullptr; return *this; } @@ -1078,6 +1070,15 @@ namespace elna::boot { } + block& block::operator=(block&& that) noexcept + { + std::swap(this->variables, that.variables); + std::swap(this->statements, that.statements); + std::swap(this->symbols, that.symbols); + + return *this; + } + block::~block() { for (const statement *body_statement : this->statements) @@ -1095,6 +1096,25 @@ namespace elna::boot { } + procedure_body::procedure_body(procedure_body&& that) noexcept + : block(std::move(that)), + return_expression(std::exchange(that.return_expression, nullptr)) + { + } + + procedure_body& procedure_body::operator=(procedure_body&& that) noexcept + { + block::operator=(std::move(that)); + std::swap(return_expression, that.return_expression); + + return *this; + } + + procedure_body::~procedure_body() + { + delete this->return_expression; + } + void traverse_body(parser_visitor *visitor, const procedure_body& body) { for (variable_declaration *variable : body.variables) @@ -1583,6 +1603,19 @@ namespace elna::boot { } + conditional_statements::conditional_statements(conditional_statements&& that) noexcept + : m_prerequisite(std::exchange(that.m_prerequisite, nullptr)), + body(std::move(that.body)) + { + } + + conditional_statements& conditional_statements::operator=(conditional_statements&& that) noexcept + { + std::swap(this->m_prerequisite, that.m_prerequisite); + body = std::move(that.body); + return *this; + } + expression& conditional_statements::prerequisite() { return *m_prerequisite; @@ -1593,6 +1626,27 @@ namespace elna::boot delete m_prerequisite; } + switch_case::switch_case(std::vector&& labels, block&& body) + : labels(std::move(labels)), body(std::move(body)) + { + } + + switch_case& switch_case::operator=(switch_case&& that) noexcept + { + std::swap(this->labels, that.labels); + std::swap(this->body, that.body); + + return *this; + } + + switch_case::~switch_case() + { + for (const expression *label : labels) + { + delete label; + } + } + case_statement::case_statement(const source_position position, expression *condition, std::vector&& cases, block *alternative) : node(position), m_condition(condition), cases(std::move(cases)), alternative(alternative) @@ -1612,13 +1666,6 @@ namespace elna::boot case_statement::~case_statement() { delete m_condition; - for (const switch_case& case_block : this->cases) - { - for (const expression *case_label : case_block.labels) - { - delete case_label; - } - } delete this->alternative; } @@ -1674,7 +1721,7 @@ namespace elna::boot } if_statement::if_statement(const source_position position, conditional_statements *branch, - std::vector&& branches, block *alternative) + std::vector&& branches, block *alternative) : node(position), m_branch(branch), branches(std::move(branches)), alternative(alternative) { } @@ -1692,10 +1739,6 @@ namespace elna::boot if_statement::~if_statement() { delete m_branch; - for (const conditional_statements *branch : branches) - { - delete branch; - } delete this->alternative; } @@ -1710,7 +1753,7 @@ namespace elna::boot } while_statement::while_statement(const source_position position, conditional_statements *branch, - std::vector&& branches) + std::vector&& branches) : node(position), m_branch(branch), branches(std::move(branches)) { } @@ -1728,10 +1771,6 @@ namespace elna::boot while_statement::~while_statement() { delete m_branch; - for (const conditional_statements *branch : branches) - { - delete branch; - } } repeat_statement::repeat_statement(const source_position position, block&& body, -- cgit v1.2.3