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 crash when the expression exceed the depth #3429

Closed
Closed
Show file tree
Hide file tree
Changes from 3 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
77 changes: 76 additions & 1 deletion src/graph/visitor/DeduceTypeVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ void DeduceTypeVisitor::visit(ConstantExpression *expr) {
}

void DeduceTypeVisitor::visit(UnaryExpression *expr) {
checkDepth();
if (!status_.ok()) {
return;
heroicNeZha marked this conversation as resolved.
Show resolved Hide resolved
}
expr->operand()->accept(this);
if (!ok()) return;
switch (expr->kind()) {
Expand Down Expand Up @@ -174,9 +178,14 @@ void DeduceTypeVisitor::visit(UnaryExpression *expr) {
break;
}
}
recoverDepth();
}

void DeduceTypeVisitor::visit(TypeCastingExpression *expr) {
checkDepth();
if (!status_.ok()) {
return;
}
expr->operand()->accept(this);
if (!ok()) return;

Expand Down Expand Up @@ -208,11 +217,16 @@ void DeduceTypeVisitor::visit(TypeCastingExpression *expr) {
}
type_ = val.type();
status_ = Status::OK();
recoverDepth();
}

void DeduceTypeVisitor::visit(LabelExpression *) { type_ = Value::Type::__EMPTY__; }

