Skip to content

Commit

Permalink
Narrowing convension (OpenAtomFoundation#1730)
Browse files Browse the repository at this point in the history
* fix: fix narrowing convension

* fix: make codes better

---------

Co-authored-by: longfar <longfar@ncy.com>
  • Loading branch information
longfar-ncy and longfar committed Jul 16, 2023
1 parent b415c9b commit baf7e7b
Show file tree
Hide file tree
Showing 67 changed files with 496 additions and 483 deletions.
7 changes: 2 additions & 5 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ Checks: '
google-*,
modernize-*,
performance-*,
-portability-*,
portability-*,
-readability-*,
-cppcoreguidelines-*,
-bugprone-easily-swappable-parameters,
-bugprone-implicit-widening-of-multiplication-result,
-bugprone-narrowing-conversions,
-bugprone-reserved-identifier,
-bugprone-signed-char-misuse,
-bugprone-suspicious-include,
-bugprone-unhandled-self-assignment,
-bugprone-exception-escape,
-bugprone-branch-clone,
-clang-analyzer-cplusplus.NewDelete,
-clang-analyzer-cplusplus.NewDeleteLeaks,
Expand Down
9 changes: 7 additions & 2 deletions include/pika_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ enum CmdFlags {

void inline RedisAppendContent(std::string& str, const std::string& value);
void inline RedisAppendLen(std::string& str, int64_t ori, const std::string& prefix);
void inline RedisAppendLenUint64(std::string& str, uint64_t ori, const std::string& prefix) {
RedisAppendLen(str, static_cast<int64_t>(ori), prefix);
}

const std::string kNewLine = "\r\n";

Expand Down Expand Up @@ -378,11 +381,13 @@ class CmdRes {

// Inline functions for Create Redis protocol
void AppendStringLen(int64_t ori) { RedisAppendLen(message_, ori, "$"); }
void AppendStringLenUint64(uint64_t ori) { RedisAppendLenUint64(message_, ori, "$"); }
void AppendArrayLen(int64_t ori) { RedisAppendLen(message_, ori, "*"); }
void AppendArrayLenUint64(uint64_t ori) { RedisAppendLenUint64(message_, ori, "*"); }
void AppendInteger(int64_t ori) { RedisAppendLen(message_, ori, ":"); }
void AppendContent(const std::string& value) { RedisAppendContent(message_, value); }
void AppendString(const std::string& value) {
AppendStringLen(value.size());
AppendStringLenUint64(value.size());
AppendContent(value);
}
void AppendStringRaw(const std::string& value) { message_.append(value); }
Expand Down Expand Up @@ -473,7 +478,7 @@ class Cmd : public std::enable_shared_from_this<Cmd> {
void InternalProcessCommand(const std::shared_ptr<Slot>& slot, const std::shared_ptr<SyncMasterSlot>& sync_slot,
const HintKeys& hint_key);
void DoCommand(const std::shared_ptr<Slot>& slot, const HintKeys& hint_key);
bool CheckArg(int num) const;
bool CheckArg(uint64_t num) const;
void LogCommand() const;

std::string name_;
Expand Down
2 changes: 1 addition & 1 deletion include/pika_define.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ class RmNode : public Node {
const std::string& DBName() const { return slot_info_.db_name_; }
uint32_t SlotId() const { return slot_info_.slot_id_; }
const SlotInfo& NodeSlotInfo() const { return slot_info_; }
void SetSessionId(uint32_t session_id) { session_id_ = session_id; }
void SetSessionId(int32_t session_id) { session_id_ = session_id; }
int32_t SessionId() const { return session_id_; }
std::string ToString() const {
return "slot=" + DBName() + "_" + std::to_string(SlotId()) + ",ip_port=" + Ip() + ":" +
Expand Down
2 changes: 1 addition & 1 deletion include/pika_dispatch_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class PikaDispatchThread {
~PikaDispatchThread();
int StartThread();

int64_t ThreadClientList(std::vector<ClientInfo>* clients);
uint64_t ThreadClientList(std::vector<ClientInfo>* clients);

bool ClientKill(const std::string& ip_port);
void ClientKillAll();
Expand Down
2 changes: 1 addition & 1 deletion include/pika_repl_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class PikaReplClient {

private:
size_t GetHashIndex(const std::string& key, bool upper_half);
void UpdateNextAvail() { next_avail_ = (next_avail_ + 1) % bg_workers_.size(); }
void UpdateNextAvail() { next_avail_ = (next_avail_ + 1) % static_cast<int32_t>(bg_workers_.size()); }

std::unique_ptr<PikaReplClientThread> client_thread_;
int next_avail_ = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/net/src/backend_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ void BackendThread::NotifyClose(const int fd) {
void BackendThread::ProcessNotifyEvents(const NetFiredEvent* pfe) {
if (pfe->mask & kReadable) {
char bb[2048];
int32_t nread = read(net_multiplexer_->NotifyReceiveFd(), bb, 2048);
int64_t nread = read(net_multiplexer_->NotifyReceiveFd(), bb, 2048);
if (nread == 0) {
return;
} else {
Expand Down Expand Up @@ -373,7 +373,7 @@ void* BackendThread::ThreadMain() {
if (cron_interval_ > 0) {
gettimeofday(&now, nullptr);
if (when.tv_sec > now.tv_sec || (when.tv_sec == now.tv_sec && when.tv_usec > now.tv_usec)) {
timeout = (when.tv_sec - now.tv_sec) * 1000 + (when.tv_usec - now.tv_usec) / 1000;
timeout = static_cast<int32_t>((when.tv_sec - now.tv_sec) * 1000 + (when.tv_usec - now.tv_usec) / 1000);
} else {
// do user defined cron
handle_->CronHandle();
Expand Down
4 changes: 2 additions & 2 deletions src/net/src/bg_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ void BGThread::Schedule(void (*function)(void*), void* arg) {

void BGThread::QueueSize(int* pri_size, int* qu_size) {
std::lock_guard lock(mu_);
*pri_size = timer_queue_.size();
*qu_size = queue_.size();
*pri_size = static_cast<int32_t>(timer_queue_.size());
*qu_size = static_cast<int32_t>(queue_.size());
}

void BGThread::QueueClear() {
Expand Down
4 changes: 2 additions & 2 deletions src/net/src/client_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ void ClientThread::NotifyWrite(const std::string& ip_port) {
void ClientThread::ProcessNotifyEvents(const NetFiredEvent* pfe) {
if (pfe->mask & kReadable) {
char bb[2048];
int32_t nread = read(net_multiplexer_->NotifyReceiveFd(), bb, 2048);
int64_t nread = read(net_multiplexer_->NotifyReceiveFd(), bb, 2048);
if (nread == 0) {
return;
} else {
Expand Down Expand Up @@ -381,7 +381,7 @@ void* ClientThread::ThreadMain() {
if (cron_interval_ > 0) {
gettimeofday(&now, nullptr);
if (when.tv_sec > now.tv_sec || (when.tv_sec == now.tv_sec && when.tv_usec > now.tv_usec)) {
timeout = (when.tv_sec - now.tv_sec) * 1000 + (when.tv_usec - now.tv_usec) / 1000;
timeout = static_cast<int32_t>((when.tv_sec - now.tv_sec) * 1000 + (when.tv_usec - now.tv_usec) / 1000);
} else {
// do user defined cron
handle_->CronHandle();
Expand Down
4 changes: 2 additions & 2 deletions src/net/src/holy_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ HolyThread::~HolyThread() { Cleanup(); }

int HolyThread::conn_num() const {
std::shared_lock l(rwlock_);
return conns_.size();
return static_cast<int32_t>(conns_.size());
}

std::vector<ServerThread::ConnInfo> HolyThread::conns_info() const {
Expand Down Expand Up @@ -279,7 +279,7 @@ bool HolyThread::KillConn(const std::string& ip_port) {
void HolyThread::ProcessNotifyEvents(const net::NetFiredEvent* pfe) {
if (pfe->mask & kReadable) {
char bb[2048];
int32_t nread = read(net_multiplexer_->NotifyReceiveFd(), bb, 2048);
int64_t nread = read(net_multiplexer_->NotifyReceiveFd(), bb, 2048);
if (nread == 0) {
return;
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/net/src/http_conn.cc
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ int HTTPRequest::ParseHeader() {
// Haven't find header
return 0;
}
int header_len = sep_pos - rbuf_ + 4;
auto header_len = static_cast<int32_t>(sep_pos - rbuf_ + 4);
int remain_size = header_len;
if (remain_size <= 5) {
// Header error
Expand Down
6 changes: 3 additions & 3 deletions src/net/src/net_cli.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ Status NetCli::Connect(const std::string& ip, const int port, const std::string&
static int PollFd(int fd, int events, int ms) {
pollfd fds[1];
fds[0].fd = fd;
fds[0].events = events;
fds[0].events = static_cast<int16_t>(events);
fds[0].revents = 0;

int ret = ::poll(fds, 1, ms);
Expand All @@ -170,7 +170,7 @@ static int CheckSockAliveness(int fd) {

ret = PollFd(fd, POLLIN | POLLPRI, 0);
if (0 < ret) {
int num = ::recv(fd, buf, 1, MSG_PEEK);
int64_t num = ::recv(fd, buf, 1, MSG_PEEK);
if (num == 0) {
return -1;
}
Expand Down Expand Up @@ -212,7 +212,7 @@ int NetCli::CheckAliveness() {
Status NetCli::SendRaw(void* buf, size_t count) {
char* wbuf = reinterpret_cast<char*>(buf);
size_t nleft = count;
int pos = 0;
ssize_t pos = 0;
ssize_t nwritten;

while (nleft > 0) {
Expand Down
12 changes: 7 additions & 5 deletions src/net/src/net_pubsub.cc
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,8 @@ void PubSubThread::PubSubChannels(const std::string& pattern, std::vector<std::s
} else {
std::lock_guard l(channel_mutex_);
for (auto& channel : pubsub_channel_) {
if (pstd::stringmatchlen(channel.first.c_str(), channel.first.size(), pattern.c_str(), pattern.size(), 0)) {
if (pstd::stringmatchlen(channel.first.c_str(), static_cast<int32_t>(channel.first.size()),
pattern.c_str(), static_cast<int32_t>(pattern.size()), 0)) {
if (!channel.second.empty()) {
result->push_back(channel.first);
}
Expand All @@ -315,7 +316,7 @@ void PubSubThread::PubSubNumSub(const std::vector<std::string>& channels,
subscribed = 0;
for (auto& channel : pubsub_channel_) {
if (channel.first == i) {
subscribed = channel.second.size();
subscribed = static_cast<int32_t>(channel.second.size());
}
}
result->push_back(std::make_pair(i, subscribed));
Expand All @@ -326,7 +327,7 @@ int PubSubThread::PubSubNumPat() {
int subscribed = 0;
std::lock_guard l(pattern_mutex_);
for (auto& channel : pubsub_pattern_) {
subscribed += channel.second.size();
subscribed += static_cast<int32_t>(channel.second.size());
}
return subscribed;
}
Expand Down Expand Up @@ -402,8 +403,9 @@ void* PubSubThread::ThreadMain() {

// Send message to a channel pattern's clients
pattern_mutex_.lock();
for (auto & it : pubsub_pattern_) {
if (pstd::stringmatchlen(it.first.c_str(), it.first.size(), channel.c_str(), channel.size(), 0)) {
for (auto& it : pubsub_pattern_) {
if (pstd::stringmatchlen(it.first.c_str(), static_cast<int32_t>(it.first.size()),
channel.c_str(), static_cast<int32_t>(channel.size()), 0)) {
for (size_t i = 0; i < it.second.size(); i++) {
if (!IsReady(it.second[i]->fd())) {
continue;
Expand Down
10 changes: 5 additions & 5 deletions src/net/src/pb_cli.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ PbCli::~PbCli() {
Status PbCli::Send(void* msg) {
auto req = reinterpret_cast<google::protobuf::Message*>(msg);

int wbuf_len = req->ByteSizeLong();
req->SerializeToArray(wbuf_ + kCommandHeaderLength, wbuf_len);
uint32_t len = htonl(wbuf_len);
memcpy(wbuf_, &len, sizeof(uint32_t));
size_t wbuf_len = req->ByteSizeLong();
req->SerializeToArray(wbuf_ + kCommandHeaderLength, static_cast<int32_t>(wbuf_len));
uint32_t len = htonl(static_cast<uint32_t>(wbuf_len));
memcpy(wbuf_, &len, sizeof(len));
wbuf_len += kCommandHeaderLength;

return NetCli::SendRaw(wbuf_, wbuf_len);
Expand All @@ -80,7 +80,7 @@ Status PbCli::Recv(void* msg_res) {
return s;
}

if (!res->ParseFromArray(rbuf_, packet_len)) {
if (!res->ParseFromArray(rbuf_, static_cast<int32_t>(packet_len))) {
return Status::Corruption("PbCli::Recv Protobuf ParseFromArray error");
}
return Status::OK();
Expand Down
8 changes: 4 additions & 4 deletions src/net/src/pb_conn.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ ReadStatus PbConn::GetRequest() {
uint32_t integer = 0;
memcpy(reinterpret_cast<char*>(&integer), rbuf_, sizeof(uint32_t));
header_len_ = ntohl(integer);
remain_packet_len_ = header_len_;
remain_packet_len_ = static_cast<int32_t>(header_len_);
connStatus_ = kPacket;
continue;
}
Expand Down Expand Up @@ -80,8 +80,8 @@ ReadStatus PbConn::GetRequest() {
} else if (nread == 0) {
return kReadClose;
}
cur_pos_ += nread;
remain_packet_len_ -= nread;
cur_pos_ += static_cast<uint32_t>(nread);
remain_packet_len_ -= static_cast<int32_t>(nread);
if (remain_packet_len_ == 0) {
connStatus_ = kComplete;
continue;
Expand Down Expand Up @@ -178,7 +178,7 @@ void PbConn::BuildInternalTag(const std::string& resp, std::string* tag) {
void PbConn::TryResizeBuffer() {
struct timeval now;
gettimeofday(&now, nullptr);
int idletime = now.tv_sec - last_interaction().tv_sec;
time_t idletime = now.tv_sec - last_interaction().tv_sec;
if (rbuf_len_ > PB_IOBUF_LEN && ((rbuf_len_ / (cur_pos_ + 1)) > 2 || idletime > 2)) {
uint32_t new_size = ((cur_pos_ + PB_IOBUF_LEN) / PB_IOBUF_LEN) * PB_IOBUF_LEN;
if (new_size < rbuf_len_) {
Expand Down
28 changes: 14 additions & 14 deletions src/net/src/redis_cli.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Status RedisCli::Send(void* msg) {
const char* wbuf = storage->data();
size_t nleft = storage->size();

int wbuf_pos = 0;
ssize_t wbuf_pos = 0;

ssize_t nwritten;
while (nleft > 0) {
Expand Down Expand Up @@ -164,15 +164,15 @@ ssize_t RedisCli::BufferRead() {
return REDIS_EREAD_NULL;
}

rbuf_offset_ += nread;
rbuf_offset_ += static_cast<int32_t>(nread);
return nread;
}
}

/* Find pointer to \r\n. */
static char* seekNewline(char* s, size_t len) {
int pos = 0;
int _len = len - 1;
auto _len = static_cast<int32_t>(len - 1);

/* Position should be < len-1 because the character at "pos" should be
* followed by a \n. Note that strchr cannot be used because it doesn't
Expand Down Expand Up @@ -252,8 +252,8 @@ int RedisCli::ProcessBulkItem() {
p = rbuf_ + rbuf_pos_;
s = seekNewline(p, rbuf_offset_);
if (s) {
bytelen = s - p + 2; /* include \r\n */
len = readLongLong(p);
bytelen = static_cast<int32_t>(s - p + 2); /* include \r\n */
len = static_cast<int32_t>(readLongLong(p));

if (len == -1) {
elements_--;
Expand All @@ -280,7 +280,7 @@ int RedisCli::ProcessMultiBulkItem() {
int len;

if (p = ReadLine(&len); p) {
elements_ = readLongLong(p);
elements_ = static_cast<int32_t>(readLongLong(p));
return REDIS_OK;
}

Expand All @@ -294,7 +294,7 @@ int RedisCli::GetReply() {
while (elements_ > 0) {
// Should read again
if (rbuf_offset_ == 0 || result == REDIS_HALF) {
if ((result = BufferRead()) < 0) {
if ((result = static_cast<int32_t>(BufferRead())) < 0) {
return result;
}
}
Expand All @@ -312,8 +312,8 @@ char* RedisCli::ReadBytes(unsigned int bytes) {
char* p = nullptr;
if (static_cast<unsigned int>(rbuf_offset_) >= bytes) {
p = rbuf_ + rbuf_pos_;
rbuf_pos_ += bytes;
rbuf_offset_ -= bytes;
rbuf_pos_ += static_cast<int32_t>(bytes);
rbuf_offset_ -= static_cast<int32_t>(bytes);
}
return p;
}
Expand All @@ -326,7 +326,7 @@ char* RedisCli::ReadLine(int* _len) {
p = rbuf_ + rbuf_pos_;
s = seekNewline(p, rbuf_offset_);
if (s) {
len = s - (rbuf_ + rbuf_pos_);
len = static_cast<int32_t>(s - (rbuf_ + rbuf_pos_));
rbuf_pos_ += len + 2; /* skip \r\n */
rbuf_offset_ -= len + 2;
if (_len) {
Expand Down Expand Up @@ -411,7 +411,7 @@ static int intlen(int i) {
}

// Helper that calculates the bulk length given a certain string length.
static size_t bulklen(size_t len) { return 1 + intlen(len) + 2 + len + 2; }
static size_t bulklen(size_t len) { return 1 + intlen(static_cast<int32_t>(len)) + 2 + len + 2; }

int redisvFormatCommand(std::string* cmd, const char* format, va_list ap) {
const char* c = format;
Expand Down Expand Up @@ -573,7 +573,7 @@ int redisvFormatCommand(std::string* cmd, const char* format, va_list ap) {
}

/* Add bytes needed to hold multi bulk count */
totlen += 1 + intlen(args.size()) + 2;
totlen += 1 + intlen(static_cast<int32_t>(args.size())) + 2;

/* Build the command at protocol level */
cmd->clear();
Expand All @@ -591,7 +591,7 @@ int redisvFormatCommand(std::string* cmd, const char* format, va_list ap) {
}
assert(cmd->size() == totlen);

return totlen;
return static_cast<int32_t>(totlen);
}

int redisvAppendCommand(std::string* cmd, const char* format, va_list ap) {
Expand All @@ -606,7 +606,7 @@ int redisvAppendCommand(std::string* cmd, const char* format, va_list ap) {
int redisFormatCommandArgv(RedisCmdArgsType argv, std::string* cmd) {
size_t argc = argv.size();

int totlen = 1 + intlen(argc) + 2;
size_t totlen = 1 + intlen(static_cast<int32_t>(argc)) + 2;
for (size_t i = 0; i < argc; i++) {
totlen += bulklen(argv[i].size());
}
Expand Down
Loading

0 comments on commit baf7e7b

Please sign in to comment.