From 59e87717947d9858d97d387f31c606c186dfbec1 Mon Sep 17 00:00:00 2001 From: Ranxy <3327004+Ranxy@users.noreply.github.com> Date: Wed, 3 Aug 2022 11:06:49 +0800 Subject: [PATCH 1/4] Replace atoi to Util::DecimalStringToNum --- src/redis_cmd.cc | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/redis_cmd.cc b/src/redis_cmd.cc index 62ee1025b60..62db7eff0e4 100644 --- a/src/redis_cmd.cc +++ b/src/redis_cmd.cc @@ -4473,12 +4473,13 @@ class CommandCluster : public Commander { if (subcommand_ == "keyslot" && args_.size() == 3) return Status::OK(); if (subcommand_ == "import") { if (args.size() != 4) return Status(Status::RedisParseErr, errWrongNumOfArguments); - try { - slot_ = atoi(args[2].c_str()); - state_ = static_cast(atoi(args[3].c_str())); - } catch (std::exception &e) { - return Status(Status::RedisParseErr, errValueNotInterger); - } + auto s = Util::DecimalStringToNum(args[2], &slot_); + if (!s.IsOK()) return s; + + int64_t state; + s = Util::DecimalStringToNum(args[3], &state, static_cast(kImportStart), static_cast(kImportNone)); + if (!s.IsOK()) return s; + state_ = static_cast(state); return Status::OK(); } return Status(Status::RedisParseErr, @@ -4535,7 +4536,7 @@ class CommandCluster : public Commander { *output = Redis::Error(s.Msg()); } } else if (subcommand_ == "import") { - Status s = svr->cluster_->ImportSlot(conn, slot_, state_); + Status s = svr->cluster_->ImportSlot(conn, static_cast(slot_), state_); if (s.IsOK()) { *output = Redis::SimpleString("OK"); } else { @@ -4549,7 +4550,7 @@ class CommandCluster : public Commander { private: std::string subcommand_; - int slot_ = -1; + int64_t slot_ = -1; ImportStatus state_ = kImportNone; }; @@ -4563,12 +4564,9 @@ class CommandClusterX : public Commander { args_[2].size() == kClusterNodeIdLen) return Status::OK(); if (subcommand_ == "migrate") { if (args.size() != 4) return Status(Status::RedisParseErr, errWrongNumOfArguments); - try { - slot_ = atoi(args[2].c_str()); - dst_node_id_ = args[3]; - } catch (std::exception &e) { - return Status(Status::RedisParseErr, errValueNotInterger); - } + auto s = Util::DecimalStringToNum(args[2], &slot_); + if (!s.IsOK()) return s; + dst_node_id_ = args[3]; return Status::OK(); } if (subcommand_ == "setnodes" && args_.size() >= 4) { @@ -4639,7 +4637,7 @@ class CommandClusterX : public Commander { int64_t v = svr->cluster_->GetVersion(); *output = Redis::BulkString(std::to_string(v)); } else if (subcommand_ == "migrate") { - Status s = svr->cluster_->MigrateSlot(slot_, dst_node_id_); + Status s = svr->cluster_->MigrateSlot(static_cast(slot_), dst_node_id_); if (s.IsOK()) { *output = Redis::SimpleString("OK"); } else { @@ -4658,7 +4656,7 @@ class CommandClusterX : public Commander { int slot_id_ = -1; bool force_ = false; std::string dst_node_id_; - int slot_ = -1; + int64_t slot_ = -1; }; class CommandEval : public Commander { From 23c65452c500640547d95830e3b508894f5fe5a8 Mon Sep 17 00:00:00 2001 From: Ranxy <3327004+Ranxy@users.noreply.github.com> Date: Wed, 3 Aug 2022 11:22:24 +0800 Subject: [PATCH 2/4] fix lint error --- src/redis_cmd.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/redis_cmd.cc b/src/redis_cmd.cc index 62db7eff0e4..cc64dae1d39 100644 --- a/src/redis_cmd.cc +++ b/src/redis_cmd.cc @@ -4477,7 +4477,9 @@ class CommandCluster : public Commander { if (!s.IsOK()) return s; int64_t state; - s = Util::DecimalStringToNum(args[3], &state, static_cast(kImportStart), static_cast(kImportNone)); + s = Util::DecimalStringToNum(args[3], &state, + static_cast(kImportStart), + static_cast(kImportNone)); if (!s.IsOK()) return s; state_ = static_cast(state); return Status::OK(); From db649f2b068ea07ed4fecafd89ccf88d5a0fd27b Mon Sep 17 00:00:00 2001 From: Ranxy <3327004+Ranxy@users.noreply.github.com> Date: Wed, 3 Aug 2022 11:28:59 +0800 Subject: [PATCH 3/4] fix lint error --- src/redis_cmd.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/redis_cmd.cc b/src/redis_cmd.cc index cc64dae1d39..0ef52ef135e 100644 --- a/src/redis_cmd.cc +++ b/src/redis_cmd.cc @@ -4477,8 +4477,8 @@ class CommandCluster : public Commander { if (!s.IsOK()) return s; int64_t state; - s = Util::DecimalStringToNum(args[3], &state, - static_cast(kImportStart), + s = Util::DecimalStringToNum(args[3], &state, + static_cast(kImportStart), static_cast(kImportNone)); if (!s.IsOK()) return s; state_ = static_cast(state); From ca89044594f4e363f6d7970f2c2000e2addbe8aa Mon Sep 17 00:00:00 2001 From: Ranxy <3327004+Ranxy@users.noreply.github.com> Date: Wed, 3 Aug 2022 12:28:06 +0800 Subject: [PATCH 4/4] fix error msg --- src/redis_cmd.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/redis_cmd.cc b/src/redis_cmd.cc index 0ef52ef135e..018f689dd19 100644 --- a/src/redis_cmd.cc +++ b/src/redis_cmd.cc @@ -4480,7 +4480,7 @@ class CommandCluster : public Commander { s = Util::DecimalStringToNum(args[3], &state, static_cast(kImportStart), static_cast(kImportNone)); - if (!s.IsOK()) return s; + if (!s.IsOK()) return Status(Status::NotOK, "Invalid import state"); state_ = static_cast(state); return Status::OK(); }