Skip to content

Commit

Permalink
fix: multiple asan errors (#715)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wu Tao authored Jan 11, 2021
1 parent ec34633 commit ab9520d
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 34 deletions.
6 changes: 5 additions & 1 deletion include/dsn/utility/TokenBucket.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,11 @@ class BasicDynamicTokenBucket
assert(burstSize > 0);

if (burstSize < toConsume) {
return boost::none;
// boost::none
// if we use boost::none here, some compilers will generate warning
// that's actually a false positive of "-Wmaybe-uninitialized".
// https://www.boost.org/doc/libs/1_65_1/libs/optional/doc/html/boost_optional/tutorial/gotchas/false_positive_with__wmaybe_uninitialized.html
return boost::make_optional(false, double());
}

while (toConsume > 0) {
Expand Down
3 changes: 3 additions & 0 deletions include/dsn/utils/time_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ static struct tm *get_localtime(uint64_t ts_ms, struct tm *tm_buf)
}

// get time string, which format is yyyy-MM-dd hh:mm:ss.SSS
// NOTE: using char* as output is usually unsafe. Please use std::string as the output argument
// as long as it's possible.
extern void time_ms_to_string(uint64_t ts_ms, char *str);
extern void time_ms_to_string(uint64_t ts_ms, std::string &str);

// get date string with format of 'yyyy-MM-dd' from given timestamp
inline void time_ms_to_date(uint64_t ts_ms, char *str, int len)
Expand Down
7 changes: 4 additions & 3 deletions src/aio/test/aio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include <dsn/tool-api/async_calls.h>
#include <dsn/utility/filesystem.h>
#include <dsn/utility/smart_pointers.h>

#include <gtest/gtest.h>

Expand Down Expand Up @@ -145,9 +146,9 @@ TEST(core, operation_failed)
auto fp = file::open("tmp_test_file", O_WRONLY, 0600);
EXPECT_TRUE(fp == nullptr);

::dsn::error_code *err = new ::dsn::error_code;
size_t *count = new size_t;
auto io_callback = [err, count](::dsn::error_code e, size_t n) {
auto err = dsn::make_unique<dsn::error_code>();
auto count = dsn::make_unique<size_t>();
auto io_callback = [&err, &count](::dsn::error_code e, size_t n) {
*err = e;
*count = n;
};
Expand Down
9 changes: 0 additions & 9 deletions src/runtime/rpc/rpc_message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,6 @@
* THE SOFTWARE.
*/

/*
* Description:
* What is this file about?
*
* Revision history:
* xxxx-xx-xx, author, first version
* xxxx-xx-xx, author, fix bug about xxx
*/

#include <dsn/utility/ports.h>
#include <dsn/utility/crc.h>
#include <dsn/utility/transient_memory.h>
Expand Down
13 changes: 1 addition & 12 deletions src/utils/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,7 @@ using namespace tools;
DSN_REGISTER_COMPONENT_PROVIDER(screen_logger, "dsn::tools::screen_logger");
DSN_REGISTER_COMPONENT_PROVIDER(simple_logger, "dsn::tools::simple_logger");

std::function<std::string()> log_prefixed_message_func = []() {
static thread_local std::string prefixed_message;

static thread_local std::once_flag flag;
std::call_once(flag, [&]() {
prefixed_message.resize(23);
int tid = dsn::utils::get_current_tid();
sprintf(const_cast<char *>(prefixed_message.c_str()), "unknown.io-thrd.%05d: ", tid);
});

return prefixed_message;
};
std::function<std::string()> log_prefixed_message_func = []() -> std::string { return ": "; };

void set_log_prefixed_message_func(std::function<std::string()> func)
{
Expand Down
19 changes: 10 additions & 9 deletions src/utils/simple_logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <dsn/utility/filesystem.h>
#include <dsn/utility/flags.h>
#include <dsn/utils/time_utils.h>
#include <fmt/format.h>

namespace dsn {
namespace tools {
Expand Down Expand Up @@ -58,17 +59,17 @@ static void print_header(FILE *fp, dsn_log_level_t log_level)
static char s_level_char[] = "IDWEF";

uint64_t ts = dsn_now_ns();
char str[24];
dsn::utils::time_ms_to_string(ts / 1000000, str);
std::string time_str;
dsn::utils::time_ms_to_string(ts / 1000000, time_str);

int tid = dsn::utils::get_current_tid();
fprintf(fp,
"%c%s (%" PRIu64 " %04x) %s",
s_level_char[log_level],
str,
ts,
tid,
log_prefixed_message_func().c_str());
fmt::print(fp,
"{}{} ({} {}) {}",
s_level_char[log_level],
time_str,
ts,
tid,
log_prefixed_message_func());
}

screen_logger::screen_logger(bool short_header) : logging_provider("./")
Expand Down
1 change: 1 addition & 0 deletions src/utils/test/priority_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ TEST(core, blocking_priority_queue)
ASSERT_EQ(0, ct);
ASSERT_EQ(0, d->priority);
ASSERT_EQ(10, d->queue_index);
delete d;

bool flag = false;

Expand Down
11 changes: 11 additions & 0 deletions src/utils/time_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,16 @@ namespace utils {
fmt::format_to(str, "{:%Y-%m-%d %H:%M:%S}.{}", *ret, static_cast<uint32_t>(ts_ms % 1000));
}

/*extern*/ void time_ms_to_string(uint64_t ts_ms, std::string &str)
{
str.clear();
struct tm tmp;
auto ret = get_localtime(ts_ms, &tmp);
fmt::format_to(std::back_inserter(str),
"{:%Y-%m-%d %H:%M:%S}.{}",
*ret,
static_cast<uint32_t>(ts_ms % 1000));
}

} // namespace utils
} // namespace dsn

0 comments on commit ab9520d

Please sign in to comment.