diff --git a/src/replica/replica_stub.cpp b/src/replica/replica_stub.cpp index 67db1a6950..d3c14e2337 100644 --- a/src/replica/replica_stub.cpp +++ b/src/replica/replica_stub.cpp @@ -2094,7 +2094,7 @@ replica *replica_stub::new_replica(gpid gpid, /* static */ bool replica_stub::parse_replica_dir_name(const std::string &dir_name, gpid &pid, std::string &app_type) { - std::vector ids(2, 0); + std::vector ids(2, 0); size_t begin = 0; for (auto &id : ids) { size_t end = dir_name.find('.', begin); @@ -2102,7 +2102,7 @@ replica_stub::parse_replica_dir_name(const std::string &dir_name, gpid &pid, std return false; } - if (!buf2int32(std::string_view(dir_name.data() + begin, end - begin), id)) { + if (!buf2uint32(std::string_view(dir_name.data() + begin, end - begin), id)) { return false; } @@ -2113,8 +2113,8 @@ replica_stub::parse_replica_dir_name(const std::string &dir_name, gpid &pid, std return false; } - pid.set_app_id(ids[0]); - pid.set_partition_index(ids[1]); + pid.set_app_id(static_cast(ids[0])); + pid.set_partition_index(static_cast(ids[1])); // TODO(wangdan): the 3rd parameter `count` does not support default argument for CentOS 7 // (gcc 7.3.1). After CentOS 7 is deprecated, consider dropping std::string::npos. diff --git a/src/replica/test/replica_dir_test.cpp b/src/replica/test/replica_dir_test.cpp index 2d5349af8a..3bc64d74f8 100644 --- a/src/replica/test/replica_dir_test.cpp +++ b/src/replica/test/replica_dir_test.cpp @@ -18,6 +18,7 @@ #include #include +#include "common/gpid.h" #include "gtest/gtest.h" #include "replica/replica_stub.h" @@ -97,8 +98,26 @@ class ParseReplicaDirNameTest : public testing::TestWithParam parse_replica_dir_name_tests{ + // Empty dir name. {"", false, {}, ""}, + // Single-digit IDs. {"1.2.pegasus", true, {1, 2}, "pegasus"}, + // Multi-digit IDs. + {"1234.56789.pegasus", true, {1234, 56789}, "pegasus"}, + // Custom app type other than "pegasus". + {"1.2.another", true, {1, 2}, "another"}, + // Custom app type with dot. + {"1.2.another.pegasus", true, {1, 2}, "another.pegasus"}, + // Custom app type with other specific symbol. + {"1.2.another_pegasus", true, {1, 2}, "another_pegasus"}, + // Missing one ID. + {"1.pegasus", false, {}, ""}, + // Missing both IDs. + {"pegasus", false, {}, ""}, + // ID with letter. + {"1.2a.pegasus", false, {}, ""}, + // ID with minus. + {"1.-2.pegasus", false, {}, ""}, }; INSTANTIATE_TEST_SUITE_P(ReplicaDirTest,