Skip to content
This repository has been archived by the owner on Dec 1, 2022. It is now read-only.

fix substr crash #491

Merged
merged 4 commits into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
32 changes: 13 additions & 19 deletions src/common/function/FunctionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1313,32 +1313,26 @@ FunctionManager::FunctionManager() {
attr.isPure_ = true;
attr.body_ = [](const auto &args) -> Value {
auto argSize = args.size();
if (argSize < 2 || argSize >3) {
if (argSize < 2 || argSize > 3) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The find method will check the count of arguments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix

LOG(ERROR) << "Unexpected arguments count " << args.size();
return Value::kNullBadData;
}
if (args[0].isNull()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty is the same. but the type Signature has made sure the input args[0] must string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type Signature only use in deduce expr's type , When it encounters null, it returns directly in validator
So in the execution stage, we need to re-judge

user cannot enter EMPTY .so we not process EMPTY

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the query input can be empty, such as pipe

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EMPTY will do later ,all function have this problem

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay

return Value::kNullValue;
}
if (!args[0].isStr() || !args[1].isInt() || (argSize == 3 && !args[2].isInt())) {
return Value::kNullBadType;
}
auto value = args[0].getStr();
auto start = args[1].getInt();
auto length = (args.size() == 2) ? value.size() - start : args[2].getInt();
if (args[0].isStr() && args[1].isInt()) {
if (argSize == 3) {
if (!args[2].isInt()) {
return Value::kNullBadType;
}
}
if (static_cast<size_t>(std::abs(start)) > value.size() || length == 0) {
return std::string("");
}
if (start < 0) {
LOG(ERROR) << "Invalid Start index " << start;
return Value::kNullBadData;
}
if (start == 0) {
return value;
}
return value.substr(start, length);
if (start < 0 || length < 0) {
return Value::kNullBadData;
}
return Value::kNullBadType;
if (static_cast<size_t>(start) > value.size() || length == 0) {
return std::string("");
}
return value.substr(start, length);
};
functions_["substring"] = attr;
}
Expand Down
5 changes: 5 additions & 0 deletions src/common/function/test/FunctionManagerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ TEST_F(FunctionManagerTest, testNull) {
TEST_FUNCTION(toUpper, args_["nullvalue"], Value::kNullValue);
TEST_FUNCTION(split, std::vector<Value>({Value::kNullValue, ","}), Value::kNullValue);
TEST_FUNCTION(split, std::vector<Value>({"123,22", Value::kNullValue}), Value::kNullValue);
TEST_FUNCTION(substr, std::vector<Value>({Value::kNullValue, 1, 2}), Value::kNullValue);
TEST_FUNCTION(substr, std::vector<Value>({"hello", Value::kNullValue, 2}), Value::kNullBadType);
TEST_FUNCTION(substr, std::vector<Value>({"hello", 2, Value::kNullValue}), Value::kNullBadType);
TEST_FUNCTION(substr, std::vector<Value>({"hello", -1, 10}), Value::kNullBadData);
TEST_FUNCTION(substr, std::vector<Value>({"hello", 1, -2}), Value::kNullBadData);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add test when start large than string's size

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alread exist in line 244

}

TEST_F(FunctionManagerTest, functionCall) {
Expand Down