Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: decide whether two C strings are equal and support CHECK_STREQ* and CHECK_STRNE* #1300

Merged
merged 9 commits into from
Dec 26, 2022
Merged
13 changes: 7 additions & 6 deletions src/aio/test/aio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@
* THE SOFTWARE.
*/

#include <gtest/gtest.h>

#include "runtime/task/async_calls.h"
#include "utils/fail_point.h"
#include "utils/filesystem.h"
#include "utils/smart_pointers.h"
#include "utils/fail_point.h"

#include <gtest/gtest.h>
#include "utils/strings.h"

using namespace ::dsn;

Expand Down Expand Up @@ -121,7 +122,7 @@ TEST(core, aio)

t->wait();
EXPECT_TRUE(t->get_transferred_size() == (size_t)len);
EXPECT_TRUE(memcmp(buffer, buffer2, len) == 0);
EXPECT_TRUE(dsn::utils::mequals(buffer, buffer2, len));
}

err = file::close(fp);
Expand Down Expand Up @@ -178,12 +179,12 @@ TEST(core, operation_failed)
t = ::dsn::file::read(fp2, buffer, 512, 0, LPC_AIO_TEST, nullptr, io_callback, 0);
t->wait();
EXPECT_TRUE(*err == ERR_OK && *count == strlen(str));
EXPECT_TRUE(strncmp(buffer, str, 10) == 0);
EXPECT_TRUE(dsn::utils::equals(buffer, str, 10));

t = ::dsn::file::read(fp2, buffer, 5, 0, LPC_AIO_TEST, nullptr, io_callback, 0);
t->wait();
EXPECT_TRUE(*err == ERR_OK && *count == 5);
EXPECT_TRUE(strncmp(buffer, str, 5) == 0);
EXPECT_TRUE(dsn::utils::equals(buffer, str, 5));

t = ::dsn::file::read(fp2, buffer, 512, 100, LPC_AIO_TEST, nullptr, io_callback, 0);
t->wait();
Expand Down
15 changes: 8 additions & 7 deletions src/block_service/test/fds_service_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@

#include "block_service/fds/fds_service.h"

#include <fcntl.h>

#include <array>
#include <fcntl.h>
#include <fstream>
#include <gtest/gtest.h>
#include <memory>

#include <gtest/gtest.h>

#include "block_service/block_service.h"
#include "utils/fmt_logging.h"
#include "utils/filesystem.h"
#include "utils/fmt_logging.h"
#include "utils/rand.h"
#include "utils/safe_strerror_posix.h"
#include "utils/strings.h"
#include "utils/utils.h"

using namespace dsn;
Expand Down Expand Up @@ -65,7 +66,7 @@ static void file_eq_compare(const std::string &fname1, const std::string &fname2
int up_to_bytes = length < (l - i) ? length : (l - i);
ifile1.read(buf1, up_to_bytes);
ifile2.read(buf2, up_to_bytes);
ASSERT_TRUE(memcmp(buf1, buf2, up_to_bytes) == 0);
ASSERT_TRUE(dsn::utils::mequals(buf1, buf2, up_to_bytes));
}
}

Expand Down Expand Up @@ -507,7 +508,7 @@ TEST_F(FDSClientTest, test_basic_operation)

ASSERT_EQ(dsn::ERR_OK, r_resp.err);
ASSERT_EQ(length, r_resp.buffer.length());
ASSERT_EQ(0, memcmp(r_resp.buffer.data(), test_buffer, length));
ASSERT_TRUE(dsn::utils::mequals(r_resp.buffer.data(), test_buffer, length));

// partitial read
cf_resp.file_handle
Expand All @@ -519,7 +520,7 @@ TEST_F(FDSClientTest, test_basic_operation)

ASSERT_EQ(dsn::ERR_OK, r_resp.err);
ASSERT_EQ(10, r_resp.buffer.length());
ASSERT_EQ(0, memcmp(r_resp.buffer.data(), test_buffer + 5, 10));
ASSERT_TRUE(dsn::utils::mequals(r_resp.buffer.data(), test_buffer + 5, 10));
}

// then test remove path
Expand Down
16 changes: 9 additions & 7 deletions src/block_service/test/hdfs_service_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@
// specific language governing permissions and limitations
// under the License.

#include "block_service/block_service.h"
#include "utils/filesystem.h"
#include "utils/flags.h"
#include "utils/rand.h"
#include <fstream>
#include <gtest/gtest.h>
#include <memory>

#include <gtest/gtest.h>

#include "block_service/block_service.h"
#include "block_service/hdfs/hdfs_service.h"
#include "utils/filesystem.h"
#include "utils/flags.h"
#include "utils/rand.h"
#include "utils/strings.h"

