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 #3545

Closed
wants to merge 4 commits into from
Closed
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
3 changes: 3 additions & 0 deletions src/common/expression/AggregateExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ class AggregateExpression final : public Expression {
aggData_ = agg_data;
}

void setDepth() override { setDepthFromSubExpr(arg_); }

private:
explicit AggregateExpression(ObjectPool* pool,
const std::string& name = "",
Expand All @@ -89,6 +91,7 @@ class AggregateExpression final : public Expression {
if (aggFuncResult.ok()) {
aggFunc_ = std::move(aggFuncResult).value();
}
setDepth();
}

void writeTo(Encoder& encoder) const override;
Expand Down
9 changes: 8 additions & 1 deletion src/common/expression/BinaryExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,16 @@ class BinaryExpression : public Expression {
rhs_ = expr;
}

void setDepth() override {
setDepthFromSubExpr(lhs_);
setDepthFromSubExpr(rhs_);
}

protected:
BinaryExpression(ObjectPool* pool, Kind kind, Expression* lhs, Expression* rhs)
: Expression(pool, kind), lhs_(lhs), rhs_(rhs) {}
: Expression(pool, kind), lhs_(lhs), rhs_(rhs) {
setDepth();
}

void writeTo(Encoder& encoder) const override;

Expand Down
8 changes: 8 additions & 0 deletions src/common/expression/CaseExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,20 @@ class CaseExpression final : public Expression {
return expr;
}

void setDepth() override {
for (auto& item : cases_) {
setDepthFromSubExpr(item.when);
setDepthFromSubExpr(item.then);
}
}

private:
explicit CaseExpression(ObjectPool* pool) : Expression(pool, Kind::kCase), isGeneric_(true) {}

explicit CaseExpression(ObjectPool* pool, CaseList* cases, bool isGeneric)
: Expression(pool, Kind::kCase), isGeneric_(isGeneric) {
cases_ = cases->items();
setDepth();
}
void writeTo(Encoder& encoder) const override;

Expand Down
13 changes: 13 additions & 0 deletions src/common/expression/ContainerExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ class ListExpression final : public Expression {
explicit ListExpression(ObjectPool *pool) : Expression(pool, Kind::kList) {}

explicit ListExpression(ObjectPool *pool, ExpressionList *items) : Expression(pool, Kind::kList) {
for (auto &item : items->get()) {
setDepthFromSubExpr(item);
}
items_ = items->get();
}

Expand Down Expand Up @@ -183,6 +186,9 @@ class SetExpression final : public Expression {
explicit SetExpression(ObjectPool *pool) : Expression(pool, Kind::kSet) {}

explicit SetExpression(ObjectPool *pool, ExpressionList *items) : Expression(pool, Kind::kSet) {
for (auto &item : items->get()) {
setDepthFromSubExpr(item);
}
items_ = items->get();
}

Expand Down Expand Up @@ -248,11 +254,18 @@ class MapExpression final : public Expression {
return true;
}

void setDepth() override {
for (auto &item : items_) {
setDepthFromSubExpr(item.second);
}
}

private:
explicit MapExpression(ObjectPool *pool) : Expression(pool, Kind::kMap) {}

explicit MapExpression(ObjectPool *pool, MapItemList *items) : Expression(pool, Kind::kMap) {
items_ = items->get();
setDepth();
}

void writeTo(Encoder &encoder) const override;
Expand Down
16 changes: 16 additions & 0 deletions src/common/expression/Expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#ifndef COMMON_EXPRESSION_EXPRESSION_H_
#define COMMON_EXPRESSION_EXPRESSION_H_

#include <cstdint>

#include "common/base/Base.h"
#include "common/base/ObjectPool.h"
#include "common/context/ExpressionContext.h"
Expand Down Expand Up @@ -168,6 +170,12 @@ class Expression {
return false;
}

int32_t getDepth() { return depth_; }

virtual void setDepth() { return; }

bool checkDepth() { return depth_ < MAX_DEPTH; }

protected:
class Encoder final {
public:
Expand Down Expand Up @@ -217,7 +225,15 @@ class Expression {

ObjectPool* pool_{nullptr};

void setDepthFromSubExpr(Expression* expr) {
if (expr != nullptr) {
depth_ = std::max(depth_, expr->getDepth() + 1);
}
}

Kind kind_;
int32_t depth_ = 1;
const int32_t MAX_DEPTH = 512;
};

std::ostream& operator<<(std::ostream& os, Expression::Kind kind);
Expand Down
7 changes: 7 additions & 0 deletions src/common/expression/FunctionCallExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ class FunctionCallExpression final : public Expression {
return args_;
}

void setDepth() override {
for (auto& item : args_->args()) {
setDepthFromSubExpr(item);
}
}

private:
explicit FunctionCallExpression(ObjectPool* pool, const std::string& name, ArgumentList* args)
: Expression(pool, Kind::kFunctionCall), name_(name), args_(args) {
Expand All @@ -113,6 +119,7 @@ class FunctionCallExpression final : public Expression {
func_ = funcResult.value();
}
}
setDepth();
}

void writeTo(Encoder& encoder) const override;
Expand Down
6 changes: 6 additions & 0 deletions src/common/expression/LabelAttributeExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ class LabelAttributeExpression final : public Expression {

std::string toString() const override;

void setDepth() override {
setDepthFromSubExpr(lhs_);
setDepthFromSubExpr(rhs_);
}

private:
explicit LabelAttributeExpression(ObjectPool* pool,
LabelExpression* lhs = nullptr,
Expand All @@ -72,6 +77,7 @@ class LabelAttributeExpression final : public Expression {
DCHECK(rhs == nullptr || rhs->value().isStr());
lhs_ = lhs;
rhs_ = rhs;
setDepth();
}

void writeTo(Encoder&) const override {
Expand Down
10 changes: 9 additions & 1 deletion src/common/expression/ListComprehensionExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ class ListComprehensionExpression final : public Expression {
return !originString_.empty();
}

void setDepth() override {
setDepthFromSubExpr(collection_);
setDepthFromSubExpr(filter_);
setDepthFromSubExpr(mapping_);
}

private:
explicit ListComprehensionExpression(ObjectPool* pool,
const std::string& innerVar = "",
Expand All @@ -109,7 +115,9 @@ class ListComprehensionExpression final : public Expression {
innerVar_(innerVar),
collection_(collection),
filter_(filter),
mapping_(mapping) {}
mapping_(mapping) {
setDepth();
}

void writeTo(Encoder& encoder) const override;

Expand Down
7 changes: 7 additions & 0 deletions src/common/expression/LogicalExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,20 @@ class LogicalExpression final : public Expression {
return true;
}

void setDepth() override {
for (auto& item : operands_) {
setDepthFromSubExpr(item);
}
}

private:
explicit LogicalExpression(ObjectPool* pool, Kind kind) : Expression(pool, kind) {}

LogicalExpression(ObjectPool* pool, Kind kind, Expression* lhs, Expression* rhs)
: Expression(pool, kind) {
operands_.emplace_back(lhs);
operands_.emplace_back(rhs);
setDepth();
}

void writeTo(Encoder& encoder) const override;
Expand Down
9 changes: 8 additions & 1 deletion src/common/expression/PredicateExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ class PredicateExpression final : public Expression {
return filter_ != nullptr;
}

void setDepth() override {
setDepthFromSubExpr(collection_);
setDepthFromSubExpr(filter_);
}

private:
explicit PredicateExpression(ObjectPool* pool,
const std::string& name = "",
Expand All @@ -109,7 +114,9 @@ class PredicateExpression final : public Expression {
name_(name),
innerVar_(innerVar),
collection_(collection),
filter_(filter) {}
filter_(filter) {
setDepth();
}

const Value& evalExists(ExpressionContext& ctx);
void writeTo(Encoder& encoder) const override;
Expand Down
9 changes: 8 additions & 1 deletion src/common/expression/ReduceExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ class ReduceExpression final : public Expression {
return !originString_.empty();
}

void setDepth() override {
setDepthFromSubExpr(collection_);
setDepthFromSubExpr(mapping_);
}

private:
explicit ReduceExpression(ObjectPool* pool,
const std::string& accumulator = "",
Expand All @@ -110,7 +115,9 @@ class ReduceExpression final : public Expression {
initial_(initial),
innerVar_(innerVar),
collection_(collection),
mapping_(mapping) {}
mapping_(mapping) {
setDepth();
}

void writeTo(Encoder& encoder) const override;
void resetFrom(Decoder& decoder) override;
Expand Down
7 changes: 7 additions & 0 deletions src/common/expression/SubscriptExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,19 @@ class SubscriptRangeExpression final : public Expression {
explicit SubscriptRangeExpression(ObjectPool* pool)
: Expression(pool, Kind::kSubscriptRange), list_(nullptr), lo_(nullptr), hi_(nullptr) {}

void setDepth() override {
setDepthFromSubExpr(list_);
setDepthFromSubExpr(lo_);
setDepthFromSubExpr(hi_);
}

explicit SubscriptRangeExpression(ObjectPool* pool,
Expression* list,
Expression* lo,
Expression* hi)
: Expression(pool, Kind::kSubscriptRange), list_(DCHECK_NOTNULL(list)), lo_(lo), hi_(hi) {
DCHECK(!(lo_ == nullptr && hi_ == nullptr));
setDepth();
}
void writeTo(Encoder& encoder) const override;

Expand Down
6 changes: 5 additions & 1 deletion src/common/expression/TypeCastingExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,15 @@ class TypeCastingExpression final : public Expression {

static bool validateTypeCast(Value::Type operandType, Value::Type type);

void setDepth() override { setDepthFromSubExpr(operand_); }

private:
explicit TypeCastingExpression(ObjectPool* pool,
Value::Type vType = Value::Type::__EMPTY__,
Expression* operand = nullptr)
: Expression(pool, Kind::kTypeCasting), vType_(vType), operand_(operand) {}
: Expression(pool, Kind::kTypeCasting), vType_(vType), operand_(operand) {
setDepth();
}

void writeTo(Encoder& encoder) const override;
void resetFrom(Decoder& decoder) override;
Expand Down
6 changes: 5 additions & 1 deletion src/common/expression/UnaryExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,13 @@ class UnaryExpression final : public Expression {
operand_ = expr;
}

void setDepth() override { setDepthFromSubExpr(operand_); }

private:
explicit UnaryExpression(ObjectPool* pool, Kind kind, Expression* operand = nullptr)
: Expression(pool, kind), operand_(operand) {}
: Expression(pool, kind), operand_(operand) {
setDepth();
}

void writeTo(Encoder& encoder) const override;
void resetFrom(Decoder& decoder) override;
Expand Down
Loading