void DeduceTypeVisitor::visit(ArithmeticExpression *expr) {
checkDepth();
if (!status_.ok()) {
return;
}
switch (expr->kind()) {
case Expression::Kind::kAdd: {
DETECT_BIEXPR_TYPE(+);
Expand All @@ -239,9 +253,14 @@ void DeduceTypeVisitor::visit(ArithmeticExpression *expr) {
break;
}
}
recoverDepth();
}

void DeduceTypeVisitor::visit(RelationalExpression *expr) {
checkDepth();
if (!status_.ok()) {
return;
}
expr->left()->accept(this);
if (!ok()) return;
expr->right()->accept(this);
Expand All @@ -259,9 +278,14 @@ void DeduceTypeVisitor::visit(RelationalExpression *expr) {
}

type_ = Value::Type::BOOL;
recoverDepth();
}

void DeduceTypeVisitor::visit(SubscriptExpression *expr) {
checkDepth();
if (!status_.ok()) {
return;
}
expr->left()->accept(this);
if (!ok()) return;
auto leftType = type_;
Expand Down Expand Up @@ -317,9 +341,14 @@ void DeduceTypeVisitor::visit(SubscriptExpression *expr) {

// Will not deduce the actual type of the value in list.
type_ = Value::Type::__EMPTY__;
recoverDepth();
}

void DeduceTypeVisitor::visit(AttributeExpression *expr) {
checkDepth();
if (!status_.ok()) {
return;
}
expr->left()->accept(this);
if (!ok()) return;
switch (type_) {
Expand Down Expand Up @@ -356,9 +385,14 @@ void DeduceTypeVisitor::visit(AttributeExpression *expr) {

// Will not deduce the actual type of the attribute.
type_ = Value::Type::__EMPTY__;
recoverDepth();
}

void DeduceTypeVisitor::visit(LogicalExpression *expr) {
checkDepth();
if (!status_.ok()) {
return;
}
switch (expr->kind()) {
case Expression::Kind::kLogicalAnd: {
DETECT_NARYEXPR_TYPE(&&);
Expand All @@ -374,9 +408,14 @@ void DeduceTypeVisitor::visit(LogicalExpression *expr) {
break;
}
}
recoverDepth();
}

void DeduceTypeVisitor::visit(LabelAttributeExpression *expr) {
checkDepth();
if (!status_.ok()) {
return;
}
const_cast<LabelExpression *>(expr->left())->accept(this);
if (!ok()) return;
if (type_ != Value::Type::STRING && !isSuperiorType(type_)) {
Expand All @@ -399,9 +438,14 @@ void DeduceTypeVisitor::visit(LabelAttributeExpression *expr) {

// Will not deduce the actual type of the attribute.
type_ = Value::Type::__EMPTY__;
recoverDepth();
}

void DeduceTypeVisitor::visit(FunctionCallExpression *expr) {
checkDepth();
if (!status_.ok()) {
return;
}
std::vector<Value::Type> argsTypeList;
argsTypeList.reserve(expr->args()->numArgs());
for (auto &arg : expr->args()->args()) {
Expand All @@ -422,12 +466,18 @@ void DeduceTypeVisitor::visit(FunctionCallExpression *expr) {
return;
}
type_ = result.value();
recoverDepth();
}

void DeduceTypeVisitor::visit(AggregateExpression *expr) {
checkDepth();
if (!status_.ok()) {
return;
}
expr->arg()->accept(this);
if (!ok()) return;
type_ = Value::Type::__EMPTY__;
recoverDepth();
}

void DeduceTypeVisitor::visit(UUIDExpression *) { type_ = Value::Type::STRING; }
Expand Down Expand Up @@ -524,6 +574,10 @@ void DeduceTypeVisitor::visit(EdgeExpression *) { type_ = Value::Type::EDGE; }
void DeduceTypeVisitor::visit(ColumnExpression *) { type_ = Value::Type::__EMPTY__; }

void DeduceTypeVisitor::visit(CaseExpression *expr) {
checkDepth();
if (!status_.ok()) {
return;
}
if (expr->hasCondition()) {
expr->condition()->accept(this);
if (!ok()) return;
Expand Down Expand Up @@ -553,9 +607,14 @@ void DeduceTypeVisitor::visit(CaseExpression *expr) {
} else {
type_ = Value::Type::__EMPTY__;
}
recoverDepth();
}

void DeduceTypeVisitor::visit(PredicateExpression *expr) {
checkDepth();
if (!status_.ok()) {
return;
}
if (expr->hasFilter()) {
expr->filter()->accept(this);
if (!ok()) {
Expand All @@ -576,9 +635,14 @@ void DeduceTypeVisitor::visit(PredicateExpression *expr) {
}

type_ = Value::Type::BOOL;
recoverDepth();
}

void DeduceTypeVisitor::visit(ListComprehensionExpression *expr) {
checkDepth();
if (!status_.ok()) {
return;
}
if (expr->hasFilter()) {
expr->filter()->accept(this);
if (!ok()) {
Expand Down Expand Up @@ -609,9 +673,14 @@ void DeduceTypeVisitor::visit(ListComprehensionExpression *expr) {
}

type_ = Value::Type::LIST;
recoverDepth();
}

void DeduceTypeVisitor::visit(ReduceExpression *expr) {
checkDepth();
if (!status_.ok()) {
return;
}
expr->initial()->accept(this);
if (!ok()) return;
expr->mapping()->accept(this);
Expand All @@ -633,9 +702,14 @@ void DeduceTypeVisitor::visit(ReduceExpression *expr) {

// Will not deduce the actual value type returned by reduce expression.
type_ = Value::Type::__EMPTY__;
recoverDepth();
}

void DeduceTypeVisitor::visit(SubscriptRangeExpression *expr) {
checkDepth();
if (!status_.ok()) {
return;
}
expr->list()->accept(this);
if (!ok()) {
return;
Expand Down Expand Up @@ -671,6 +745,7 @@ void DeduceTypeVisitor::visit(SubscriptRangeExpression *expr) {
}
}
type_ = Value::Type::LIST;
recoverDepth();
}

void DeduceTypeVisitor::visitVertexPropertyExpr(PropertyExpression *expr) {
Expand Down Expand Up @@ -700,6 +775,6 @@ void DeduceTypeVisitor::visit(PathBuildExpression *) { type_ = Value::Type::PATH
#undef DETECT_NARYEXPR_TYPE
#undef DETECT_UNARYEXPR_TYPE
#undef DETECT_BIEXPR_TYPE

#undef CHECK_DEPTH
} // namespace graph
} // namespace nebula
12 changes: 12 additions & 0 deletions src/graph/visitor/DeduceTypeVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,25 @@ class DeduceTypeVisitor final : public ExprVisitor {
return type == Value::Type::NULLVALUE || type == Value::Type::__EMPTY__;
}

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; }

const QueryContext *qctx_{nullptr};
const ValidateContext *vctx_{nullptr};
const ColsDef &inputs_;
GraphSpaceID space_;
Status status_;
Value::Type type_;
Value::Type vidType_;
int32_t depth = 0;
const int32_t MAX_DEPTH = 816;
};

} // namespace graph
Expand Down
Loading