using namespace dsn;
using namespace dsn::dist::block_service;
Expand Down Expand Up @@ -219,7 +221,7 @@ TEST_F(HDFSClientTest, test_basic_operation)
->wait();
ASSERT_EQ(dsn::ERR_OK, r_resp.err);
ASSERT_EQ(length, r_resp.buffer.length());
ASSERT_EQ(0, memcmp(r_resp.buffer.data(), test_buffer, length));
ASSERT_TRUE(dsn::utils::mequals(r_resp.buffer.data(), test_buffer, length));

// test partitial read.
cf_resp.file_handle
Expand All @@ -230,7 +232,7 @@ TEST_F(HDFSClientTest, test_basic_operation)
->wait();
ASSERT_EQ(dsn::ERR_OK, r_resp.err);
ASSERT_EQ(10, r_resp.buffer.length());
ASSERT_EQ(0, memcmp(r_resp.buffer.data(), test_buffer + 5, 10));
ASSERT_TRUE(dsn::utils::mequals(r_resp.buffer.data(), test_buffer + 5, 10));

// clean up local test files.
utils::filesystem::remove_path(local_test_file);
Expand Down
4 changes: 2 additions & 2 deletions src/failure_detector/test/failure_detector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,11 @@ bool get_worker_and_master(test_worker *&worker, std::vector<test_master *> &mas
worker = nullptr;

for (int i = 0; i != apps.size(); ++i) {
if (strcmp(apps[i]->info().type.c_str(), "worker") == 0) {
if (apps[i]->info().type == "worker") {
if (worker != nullptr)
return false;
worker = reinterpret_cast<test_worker *>(apps[i]);
} else if (strcmp(apps[i]->info().type.c_str(), "master") == 0) {
} else if (apps[i]->info().type == "master") {
int index = apps[i]->info().index - 1;
if (index >= masters.size() || masters[index] != nullptr)
return false;
Expand Down
16 changes: 9 additions & 7 deletions src/http/http_message_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@

#include "http_message_parser.h"

#include "utils/fmt_logging.h"
#include "utils/ports.h"
#include "utils/crc.h"
#include <iomanip>

#include "http_server.h"
#include "runtime/api_layer1.h"
#include "runtime/rpc/rpc_message.h"
#include "runtime/rpc/serialization.h"
#include "runtime/api_layer1.h"
#include "http_server.h"
#include <iomanip>
#include "utils/crc.h"
#include "utils/fmt_logging.h"
#include "utils/ports.h"
#include "utils/strings.h"

namespace dsn {

Expand Down Expand Up @@ -98,7 +100,7 @@ http_message_parser::http_message_parser()
[](http_parser *parser, const char *at, size_t length) -> int {
http_message_parser *msg_parser = static_cast<parser_context *>(parser->data)->parser;
msg_parser->_stage = HTTP_ON_HEADER_FIELD;
if (strncmp(at, "Content-Type", length) == 0) {
if (utils::equals(at, "Content-Type", length)) {
msg_parser->_is_field_content_type = true;
}
return 0;
Expand Down
25 changes: 13 additions & 12 deletions src/http/pprof_http_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,26 @@

#ifdef DSN_ENABLE_GPERF

#include <fcntl.h>
#include "pprof_http_service.h"

#include <cstdlib>
#include <chrono>
#include <cstdlib>
#include <fcntl.h>
#include <fstream>
#include <sstream>

#include "pprof_http_service.h"
#include <gperftools/heap-profiler.h>
#include <gperftools/malloc_extension.h>
#include <gperftools/profiler.h>

#include "utils/fmt_logging.h"
#include "runtime/api_layer1.h"
#include "utils/defer.h"
#include "utils/fmt_logging.h"
#include "utils/process_utils.h"
#include "utils/string_conv.h"
#include "utils/defer.h"
#include "utils/timer.h"
#include "utils/string_splitter.h"
#include <gperftools/heap-profiler.h>
#include <gperftools/malloc_extension.h>
#include <gperftools/profiler.h>
#include "utils/strings.h"
#include "utils/timer.h"

namespace dsn {

Expand Down Expand Up @@ -109,9 +110,9 @@ static int extract_symbols_from_binary(std::map<uintptr_t, std::string> &addr_ma
continue;
}
const char *name_begin = sp.field();
if (strncmp(name_begin, "typeinfo ", 9) == 0 || strncmp(name_begin, "VTT ", 4) == 0 ||
strncmp(name_begin, "vtable ", 7) == 0 || strncmp(name_begin, "global ", 7) == 0 ||
strncmp(name_begin, "guard ", 6) == 0) {
if (utils::equals(name_begin, "typeinfo ", 9) || utils::equals(name_begin, "VTT ", 4) ||
utils::equals(name_begin, "vtable ", 7) || utils::equals(name_begin, "global ", 7) ||
utils::equals(name_begin, "guard ", 6)) {
addr_map[addr] = std::string();
continue;
}
Expand Down
9 changes: 5 additions & 4 deletions src/meta/meta_state_service_simple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
#include "meta_state_service_simple.h"

#include <fcntl.h>

#include <stack>
#include <utility>

#include "runtime/task/async_calls.h"
#include "runtime/task/task.h"
#include "utils/filesystem.h"
#include "utils/fmt_logging.h"
#include "utils/strings.h"

namespace dsn {
namespace dist {
Expand Down Expand Up @@ -349,7 +349,7 @@ task_ptr meta_state_service_simple::submit_transaction(
op._node.push_back('/');
std::set<std::string>::iterator iter = snapshot.lower_bound(op._node);
if (iter != snapshot.end() && (*iter).length() >= op._node.length() &&
memcmp((*iter).c_str(), op._node.c_str(), op._node.length()) == 0) {
utils::mequals((*iter).c_str(), op._node.c_str(), op._node.length())) {
// op._node is the prefix of some path, so we regard this directory as not empty
op._result = ERR_INVALID_PARAMETERS;
} else {
Expand Down Expand Up @@ -507,5 +507,6 @@ meta_state_service_simple::~meta_state_service_simple()
}
_quick_map.clear();
}
}
}

} // namespace dist
} // namespace dsn
32 changes: 16 additions & 16 deletions src/meta/server_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,25 @@
* 2016-04-25, Weijie Sun(sunweijie at xiaomi.com), refactor
*/

#include "utils/fmt_logging.h"
#include "common/replica_envs.h"
#include "utils/factory_store.h"
#include "utils/string_conv.h"
#include "utils/strings.h"
#include "runtime/task/task.h"
#include "utils/command_manager.h"
#include "runtime/task/async_calls.h"
#include <sstream>
#include <cinttypes>
#include <sstream>
#include <string>

#include <boost/lexical_cast.hpp>

#include "server_state.h"
#include "server_load_balancer.h"
#include "dump_file.h"
#include "app_env_validator.h"
#include "common/replica_envs.h"
#include "dump_file.h"
#include "meta_bulk_load_service.h"
#include "runtime/task/async_calls.h"
#include "runtime/task/task.h"
#include "server_load_balancer.h"
#include "server_state.h"
#include "utils/command_manager.h"
#include "utils/factory_store.h"
#include "utils/fmt_logging.h"
#include "utils/string_conv.h"
#include "utils/strings.h"

using namespace dsn;

Expand Down Expand Up @@ -112,8 +113,7 @@ void server_state::register_cli_commands()
} else {
const char *target_file = nullptr;
for (int i = 0; i < args.size(); i += 2) {
if (strcmp(args[i].c_str(), "-t") == 0 ||
strcmp(args[i].c_str(), "--target") == 0)
if (args[i] == "-t" || args[i] == "--target")
target_file = args[i + 1].c_str();
}
if (target_file == nullptr) {
Expand Down Expand Up @@ -382,7 +382,7 @@ error_code server_state::restore_from_local_storage(const char *local_path)
CHECK_EQ_MSG(file->read_next_buffer(data), 1, "read format header failed");
_all_apps.clear();

CHECK_EQ(memcmp(data.data(), "binary", 6), 0);
CHECK_TRUE(utils::mequals(data.data(), "binary", 6));
while (true) {
int ans = file->read_next_buffer(data);
CHECK_NE_MSG(ans, -1, "read file failed");
Expand Down Expand Up @@ -434,7 +434,7 @@ error_code server_state::initialize_default_apps()
app_info default_app;
for (int i = 0; i < sections.size(); i++) {
if (strstr(sections[i], "meta_server.apps") == sections[i] ||
strcmp(sections[i], "replication.app") == 0) {
utils::equals(sections[i], "replication.app")) {
const char *s = sections[i];

default_app.status = app_status::AS_CREATING;
Expand Down
4 changes: 3 additions & 1 deletion src/meta/test/dump_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
*/

#include <gtest/gtest.h>

#include "meta/dump_file.h"
#include "utils/strings.h"

TEST(dump_file, read_write)
{
Expand Down Expand Up @@ -78,7 +80,7 @@ TEST(dump_file, read_write)
}

ASSERT_EQ(block_offset, length_blocks.size());
ASSERT_EQ(memcmp(out_buffer.get(), buffer.get(), total_length), 0);
ASSERT_TRUE(dsn::utils::mequals(out_buffer.get(), buffer.get(), total_length));
}

// corrupted end
Expand Down
2 changes: 1 addition & 1 deletion src/meta/test/json_compacity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void meta_service_test_app::json_compacity()
ASSERT_EQ(234, pc.ballot);
ASSERT_TRUE(pc.primary.is_invalid());
ASSERT_EQ(1, pc.secondaries.size());
ASSERT_EQ(0, strcmp(pc.secondaries[0].to_string(), "127.0.0.1:6"));
ASSERT_STREQ("127.0.0.1:6", pc.secondaries[0].to_string());
ASSERT_EQ(157, pc.last_committed_decree);
ASSERT_EQ(0, pc.partition_flags);

Expand Down
Loading