Skip to content

Commit

Permalink
BugFix: fix coredump of trpc::config::GetInt64() with overflowed int32
Browse files Browse the repository at this point in the history
issue: trpc::config::GetInt64() with json may coredump when real value is greater than INT32_MAX.
cause: asInt() of json-cpp with overflowed interge will assert failure.
fix: change asInt() to asInt64().
  • Loading branch information
liucf3995 committed Dec 8, 2023
1 parent f99f77d commit 737cf13
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
1 change: 1 addition & 0 deletions trpc/config/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ cc_test(
srcs = ["trpc_conf_compatible_test.cc"],
deps = [
":trpc_conf",
"@com_github_fmtlib_fmt//:fmtlib",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],
Expand Down
2 changes: 1 addition & 1 deletion trpc/config/trpc_conf_compatible.cc
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ bool TransformConfig(const Json::Value& from, std::map<std::string, std::string>
Json::ValueType value_type = from[*it].type();
switch (value_type) {
case Json::intValue: {
config_map->insert(std::pair<std::string, std::string>(key_name, std::to_string(from[*it].asInt())));
config_map->insert(std::pair<std::string, std::string>(key_name, std::to_string(from[*it].asInt64())));
break;
}
case Json::uintValue: {
Expand Down
38 changes: 38 additions & 0 deletions trpc/config/trpc_conf_compatible_test.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "trpc/config/trpc_conf_compatible.h"

#include "fmt/core.h"
#include "fmt/format.h"
#include "gtest/gtest.h"
#include "json/json.h"
#include "yaml-cpp/yaml.h"
Expand Down Expand Up @@ -65,6 +67,7 @@ TEST_F(TrpcConfCompatibleTest, GetInt64_Success) {
ASSERT_EQ(result, -42);
}


TEST_F(TrpcConfCompatibleTest, GetInt64_Fail) {
Json::Value config_json;
config_json["value1"] = 42;
Expand Down Expand Up @@ -152,4 +155,39 @@ TEST_F(TrpcConfCompatibleTest, ExtractYamlKeys) {
ASSERT_EQ(yaml_keys[1], "key2");
}

TEST_F(TrpcConfCompatibleTest, TransformConfig_JSON_Success_WithIntegerMaxAndMin) {
auto int_max = std::numeric_limits<std::int32_t>::max();
auto int_min = std::numeric_limits<std::int32_t>::min();
auto int64_max = std::numeric_limits<std::int64_t>::max();
auto int64_min = std::numeric_limits<std::int64_t>::min();
std::string from = fmt::format(R"({{"id": "0001", "int_max": {}, "int_min": {}, "int64_max": {}, "int64_min": {}}})",
int_max, int_min, int64_max, int64_min);

Json::Value json_value;
bool trans_result = trpc::config::TransformConfig(from, &json_value);
ASSERT_TRUE(trans_result);

std::map<std::string, std::string> out_map;
trans_result = trpc::config::TransformConfig(json_value, &out_map, "");
ASSERT_TRUE(trans_result);
ASSERT_TRUE(out_map.find("id") != out_map.end());
ASSERT_TRUE(out_map.find("int_max") != out_map.end());
ASSERT_TRUE(out_map.find("int_min") != out_map.end());
ASSERT_TRUE(out_map.find("int64_max") != out_map.end());
ASSERT_TRUE(out_map.find("int64_min") != out_map.end());
}

TEST_F(TrpcConfCompatibleTest, TransformConfig_JSON_Fail_WithIntegerOverflow) {
std::string int_overflow{"12233344449223372036854775807"};
std::string from = fmt::format(R"({{"id": "0001", "int_overflow": {}}})", int_overflow);

Json::Value json_value;
bool trans_result = trpc::config::TransformConfig(from, &json_value);
// Overflowed integer-64 value in cpp-json is ok.
ASSERT_TRUE(trans_result);

int64_t v{0};
ASSERT_FALSE(trpc::config::GetInt64(json_value, "int_overflow", &v));
}

} // namespace trpc::testing

0 comments on commit 737cf13

Please sign in to comment.