Skip to content

Commit

Permalink
Fix #5259: catch exception in boost::tokenizer (#5345) (#5362)
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Apr 21, 2022
1 parent 2b006ea commit 9771b47
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
11 changes: 8 additions & 3 deletions be/src/exprs/vectorized/json_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,14 @@ Status JsonFunctions::json_path_prepare(starrocks_udf::FunctionContext* context,
return Status::OK();
}

boost::tokenizer<boost::escaped_list_separator<char>> tok(path_str,
boost::escaped_list_separator<char>("\\", ".", "\""));
std::vector<std::string> path_exprs(tok.begin(), tok.end());
std::vector<std::string> path_exprs;
try {
boost::tokenizer<boost::escaped_list_separator<char>> tok(path_str,
boost::escaped_list_separator<char>("\\", ".", "\""));
path_exprs.assign(tok.begin(), tok.end());
} catch (const boost::escaped_list_error& e) {
return Status::InvalidArgument(strings::Substitute("Illegal json path: $0", e.what()));
}
std::vector<SimpleJsonPath>* parsed_paths = new std::vector<SimpleJsonPath>();
_get_parsed_paths(path_exprs, parsed_paths);

Expand Down
11 changes: 8 additions & 3 deletions be/src/exprs/vectorized/jsonpath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,14 @@ Status JsonPathPiece::parse(const std::string& path_string, std::vector<JsonPath
// '$.text#abc.xyz' -> [$, text#abc, xyz]
// '$."text.abc".xyz' -> [$, text.abc, xyz]
// '$."text.abc"[1].xyz' -> [$, text.abc[1], xyz]
boost::tokenizer<boost::escaped_list_separator<char>> tok(path_string,
boost::escaped_list_separator<char>("\\", ".", "\""));
std::vector<std::string> path_exprs(tok.begin(), tok.end());
std::vector<std::string> path_exprs;
try {
boost::tokenizer<boost::escaped_list_separator<char>> tok(path_string,
boost::escaped_list_separator<char>("\\", ".", "\""));
path_exprs.assign(tok.begin(), tok.end());
} catch (const boost::escaped_list_error& e) {
return Status::InvalidArgument(strings::Substitute("Invalid json path $0", e.what()));
}

for (int i = 0; i < path_exprs.size(); i++) {
std::string col;
Expand Down
11 changes: 9 additions & 2 deletions be/test/exprs/vectorized/json_functions_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,11 @@ TEST_P(JsonExistTestFixture, json_exists) {
Columns columns{ints, builder.build(true)};

ctx.get()->impl()->set_constant_columns(columns);
JsonFunctions::native_json_path_prepare(ctx.get(), FunctionContext::FunctionStateScope::FRAGMENT_LOCAL);
Status st = JsonFunctions::native_json_path_prepare(ctx.get(), FunctionContext::FunctionStateScope::FRAGMENT_LOCAL);
if (!st.ok()) {
ASSERT_FALSE(param_exist);
return;
}

ColumnPtr result = JsonFunctions::json_exists(ctx.get(), columns);
ASSERT_TRUE(!!result);
Expand Down Expand Up @@ -485,7 +489,10 @@ INSTANTIATE_TEST_SUITE_P(JsonExistTest, JsonExistTestFixture,

// special case
std::make_tuple(R"({ "k1": {}})", "$", true),
std::make_tuple(R"({ "k1": {}})", "", true)));
std::make_tuple(R"({ "k1": {}})", "", true),

// error case
std::make_tuple(R"( {"k1": null} )", std::string(10, 0x1), false)));

class JsonParseTestFixture : public ::testing::TestWithParam<std::tuple<std::string, bool>> {};

Expand Down

0 comments on commit 9771b47

Please sign in to comment.