Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix issues detected by address sanitizer #19

Merged
merged 5 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ class FilterUnit
public:
FilterUnit() = default;
~FilterUnit()
{}
{
// FilterUnit 只会在 create_filter_unit 中创建,这里的左右表达式都是独占所有权,需要释放
delete left_expr_;
delete right_expr_;
}

void set_comp(CompOp comp)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class UpdateStmt : public Stmt
{
public:
UpdateStmt(Table *table, std::vector<UpdateUnit> update_units, FilterStmt *filter_stmt);
~UpdateStmt();

public:
static RC create(Db *db, const UpdateSqlNode &update_sql, Stmt *&stmt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class UpdateLogicalNode : public LogicalNode
{
public:
UpdateLogicalNode(Table *table, std::vector<UpdateUnit> update_units);
~UpdateLogicalNode() override = default;
~UpdateLogicalNode() override;

LogicalNodeType type() const override
{
Expand All @@ -19,7 +19,7 @@ class UpdateLogicalNode : public LogicalNode
Table *table() const {
return table_;
}
std::vector<UpdateUnit> update_units() {
std::vector<UpdateUnit> &update_units() {
return update_units_;
}
private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class UpdatePhysicalOperator : public PhysicalOperator
UpdatePhysicalOperator(Table *table, std::vector<UpdateUnit> update_units) : table_(table), update_units_(std::move(update_units))
{}

virtual ~UpdatePhysicalOperator() = default;
virtual ~UpdatePhysicalOperator();

PhysicalOperatorType type() const override
{
Expand Down
2 changes: 2 additions & 0 deletions src/server/include/query_engine/structor/tuple/tuple_cell.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class TupleCellSpec
TupleCellSpec(const char *table_name, const char *field_name, const char *alias = nullptr);
explicit TupleCellSpec(const char *alias);

~TupleCellSpec();

const char *table_name() const {
return table_name_.c_str();
}
Expand Down
1 change: 1 addition & 0 deletions src/server/include/storage_engine/buffer/buffer_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class FileBufferPool
* 将dirty frame中的数据刷新到磁盘上
*/
RC flush_page(Frame &frame);
RC flush_all_pages();

/**
* 驱逐frame
Expand Down
17 changes: 16 additions & 1 deletion src/server/query_engine/analyzer/statement/select_stmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,25 @@

SelectStmt::~SelectStmt()
{
// 这些都是 create 中创建的对象,独占所有权,需要释放
for (auto *field : query_fields_) {
delete field;
}
for (auto *project : projects_) {
delete project;
}

if (nullptr != filter_stmt_) {
delete filter_stmt_;
filter_stmt_ = nullptr;
}

delete group_by_stmt_;
delete having_stmt_;
delete order_stmt_;
for (auto *join_filter_stmt : join_filter_stmts_) {
delete join_filter_stmt;
}
}

static void wildcard_fields_without_table_name(Table *table, std::vector<Expression*> &projects, const std::string &table_name) {
Expand Down Expand Up @@ -345,7 +360,7 @@ RC SelectStmt::create(Db *db, const SelectSqlNode &select_sql, Stmt *&stmt)
select_stmt->group_by_stmt_ = group_by_stmt;
select_stmt->having_stmt_ = having_stmt;
select_stmt->order_stmt_ = order_stmt;
select_stmt->join_filter_stmts_ = join_filter_stmts;
select_stmt->join_filter_stmts_.swap(join_filter_stmts);
stmt = select_stmt;
return RC::SUCCESS;
}
9 changes: 9 additions & 0 deletions src/server/query_engine/analyzer/statement/update_stmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
UpdateStmt::UpdateStmt(Table *table, std::vector<UpdateUnit> update_units, FilterStmt *filter_stmt) : table_(table), update_units_(std::move(update_units)), filter_stmt_(filter_stmt)
{}

UpdateStmt::~UpdateStmt()
{
// 这些都是 create 中创建的对象,独占所有权,需要释放
delete filter_stmt_;
for (auto &unit : update_units_) {
delete unit.value;
}
}

RC UpdateStmt::create(Db *db, const UpdateSqlNode &update, Stmt *&stmt)
{
const char *table_name = update.relation_name.c_str();
Expand Down
1 change: 1 addition & 0 deletions src/server/query_engine/executor/execution_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ RC write_to_communicator(const char* data, int32_t size, Communicator* communica
LOG_WARN("failed to send data to client. err=%s", strerror(errno));
return rc;
}
delete[] padding;
}
RC rc = communicator->write_result(data, size);
if(RC_FAIL(rc)){
Expand Down
16 changes: 8 additions & 8 deletions src/server/query_engine/parser/yacc_sql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2003,7 +2003,7 @@ YYLTYPE yylloc = yyloc_default;
(yyval.multi_attribute_names) = new std::vector<std::string>;
}
(yyval.multi_attribute_names)->emplace_back((yyvsp[-1].string));
delete (yyvsp[-1].string);
free((yyvsp[-1].string));
}
#line 2009 "yacc_sql.cpp"
break;
Expand Down Expand Up @@ -2768,7 +2768,7 @@ YYLTYPE yylloc = yyloc_default;
relAttrSqlNode->relation_name = (yyvsp[-3].string);
relAttrSqlNode->attribute_name = "*";
(yyval.expression_list)->emplace_back(new RelAttrExpr(*relAttrSqlNode));
delete (yyvsp[-3].string);
free((yyvsp[-3].string));
}
#line 2774 "yacc_sql.cpp"
break;
Expand Down Expand Up @@ -2837,7 +2837,7 @@ YYLTYPE yylloc = yyloc_default;
relAttrSqlNode->relation_name = (yyvsp[-3].string);
relAttrSqlNode->attribute_name = "*";
(yyval.expression_list)->emplace_back(new RelAttrExpr(*relAttrSqlNode));
delete (yyvsp[-3].string);
free((yyvsp[-3].string));
}
#line 2843 "yacc_sql.cpp"
break;
Expand Down Expand Up @@ -2907,7 +2907,7 @@ YYLTYPE yylloc = yyloc_default;
(yyval.rel_attr) = new RelAttrSqlNode;
(yyval.rel_attr)->relation_name = "";
(yyval.rel_attr)->attribute_name = (yyvsp[0].string);
delete (yyvsp[0].string);
free((yyvsp[0].string));
}
#line 2913 "yacc_sql.cpp"
break;
Expand All @@ -2918,8 +2918,8 @@ YYLTYPE yylloc = yyloc_default;
(yyval.rel_attr) = new RelAttrSqlNode;
(yyval.rel_attr)->relation_name = (yyvsp[-2].string);
(yyval.rel_attr)->attribute_name = (yyvsp[0].string);
delete (yyvsp[-2].string);
delete (yyvsp[0].string);
free((yyvsp[-2].string));
free((yyvsp[0].string));
}
#line 2925 "yacc_sql.cpp"
break;
Expand Down Expand Up @@ -2957,7 +2957,7 @@ YYLTYPE yylloc = yyloc_default;
(yyval.relation_list) = new std::vector<RelationSqlNode>;
}
(yyval.relation_list)->push_back(*(yyvsp[-1].relation));
free((yyvsp[-1].relation));
delete (yyvsp[-1].relation);
}
#line 2963 "yacc_sql.cpp"
break;
Expand Down Expand Up @@ -3044,7 +3044,7 @@ YYLTYPE yylloc = yyloc_default;
delete (yyvsp[-1].condition_list);
(yyval.join_list)->emplace_back(*joinSqlNode);
delete joinSqlNode;
free((yyvsp[-2].relation));
delete (yyvsp[-2].relation);
}
#line 3050 "yacc_sql.cpp"
break;
Expand Down
16 changes: 8 additions & 8 deletions src/server/query_engine/parser/yacc_sql.y
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ multi_attribute_names:
$$ = new std::vector<std::string>;
}
$$->emplace_back($2);
delete $2;
free($2);
}
;

Expand Down Expand Up @@ -869,7 +869,7 @@ select_attr:
relAttrSqlNode->relation_name = $1;
relAttrSqlNode->attribute_name = "*";
$$->emplace_back(new RelAttrExpr(*relAttrSqlNode));
delete $1;
free($1);
} | add_expr expression_list {
if ($2 != nullptr) {
$$ = $2;
Expand Down Expand Up @@ -912,7 +912,7 @@ expression_list:
relAttrSqlNode->relation_name = $2;
relAttrSqlNode->attribute_name = "*";
$$->emplace_back(new RelAttrExpr(*relAttrSqlNode));
delete $2;
free($2);
} | COMMA add_expr expression_list {
if ($3 != nullptr) {
$$ = $3;
Expand Down Expand Up @@ -956,13 +956,13 @@ rel_attr:
$$ = new RelAttrSqlNode;
$$->relation_name = "";
$$->attribute_name = $1;
delete $1;
free($1);
} | ID DOT ID {
$$ = new RelAttrSqlNode;
$$->relation_name = $1;
$$->attribute_name = $3;
delete $1;
delete $3;
free($1);
free($3);
}
;

Expand All @@ -989,7 +989,7 @@ relation_list:
$$ = new std::vector<RelationSqlNode>;
}
$$->push_back(*$1);
free($1);
delete $1;
}
;

Expand Down Expand Up @@ -1048,7 +1048,7 @@ join_list:
delete $4;
$$->emplace_back(*joinSqlNode);
delete joinSqlNode;
free($3);
delete $3;
}
;

Expand Down
10 changes: 9 additions & 1 deletion src/server/query_engine/planner/node/update_logical_node.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
#include "include/query_engine/planner/node/update_logical_node.h"

UpdateLogicalNode::UpdateLogicalNode(Table *table, std::vector<UpdateUnit> update_units) : table_(table), update_units_(std::move(update_units))
{}
{}

UpdateLogicalNode::~UpdateLogicalNode()
{
// update_units_ 在 plan_node 中 copy 了一份,这里需要释放
for (auto &unit : update_units_) {
delete unit.value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ RC PhysicalOperatorGenerator::create_plan(

auto *project_operator = new ProjectPhysicalOperator(&project_oper);
for (const auto &i : project_oper.expressions()) {
project_operator->add_projector(i->copy());
// TupleCellSpec 的构造函数中已经 copy 了 Expression,这里无需 copy
project_operator->add_projector(i.get());
}

if (child_phy_oper) {
Expand Down Expand Up @@ -285,7 +286,8 @@ RC PhysicalOperatorGenerator::create_plan(UpdateLogicalNode &update_oper, unique
}
}

oper = unique_ptr<PhysicalOperator>(new UpdatePhysicalOperator(update_oper.table(), update_oper.update_units()));
// 将 update_units 从逻辑算子转移给物理算子,避免重复释放
oper = unique_ptr<PhysicalOperator>(new UpdatePhysicalOperator(update_oper.table(), std::move(update_oper.update_units())));

if (child_physical_oper) {
oper->add_child(std::move(child_physical_oper));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
#include "include/query_engine/structor/expression/value_expression.h"
#include "include/query_engine/structor/tuple/row_tuple.h"

UpdatePhysicalOperator::~UpdatePhysicalOperator()
{
// 这些 update_units 从逻辑算子中转移过来,独占所有权,需要释放
for (auto &unit : update_units_) {
delete unit.value;
}
}

RC UpdatePhysicalOperator::open(Trx *trx)
{
if (children_.empty()) {
Expand Down Expand Up @@ -84,6 +92,9 @@ RC UpdatePhysicalOperator::open(Trx *trx)
unit.value = new ValueExpr(value);
processed_update_units.emplace_back(unit);
}
for (auto &unit : update_units_) {
delete unit.value;
}
update_units_.clear();
update_units_ = std::move(processed_update_units);
return RC::SUCCESS;
Expand Down
6 changes: 6 additions & 0 deletions src/server/query_engine/structor/tuple/tuple_cell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@ TupleCellSpec::TupleCellSpec(const char *alias)
alias_ = alias;
}
}

TupleCellSpec::~TupleCellSpec()
{
// 构造时 copy 了一份 expression,这里需要释放
delete expression_;
}
2 changes: 2 additions & 0 deletions src/server/session/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ int Server::start_stdin_server()

/// 在当前线程立即处理对应的事件
bool need_disconnect = query_engine_.process_session_request(event);
// event 对象在 read_event 中创建,需要在这里释放
delete event;
if(need_disconnect){
// 函数最后已经做了清理工作,且 stdin server 不需要创建 read_event
// 所以此处无需调用 close_connection,直接退出循环即可
Expand Down
20 changes: 20 additions & 0 deletions src/server/storage_engine/buffer/buffer_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,26 @@ RC FileBufferPool::flush_page_internal(Frame &frame)
return RC::SUCCESS;
}

/**
* 将所有的脏页刷盘
*/
RC FileBufferPool::flush_all_pages()
{
std::scoped_lock lock_guard(lock_);
RC rc = RC::SUCCESS;
for (Frame *frame : frame_manager_.find_list(file_desc_)) {
if (frame->dirty()) {
RC _rc = flush_page_internal(*frame);
if (_rc != RC::SUCCESS) {
LOG_ERROR("Failed to flush page %s:%d, rc=%s", file_name_.c_str(), frame->page_num(), strrc(_rc));
rc = _rc;
}
}
frame->unpin();
}
return rc;
}

/**
* TODO [Lab1] 需要同学们实现某个指定页面的驱逐
*/
Expand Down
Loading