From b08ea5bcac9aa82af1015625922defd8e9ae6ad3 Mon Sep 17 00:00:00 2001 From: steppenwolfyuetong Date: Mon, 13 May 2019 15:13:13 +0800 Subject: [PATCH] Fixed potential buffer corruption in toString of serveral AST (#345) * fix toString function defect * address dangleptr's comments * address dutor's comments --- src/parser/AdminSentences.cpp | 8 ++++++-- src/parser/Clauses.cpp | 8 ++++++-- src/parser/MaintainSentences.cpp | 28 ++++++++++++++++++++-------- src/parser/MutateSentences.cpp | 29 +++++++++++++++++++++-------- 4 files changed, 53 insertions(+), 20 deletions(-) diff --git a/src/parser/AdminSentences.cpp b/src/parser/AdminSentences.cpp index 5cd41a43ac2..ce2e0426b3e 100644 --- a/src/parser/AdminSentences.cpp +++ b/src/parser/AdminSentences.cpp @@ -37,7 +37,9 @@ std::string HostList::toString() const { buf += std::to_string(host->second); buf += ","; } - buf.resize(buf.size() - 1); + if (!buf.empty()) { + buf.resize(buf.size() - 1); + } return buf; } @@ -82,7 +84,9 @@ std::string SpaceOptList::toString() const { buf += item->toString(); buf += ","; } - buf.resize(buf.size()-1); + if (!buf.empty()) { + buf.resize(buf.size()-1); + } return buf; } diff --git a/src/parser/Clauses.cpp b/src/parser/Clauses.cpp index 522cf466c66..884a3295f3e 100644 --- a/src/parser/Clauses.cpp +++ b/src/parser/Clauses.cpp @@ -28,7 +28,9 @@ std::string SourceNodeList::toString() const { buf += std::to_string(id); buf += ","; } - buf.resize(buf.size() - 1); + if (!buf.empty()) { + buf.resize(buf.size() - 1); + } return buf; } @@ -80,7 +82,9 @@ std::string YieldColumns::toString() const { } buf += ","; } - buf.resize(buf.size() - 1); + if (!buf.empty()) { + buf.resize(buf.size() - 1); + } return buf; } diff --git a/src/parser/MaintainSentences.cpp b/src/parser/MaintainSentences.cpp index a45a8eff297..0eaaaffc39d 100644 --- a/src/parser/MaintainSentences.cpp +++ b/src/parser/MaintainSentences.cpp @@ -15,7 +15,8 @@ std::string CreateTagSentence::toString() const { buf += "CREATE TAG "; buf += *name_; buf += " ("; - for (auto *col : columns_->columnSpecs()) { + auto colSpecs = std::move(columns_->columnSpecs()); + for (auto *col : colSpecs) { buf += *col->name(); buf += " "; buf += columnTypeToString(col->type()); @@ -25,7 +26,9 @@ std::string CreateTagSentence::toString() const { } buf += ","; } - buf.resize(buf.size() - 1); + if (!colSpecs.empty()) { + buf.resize(buf.size() - 1); + } buf += ")"; return buf; } @@ -37,7 +40,8 @@ std::string CreateEdgeSentence::toString() const { buf += "CREATE EDGE "; buf += *name_; buf += " ("; - for (auto &col : columns_->columnSpecs()) { + auto colSpecs = std::move(columns_->columnSpecs()); + for (auto &col : colSpecs) { buf += *col->name(); buf += " "; buf += columnTypeToString(col->type()); @@ -47,7 +51,9 @@ std::string CreateEdgeSentence::toString() const { } buf += ","; } - buf.resize(buf.size() - 1); + if (!colSpecs.empty()) { + buf.resize(buf.size() - 1); + } buf += ")"; return buf; } @@ -58,7 +64,8 @@ std::string AlterTagOptItem::toString() const { buf.reserve(256); buf += getOptTypeStr(); buf += " ("; - for (auto &col : columns_->columnSpecs()) { + auto colSpecs = std::move(columns_->columnSpecs()); + for (auto &col : colSpecs) { buf += *col->name(); buf += " "; buf += columnTypeToString(col->type()); @@ -68,7 +75,9 @@ std::string AlterTagOptItem::toString() const { } buf += ","; } - buf.resize(buf.size() - 1); + if (!colSpecs.empty()) { + buf.resize(buf.size() - 1); + } buf += ")"; return buf; } @@ -106,7 +115,8 @@ std::string AlterEdgeSentence::toString() const { buf += "ALTER EDGE "; buf += *name_; buf += "("; - for (auto &col : columns_->columnSpecs()) { + auto colSpecs = std::move(columns_->columnSpecs()); + for (auto &col : colSpecs) { buf += *col->name(); buf += " "; buf += columnTypeToString(col->type()); @@ -116,7 +126,9 @@ std::string AlterEdgeSentence::toString() const { } buf += ","; } - buf.resize(buf.size() - 1); + if (!colSpecs.empty()) { + buf.resize(buf.size() - 1); + } buf += ")"; return buf; } diff --git a/src/parser/MutateSentences.cpp b/src/parser/MutateSentences.cpp index aeab9a1ffa6..54c4714b25d 100644 --- a/src/parser/MutateSentences.cpp +++ b/src/parser/MutateSentences.cpp @@ -15,7 +15,9 @@ std::string PropertyList::toString() const { buf += *prop; buf += ","; } - buf.resize(buf.size() - 1); + if (!buf.empty()) { + buf.resize(buf.size() - 1); + } return buf; } @@ -43,8 +45,9 @@ std::string VertexTagList::toString() const { buf += item->toString(); buf += ","; } - buf.resize(buf.size() - 1); - + if (!buf.empty()) { + buf.resize(buf.size() - 1); + } return buf; } @@ -56,7 +59,9 @@ std::string ValueList::toString() const { buf += expr->toString(); buf += ","; } - buf.resize(buf.size() - 1); + if (!buf.empty()) { + buf.resize(buf.size() - 1); + } return buf; } @@ -80,7 +85,9 @@ std::string VertexRowList::toString() const { buf += item->toString(); buf += ","; } - buf.resize(buf.size() - 1); + if (!buf.empty()) { + buf.resize(buf.size() - 1); + } return buf; } @@ -123,7 +130,9 @@ std::string EdgeRowList::toString() const { buf += item->toString(); buf += ","; } - buf.resize(buf.size() - 1); + if (!buf.empty()) { + buf.resize(buf.size() - 1); + } return buf; } @@ -161,7 +170,9 @@ std::string UpdateList::toString() const { buf += item->toString(); buf += ","; } - buf.resize(buf.size() - 1); + if (!buf.empty()) { + buf.resize(buf.size() - 1); + } return buf; } @@ -236,7 +247,9 @@ std::string EdgeList::toString() const { buf += std::to_string(edge.second); buf += ","; } - buf.resize(buf.size() - 1); + if (!buf.empty()) { + buf.resize(buf.size() - 1); + } return buf; }