This is an automated email from the ASF dual-hosted git repository.
wangdan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-pegasus.git
The following commit(s) were added to refs/heads/master by this push:
new de1d02b5c feat: improve checking if C string is empty (#1229)
de1d02b5c is described below
commit de1d02b5cfa548201af8a651b73b7f32584d6af3
Author: Dan Wang <[email protected]>
AuthorDate: Tue Nov 8 19:38:47 2022 +0800
feat: improve checking if C string is empty (#1229)
---
src/common/common.cpp | 3 +-
src/redis_protocol/proxy_lib/redis_parser.cpp | 10 +++---
src/runtime/rpc/network.cpp | 3 +-
src/runtime/security/kinit_context.cpp | 9 +++---
src/runtime/security/meta_access_controller.cpp | 5 +--
src/utils/strings.h | 2 ++
src/utils/test/utils.cpp | 43 +++++++++++++++++++++----
7 files changed, 57 insertions(+), 18 deletions(-)
diff --git a/src/common/common.cpp b/src/common/common.cpp
index 1419f4d37..b0f07b8e3 100644
--- a/src/common/common.cpp
+++ b/src/common/common.cpp
@@ -19,13 +19,14 @@
#include "utils/flags.h"
#include "utils/fmt_logging.h"
+#include "utils/strings.h"
namespace dsn {
DSN_DEFINE_string("replication", cluster_name, "", "name of this cluster");
/*extern*/ const char *get_current_cluster_name()
{
- CHECK_GT_MSG(strlen(FLAGS_cluster_name), 0, "cluster_name is not set");
+ CHECK(!utils::is_empty(FLAGS_cluster_name), "cluster_name is not set");
return FLAGS_cluster_name;
}
} // namespace dsn
diff --git a/src/redis_protocol/proxy_lib/redis_parser.cpp
b/src/redis_protocol/proxy_lib/redis_parser.cpp
index 280c7eea6..135540df9 100644
--- a/src/redis_protocol/proxy_lib/redis_parser.cpp
+++ b/src/redis_protocol/proxy_lib/redis_parser.cpp
@@ -20,15 +20,17 @@
#include "redis_parser.h"
#include <rocksdb/status.h>
-#include "utils/fmt_logging.h"
-#include "common/replication_other_types.h"
-#include "utils/string_conv.h"
#include <rrdb/rrdb.client.h>
#include <pegasus/error.h>
#include <pegasus_key_schema.h>
#include <pegasus_utils.h>
+
#include "base/pegasus_const.h"
+#include "common/replication_other_types.h"
+#include "utils/fmt_logging.h"
+#include "utils/string_conv.h"
+#include "utils/strings.h"
namespace pegasus {
namespace proxy {
@@ -81,7 +83,7 @@ redis_parser::redis_parser(proxy_stub *op, dsn::message_ex
*first_msg)
dsn::replication::replica_helper::load_meta_servers(
meta_list, PEGASUS_CLUSTER_SECTION_NAME.c_str(),
op->get_cluster());
r = new ::dsn::apps::rrdb_client(op->get_cluster(), meta_list,
op->get_app());
- if (strlen(op->get_geo_app()) != 0) {
+ if (!dsn::utils::is_empty(op->get_geo_app())) {
_geo_client = dsn::make_unique<geo::geo_client>(
"config.ini", op->get_cluster(), op->get_app(),
op->get_geo_app());
}
diff --git a/src/runtime/rpc/network.cpp b/src/runtime/rpc/network.cpp
index a1fd672a1..26cf87feb 100644
--- a/src/runtime/rpc/network.cpp
+++ b/src/runtime/rpc/network.cpp
@@ -32,6 +32,7 @@
#include "utils/flags.h"
#include "utils/fmt_logging.h"
#include "utils/safe_strerror_posix.h"
+#include "utils/strings.h"
namespace dsn {
/*static*/ join_point<void, rpc_session *>
@@ -566,7 +567,7 @@ uint32_t network::get_local_ipv4()
uint32_t ip = 0;
- if (strlen(explicit_host) > 0) {
+ if (!utils::is_empty(explicit_host)) {
ip = rpc_address::ipv4_from_host(explicit_host);
}
diff --git a/src/runtime/security/kinit_context.cpp
b/src/runtime/security/kinit_context.cpp
index e42b0abaa..abbf4ccab 100644
--- a/src/runtime/security/kinit_context.cpp
+++ b/src/runtime/security/kinit_context.cpp
@@ -29,6 +29,7 @@
#include "utils/filesystem.h"
#include "utils/smart_pointers.h"
#include "utils/rand.h"
+#include "utils/strings.h"
namespace dsn {
namespace security {
@@ -58,17 +59,17 @@ error_s check_configuration()
"There is no need to check configuration if FLAGS_enable_auth"
" and FLAGS_enable_zookeeper_kerberos both are not true");
- if (0 == strlen(FLAGS_krb5_keytab) ||
!utils::filesystem::file_exists(FLAGS_krb5_keytab)) {
+ if (utils::is_empty(FLAGS_krb5_keytab) ||
!utils::filesystem::file_exists(FLAGS_krb5_keytab)) {
return error_s::make(ERR_INVALID_PARAMETERS,
fmt::format("invalid keytab file \"{}\"",
FLAGS_krb5_keytab));
}
- if (0 == strlen(FLAGS_krb5_config) ||
!utils::filesystem::file_exists(FLAGS_krb5_config)) {
+ if (utils::is_empty(FLAGS_krb5_config) ||
!utils::filesystem::file_exists(FLAGS_krb5_config)) {
return error_s::make(ERR_INVALID_PARAMETERS,
fmt::format("invalid krb5 config file \"{}\"",
FLAGS_krb5_config));
}
- if (0 == strlen(FLAGS_krb5_principal)) {
+ if (utils::is_empty(FLAGS_krb5_principal)) {
return error_s::make(ERR_INVALID_PARAMETERS, "empty principal");
}
@@ -202,7 +203,7 @@ error_s kinit_context::parse_username_from_principal()
}
KRB5_RETURN_NOT_OK(err, "krb5 parse aname to localname failed");
- if (strlen(buf) <= 0) {
+ if (utils::is_empty(buf)) {
return error_s::make(ERR_KRB5_INTERNAL, "empty username");
}
diff --git a/src/runtime/security/meta_access_controller.cpp
b/src/runtime/security/meta_access_controller.cpp
index 43dfbc911..f5d05ab62 100644
--- a/src/runtime/security/meta_access_controller.cpp
+++ b/src/runtime/security/meta_access_controller.cpp
@@ -17,10 +17,11 @@
#include "meta_access_controller.h"
+#include "runtime/rpc/network.h"
#include "runtime/rpc/rpc_message.h"
#include "utils/flags.h"
-#include "runtime/rpc/network.h"
#include "utils/fmt_logging.h"
+#include "utils/strings.h"
namespace dsn {
namespace security {
@@ -33,7 +34,7 @@ meta_access_controller::meta_access_controller()
{
// MetaServer serves the allow-list RPC from all users. RPCs unincluded
are accessible to only
// superusers.
- if (strlen(FLAGS_meta_acl_rpc_allow_list) == 0) {
+ if (utils::is_empty(FLAGS_meta_acl_rpc_allow_list)) {
register_allowed_list("RPC_CM_LIST_APPS");
register_allowed_list("RPC_CM_LIST_NODES");
register_allowed_list("RPC_CM_CLUSTER_INFO");
diff --git a/src/utils/strings.h b/src/utils/strings.h
index 5fbb5af10..dc9fb7835 100644
--- a/src/utils/strings.h
+++ b/src/utils/strings.h
@@ -36,6 +36,8 @@
namespace dsn {
namespace utils {
+inline bool is_empty(const char *str) { return str == nullptr || *str == '\0';
}
+
void split_args(const char *args,
/*out*/ std::vector<std::string> &sargs,
char splitter = ' ',
diff --git a/src/utils/test/utils.cpp b/src/utils/test/utils.cpp
index c8682aa8d..35f6cebdd 100644
--- a/src/utils/test/utils.cpp
+++ b/src/utils/test/utils.cpp
@@ -33,16 +33,17 @@
* xxxx-xx-xx, author, fix bug about xxx
*/
-#include "utils/utils.h"
-#include "utils/strings.h"
+#include <gtest/gtest.h>
+
+#include "runtime/api_layer1.h"
+#include "utils/autoref_ptr.h"
#include "utils/binary_reader.h"
#include "utils/binary_writer.h"
-#include "utils/link.h"
#include "utils/crc.h"
-#include "utils/autoref_ptr.h"
-#include "runtime/api_layer1.h"
-#include <gtest/gtest.h>
+#include "utils/link.h"
#include "utils/rand.h"
+#include "utils/strings.h"
+#include "utils/utils.h"
using namespace ::dsn;
using namespace ::dsn::utils;
@@ -84,6 +85,36 @@ TEST(core, binary_io)
EXPECT_TRUE(value3 == value);
}
+void check_empty(const char *str) { EXPECT_TRUE(dsn::utils::is_empty(str)); }
+
+void check_nonempty(const char *str) {
EXPECT_FALSE(dsn::utils::is_empty(str)); }
+
+TEST(core, check_c_string_empty)
+{
+ const char *empty_strings[] = {nullptr, "", "\0", "\0\0", "\0\0\0", "\0a",
"\0ab", "\0abc"};
+ for (const auto &p : empty_strings) {
+ check_empty(p);
+ }
+
+ const char *nonempty_strings[] = {"\\",
+ "\\\\",
+ "0",
+ "00",
+ "\\0",
+ "\\0a",
+ "\\\\00",
+ "a",
+ "a\0",
+ "a\\0",
+ "a\0b",
+ "ab\0c",
+ "abc\0",
+ "abc"};
+ for (const auto &p : nonempty_strings) {
+ check_nonempty(p);
+ }
+}
+
TEST(core, split_args)
{
std::string value = "a ,b, c ";
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]