-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 crash when the expression exceed the depth #3429
Closed
heroicNeZha
wants to merge
8
commits into
vesoft-inc:master
from
heroicNeZha:limit_depth_of_expression
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3350f85
fix crash when the expression exceed the depth
heroicNeZha f3067fc
format
heroicNeZha 80073ab
remove define
heroicNeZha 090a758
split new visitor
heroicNeZha d5cae60
Merge branch 'master' into limit_depth_of_expression
heroicNeZha 2246c9b
style
heroicNeZha 3821895
Merge branch 'limit_depth_of_expression' of github.com:heroicNeZha/ne…
heroicNeZha fb9de28
move check_depth into deduceProps
heroicNeZha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,251 @@ | ||
/* Copyright (c) 2021 vesoft inc. All rights reserved. | ||
* | ||
* This source code is licensed under Apache 2.0 License. | ||
*/ | ||
|
||
#include "graph/visitor/CheckDepthVisitor.h" | ||
|
||
#include <sstream> | ||
#include <unordered_map> | ||
|
||
#include "common/datatypes/DataSet.h" | ||
#include "common/datatypes/Edge.h" | ||
#include "common/datatypes/List.h" | ||
#include "common/datatypes/Map.h" | ||
#include "common/datatypes/Path.h" | ||
#include "common/datatypes/Set.h" | ||
#include "common/function/FunctionManager.h" | ||
#include "graph/context/QueryContext.h" | ||
#include "graph/context/QueryExpressionContext.h" | ||
#include "graph/context/ValidateContext.h" | ||
#include "graph/util/SchemaUtil.h" | ||
#include "graph/visitor/EvaluableExprVisitor.h" | ||
|
||
namespace nebula { | ||
namespace graph { | ||
|
||
CheckDepthVisitor::CheckDepthVisitor() {} | ||
|
||
void CheckDepthVisitor::visit(ConstantExpression *) {} | ||
|
||
void CheckDepthVisitor::visit(UnaryExpression *expr) { | ||
checkDepth(); | ||
SCOPE_EXIT { recoverDepth(); }; | ||
if (!ok()) return; | ||
expr->operand()->accept(this); | ||
} | ||
|
||
void CheckDepthVisitor::visit(TypeCastingExpression *expr) { | ||
checkDepth(); | ||
SCOPE_EXIT { recoverDepth(); }; | ||
if (!ok()) return; | ||
expr->operand()->accept(this); | ||
if (!ok()) return; | ||
} | ||
|
||
void CheckDepthVisitor::visit(LabelExpression *) {} | ||
|
||
void CheckDepthVisitor::visit(ArithmeticExpression *expr) { | ||
checkDepth(); | ||
SCOPE_EXIT { recoverDepth(); }; | ||
if (!ok()) return; | ||
expr->left()->accept(this); | ||
if (!ok()) return; | ||
expr->right()->accept(this); | ||
} | ||
|
||
void CheckDepthVisitor::visit(RelationalExpression *expr) { | ||
checkDepth(); | ||
SCOPE_EXIT { recoverDepth(); }; | ||
if (!ok()) return; | ||
expr->left()->accept(this); | ||
if (!ok()) return; | ||
expr->right()->accept(this); | ||
} | ||
|
||
void CheckDepthVisitor::visit(SubscriptExpression *expr) { | ||
checkDepth(); | ||
SCOPE_EXIT { recoverDepth(); }; | ||
if (!ok()) return; | ||
expr->left()->accept(this); | ||
if (!ok()) return; | ||
expr->right()->accept(this); | ||
} | ||
|
||
void CheckDepthVisitor::visit(AttributeExpression *expr) { | ||
checkDepth(); | ||
SCOPE_EXIT { recoverDepth(); }; | ||
if (!ok()) return; | ||
expr->left()->accept(this); | ||
if (!ok()) return; | ||
expr->right()->accept(this); | ||
} | ||
|
||
void CheckDepthVisitor::visit(LogicalExpression *expr) { | ||
checkDepth(); | ||
SCOPE_EXIT { recoverDepth(); }; | ||
if (!ok()) return; | ||
auto &operands = expr->operands(); | ||
for (auto i = 0u; i < operands.size(); i++) { | ||
operands[i]->accept(this); | ||
if (!ok()) return; | ||
} | ||
} | ||
|
||
void CheckDepthVisitor::visit(LabelAttributeExpression *expr) { | ||
checkDepth(); | ||
SCOPE_EXIT { recoverDepth(); }; | ||
if (!ok()) return; | ||
const_cast<LabelExpression *>(expr->left())->accept(this); | ||
if (!ok()) return; | ||
const_cast<ConstantExpression *>(expr->right())->accept(this); | ||
} | ||
|
||
void CheckDepthVisitor::visit(FunctionCallExpression *expr) { | ||
checkDepth(); | ||
SCOPE_EXIT { recoverDepth(); }; | ||
if (!ok()) return; | ||
for (auto &arg : expr->args()->args()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. consider use any_of |
||
arg->accept(this); | ||
if (!ok()) return; | ||
} | ||
} | ||
|
||
void CheckDepthVisitor::visit(AggregateExpression *expr) { | ||
checkDepth(); | ||
SCOPE_EXIT { recoverDepth(); }; | ||
if (!ok()) return; | ||
expr->arg()->accept(this); | ||
} | ||
|
||
void CheckDepthVisitor::visit(CaseExpression *expr) { | ||
checkDepth(); | ||
SCOPE_EXIT { recoverDepth(); }; | ||
if (!ok()) return; | ||
if (expr->hasCondition()) { | ||
expr->condition()->accept(this); | ||
if (!ok()) return; | ||
} | ||
if (expr->hasDefault()) { | ||
expr->defaultResult()->accept(this); | ||
if (!ok()) return; | ||
} | ||
for (const auto &whenThen : expr->cases()) { | ||
whenThen.when->accept(this); | ||
if (!ok()) return; | ||
whenThen.then->accept(this); | ||
if (!ok()) return; | ||
} | ||
} | ||
|
||
void CheckDepthVisitor::visit(PredicateExpression *expr) { | ||
checkDepth(); | ||
SCOPE_EXIT { recoverDepth(); }; | ||
if (!ok()) return; | ||
if (expr->hasFilter()) { | ||
expr->filter()->accept(this); | ||
if (!ok()) { | ||
return; | ||
} | ||
} | ||
expr->collection()->accept(this); | ||
} | ||
|
||
void CheckDepthVisitor::visit(ListComprehensionExpression *expr) { | ||
checkDepth(); | ||
SCOPE_EXIT { recoverDepth(); }; | ||
if (!ok()) return; | ||
if (expr->hasFilter()) { | ||
expr->filter()->accept(this); | ||
if (!ok()) { | ||
return; | ||
} | ||
} | ||
if (expr->hasMapping()) { | ||
expr->mapping()->accept(this); | ||
if (!ok()) { | ||
return; | ||
} | ||
} | ||
expr->collection()->accept(this); | ||
if (!ok()) { | ||
return; | ||
} | ||
} | ||
|
||
void CheckDepthVisitor::visit(ReduceExpression *expr) { | ||
checkDepth(); | ||
SCOPE_EXIT { recoverDepth(); }; | ||
if (!ok()) return; | ||
expr->initial()->accept(this); | ||
if (!ok()) return; | ||
expr->mapping()->accept(this); | ||
if (!ok()) return; | ||
expr->collection()->accept(this); | ||
if (!ok()) return; | ||
} | ||
|
||
void CheckDepthVisitor::visit(SubscriptRangeExpression *expr) { | ||
checkDepth(); | ||
SCOPE_EXIT { recoverDepth(); }; | ||
if (!ok()) return; | ||
expr->list()->accept(this); | ||
if (!ok()) { | ||
return; | ||
} | ||
if (expr->lo() != nullptr) { | ||
expr->lo()->accept(this); | ||
if (!ok()) { | ||
return; | ||
} | ||
} | ||
|
||
if (expr->hi() != nullptr) { | ||
expr->hi()->accept(this); | ||
if (!ok()) { | ||
return; | ||
} | ||
} | ||
} | ||
|
||
void CheckDepthVisitor::visit(UUIDExpression *) {} | ||
|
||
void CheckDepthVisitor::visit(VariableExpression *) {} | ||
|
||
void CheckDepthVisitor::visit(VersionedVariableExpression *) {} | ||
|
||
void CheckDepthVisitor::visit(ListExpression *) {} | ||
|
||
void CheckDepthVisitor::visit(SetExpression *) {} | ||
|
||
void CheckDepthVisitor::visit(MapExpression *) {} | ||
|
||
void CheckDepthVisitor::visit(TagPropertyExpression *) {} | ||
|
||
void CheckDepthVisitor::visit(EdgePropertyExpression *) {} | ||
|
||
void CheckDepthVisitor::visit(InputPropertyExpression *) {} | ||
|
||
void CheckDepthVisitor::visit(VariablePropertyExpression *) {} | ||
|
||
void CheckDepthVisitor::visit(DestPropertyExpression *) {} | ||
|
||
void CheckDepthVisitor::visit(SourcePropertyExpression *) {} | ||
|
||
void CheckDepthVisitor::visit(EdgeSrcIdExpression *) {} | ||
|
||
void CheckDepthVisitor::visit(EdgeTypeExpression *) {} | ||
|
||
void CheckDepthVisitor::visit(EdgeRankExpression *) {} | ||
|
||
void CheckDepthVisitor::visit(EdgeDstIdExpression *) {} | ||
|
||
void CheckDepthVisitor::visit(VertexExpression *) {} | ||
|
||
void CheckDepthVisitor::visit(EdgeExpression *) {} | ||
|
||
void CheckDepthVisitor::visit(ColumnExpression *) {} | ||
|
||
void CheckDepthVisitor::visit(PathBuildExpression *) {} | ||
} // namespace graph | ||
} // namespace nebula |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* Copyright (c) 2021 vesoft inc. All rights reserved. | ||
* | ||
* This source code is licensed under Apache 2.0 License. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "common/base/Status.h" | ||
#include "common/datatypes/Value.h" | ||
#include "common/expression/ExprVisitor.h" | ||
#include "graph/context/ValidateContext.h" | ||
|
||
namespace nebula { | ||
namespace graph { | ||
|
||
class QueryContext; | ||
|
||
class CheckDepthVisitor final : public ExprVisitor { | ||
public: | ||
CheckDepthVisitor(); | ||
~CheckDepthVisitor() = default; | ||
|
||
bool ok() const { return status_.ok(); } | ||
|
||
Status status() && { return std::move(status_); } | ||
|
||
private: | ||
void visit(ConstantExpression *) override; | ||
void visit(UnaryExpression *expr) override; | ||
void visit(TypeCastingExpression *expr) override; | ||
void visit(LabelExpression *) override; | ||
void visit(LabelAttributeExpression *expr) override; | ||
// binary expression | ||
void visit(ArithmeticExpression *expr) override; | ||
void visit(RelationalExpression *expr) override; | ||
void visit(SubscriptExpression *expr) override; | ||
void visit(AttributeExpression *expr) override; | ||
void visit(LogicalExpression *expr) override; | ||
// function call | ||
void visit(FunctionCallExpression *expr) override; | ||
void visit(AggregateExpression *expr) override; | ||
void visit(UUIDExpression *) override; | ||
// variable expression | ||
void visit(VariableExpression *) override; | ||
void visit(VersionedVariableExpression *) override; | ||
// container expression | ||
void visit(ListExpression *) override; | ||
void visit(SetExpression *) override; | ||
void visit(MapExpression *) override; | ||
// property Expression | ||
void visit(TagPropertyExpression *) override; | ||
void visit(EdgePropertyExpression *) override; | ||
void visit(InputPropertyExpression *) override; | ||
void visit(VariablePropertyExpression *) override; | ||
void visit(DestPropertyExpression *) override; | ||
void visit(SourcePropertyExpression *) override; | ||
void visit(EdgeSrcIdExpression *) override; | ||
void visit(EdgeTypeExpression *) override; | ||
void visit(EdgeRankExpression *) override; | ||
void visit(EdgeDstIdExpression *) override; | ||
// vertex/edge expression | ||
void visit(VertexExpression *) override; | ||
void visit(EdgeExpression *) override; | ||
// case expression | ||
void visit(CaseExpression *expr) override; | ||
// path build expression | ||
void visit(PathBuildExpression *) override; | ||
// column expression | ||
void visit(ColumnExpression *) override; | ||
// predicate expression | ||
void visit(PredicateExpression *expr) override; | ||
// list comprehension expression | ||
void visit(ListComprehensionExpression *expr) override; | ||
// reduce expression | ||
void visit(ReduceExpression *expr) override; | ||
// subscript range | ||
void visit(SubscriptRangeExpression *expr) override; | ||
|
||
inline void checkDepth() { | ||
if (++depth > MAX_DEPTH) { | ||
status_ = Status::SemanticError( | ||
"The above expression is not a valid expression, " | ||
"because its depth exceeds the maximum depth"); | ||
} | ||
} | ||
|
||
inline void recoverDepth() { --depth; } | ||
|
||
Status status_; | ||
|
||
int32_t depth = 0; | ||
const int32_t MAX_DEPTH = 512; | ||
}; | ||
|
||
} // namespace graph | ||
} // namespace nebula |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
consider refactor to two function that return bool and use: fa() && fb();
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this pr is closed