Skip to content

Commit b46138e

Browse files
authored
Fix warnings on Windows (#73)
1 parent f9a4de5 commit b46138e

15 files changed

+99
-52
lines changed

trantor/net/EventLoop.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ void EventLoop::loop()
164164
#ifdef __linux__
165165
poller_->poll(kPollTimeMs, &activeChannels_);
166166
#else
167-
poller_->poll(timerQueue_->getTimeout(), &activeChannels_);
167+
poller_->poll(static_cast<int>(timerQueue_->getTimeout()),
168+
&activeChannels_);
168169
timerQueue_->processTimers();
169170
#endif
170171
// TODO sort channel by priority
@@ -302,12 +303,13 @@ void EventLoop::wakeup()
302303
}
303304
void EventLoop::wakeupRead()
304305
{
305-
uint64_t tmp;
306306
ssize_t ret = 0;
307307
#ifdef __linux__
308+
uint64_t tmp;
308309
ret = read(wakeupFd_, &tmp, sizeof(tmp));
309310
#elif defined _WIN32
310311
#else
312+
uint64_t tmp;
311313
ret = read(wakeupFd_[0], &tmp, sizeof(tmp));
312314
#endif
313315
if (ret < 0)

trantor/net/InetAddress.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@ struct in6_addr_uint
2727
#endif
2828

2929
// INADDR_ANY use (type)value casting.
30-
#pragma GCC diagnostic ignored "-Wold-style-cast"
3130
static const in_addr_t kInaddrAny = INADDR_ANY;
3231
static const in_addr_t kInaddrLoopback = INADDR_LOOPBACK;
33-
#pragma GCC diagnostic error "-Wold-style-cast"
3432

3533
// /* Structure describing an Internet socket address. */
3634
// struct sockaddr_in {
@@ -115,7 +113,7 @@ std::string InetAddress::toIpPort() const
115113
{
116114
char buf[64] = "";
117115
uint16_t port = ntohs(addr_.sin_port);
118-
sprintf(buf, ":%u", port);
116+
snprintf(buf, sizeof(buf), ":%u", port);
119117
return toIp() + std::string(buf);
120118
}
121119
bool InetAddress::isIntranetIp() const

trantor/net/TcpServer.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ void TcpServer::start()
116116
timingWheelMap_[loop_] =
117117
std::make_shared<TimingWheel>(loop_,
118118
idleTimeout_,
119-
1,
119+
1.0F,
120120
idleTimeout_ < 500
121121
? idleTimeout_ + 1
122122
: 100);
@@ -130,7 +130,7 @@ void TcpServer::start()
130130
timingWheelMap_[poolLoop] =
131131
std::make_shared<TimingWheel>(poolLoop,
132132
idleTimeout_,
133-
1,
133+
1.0F,
134134
idleTimeout_ < 500
135135
? idleTimeout_ + 1
136136
: 100);

trantor/net/inner/NormalResolver.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ void NormalResolver::resolve(const std::string &hostname,
2626
if (iter != globalCache().end())
2727
{
2828
auto &cachedAddr = iter->second;
29-
if (timeout_ == 0 ||
30-
cachedAddr.second.after(timeout_) > trantor::Date::date())
29+
if (timeout_ == 0 || cachedAddr.second.after(static_cast<double>(
30+
timeout_)) > trantor::Date::date())
3131
{
3232
struct sockaddr_in addr;
3333
memset(&addr, 0, sizeof addr);

trantor/net/inner/Socket.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ int Socket::accept(InetAddress *peeraddr)
8989
&size,
9090
SOCK_NONBLOCK | SOCK_CLOEXEC);
9191
#else
92-
int connfd = ::accept(sockFd_, (struct sockaddr *)&addr6, &size);
92+
int connfd =
93+
static_cast<int>(::accept(sockFd_, (struct sockaddr *)&addr6, &size));
9394
setNonBlockAndCloseOnExec(connfd);
9495
#endif
9596
if (connfd >= 0)
@@ -114,7 +115,7 @@ int Socket::read(char *buffer, uint64_t len)
114115
#ifndef _WIN32
115116
return ::read(sockFd_, buffer, len);
116117
#else
117-
return recv(sockFd_, buffer, len, 0);
118+
return recv(sockFd_, buffer, static_cast<int>(len), 0);
118119
#endif
119120
}
120121

trantor/net/inner/Socket.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Socket : NonCopyable
3535
SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
3636
IPPROTO_TCP);
3737
#else
38-
int sock = ::socket(family, SOCK_STREAM, IPPROTO_TCP);
38+
int sock = static_cast<int>(::socket(family, SOCK_STREAM, IPPROTO_TCP));
3939
setNonBlockAndCloseOnExec(sock);
4040
#endif
4141
if (sock < 0)

trantor/net/inner/TcpConnectionImpl.cc

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,21 +109,33 @@ std::shared_ptr<SSLContext> newSSLServerContext(const std::string &certPath,
109109
auto r = SSL_CTX_use_certificate_chain_file(ctx->get(), certPath.c_str());
110110
if (!r)
111111
{
112+
#ifndef _MSC_VER
112113
LOG_FATAL << strerror(errno);
114+
#else
115+
LOG_FATAL << strerror_tl(errno);
116+
#endif
113117
abort();
114118
}
115119
r = SSL_CTX_use_PrivateKey_file(ctx->get(),
116120
keyPath.c_str(),
117121
SSL_FILETYPE_PEM);
118122
if (!r)
119123
{
124+
#ifndef _MSC_VER
120125
LOG_FATAL << strerror(errno);
126+
#else
127+
LOG_FATAL << strerror_tl(errno);
128+
#endif
121129
abort();
122130
}
123131
r = SSL_CTX_check_private_key(ctx->get());
124132
if (!r)
125133
{
134+
#ifndef _MSC_VER
126135
LOG_FATAL << strerror(errno);
136+
#else
137+
LOG_FATAL << strerror_tl(errno);
138+
#endif
127139
abort();
128140
}
129141
return ctx;
@@ -329,7 +341,7 @@ void TcpConnectionImpl::readCallback()
329341
readLength = readBuffer_.writableBytes();
330342
rd = SSL_read(sslEncryptionPtr_->sslPtr_->get(),
331343
readBuffer_.beginWrite(),
332-
readLength);
344+
static_cast<int>(readLength));
333345
LOG_TRACE << "ssl read:" << rd << " bytes";
334346
if (rd <= 0)
335347
{
@@ -947,8 +959,15 @@ void TcpConnectionImpl::sendFile(const char *fileName,
947959

948960
sendFile(fd, offset, length);
949961
#else
962+
#ifndef _MSC_VER
950963
auto fp = fopen(fileName, "rb");
951-
964+
#else
965+
FILE *fp;
966+
if (fopen_s(&fp, fileName, "rb") != 0)
967+
{
968+
fp = nullptr;
969+
}
970+
#endif
952971
if (fp == nullptr)
953972
{
954973
LOG_SYSERR << fileName << " open error";
@@ -987,7 +1006,7 @@ void TcpConnectionImpl::sendFile(FILE *fp, size_t offset, size_t length)
9871006
BufferNodePtr node(new BufferNode);
9881007
node->sendFp_ = fp;
9891008
#endif
990-
node->offset_ = offset;
1009+
node->offset_ = static_cast<off_t>(offset);
9911010
node->fileBytesToSend_ = length;
9921011
if (loop_->isInLoopThread())
9931012
{
@@ -1113,8 +1132,8 @@ void TcpConnectionImpl::sendFileInLoop(const BufferNodePtr &filePtr)
11131132
if (nSend >= 0)
11141133
{
11151134
filePtr->fileBytesToSend_ -= nSend;
1116-
filePtr->offset_ += nSend;
1117-
if (nSend < n)
1135+
filePtr->offset_ += static_cast<off_t>(nSend);
1136+
if (static_cast<size_t>(nSend) < n)
11181137
{
11191138
if (!ioChannelPtr_->isWriting())
11201139
{
@@ -1175,7 +1194,7 @@ ssize_t TcpConnectionImpl::writeInLoop(const char *buffer, size_t length)
11751194
return write(socketPtr_->fd(), buffer, length);
11761195
#else
11771196
errno = 0;
1178-
return ::send(socketPtr_->fd(), buffer, length, 0);
1197+
return ::send(socketPtr_->fd(), buffer, static_cast<int>(length), 0);
11791198
#endif
11801199
#ifdef USE_OPENSSL
11811200
}
@@ -1208,7 +1227,7 @@ ssize_t TcpConnectionImpl::writeInLoop(const char *buffer, size_t length)
12081227
ERR_clear_error();
12091228
auto sendLen = SSL_write(sslEncryptionPtr_->sslPtr_->get(),
12101229
sslEncryptionPtr_->sendBufferPtr_->data(),
1211-
len);
1230+
static_cast<int>(len));
12121231
if (sendLen <= 0)
12131232
{
12141233
int sslerr =

trantor/utils/AsyncFileLogger.cc

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ namespace trantor
2828
{
2929
static constexpr std::chrono::seconds kLogFlushTimeout{1};
3030
static constexpr size_t kMemBufferSize{4 * 1024 * 1024};
31+
extern const char *strerror_tl(int savedErrno);
3132
} // namespace trantor
3233

3334
using namespace trantor;
@@ -89,9 +90,10 @@ void AsyncFileLogger::output(const char *msg, const uint64_t len)
8990
{
9091
char logErr[128];
9192
auto strlen =
92-
sprintf(logErr,
93-
"%llu log information is lost\n",
94-
static_cast<long long unsigned int>(lostCounter_));
93+
snprintf(logErr,
94+
sizeof(logErr),
95+
"%llu log information is lost\n",
96+
static_cast<long long unsigned int>(lostCounter_));
9597
lostCounter_ = 0;
9698
logBufferPtr_->append(logErr, strlen);
9799
}
@@ -179,10 +181,17 @@ AsyncFileLogger::LoggerFile::LoggerFile(const std::string &filePath,
179181
fileExtName_(fileExtName)
180182
{
181183
fileFullName_ = filePath + fileBaseName + fileExtName;
184+
#ifndef _MSC_VER
182185
fp_ = fopen(fileFullName_.c_str(), "a");
186+
#else
187+
if (fopen_s(&fp_, fileFullName_.c_str(), "a") != 0)
188+
{
189+
fp_ = nullptr;
190+
}
191+
#endif
183192
if (fp_ == nullptr)
184193
{
185-
std::cout << strerror(errno) << std::endl;
194+
std::cout << strerror_tl(errno) << std::endl;
186195
}
187196
}
188197

@@ -216,10 +225,11 @@ AsyncFileLogger::LoggerFile::~LoggerFile()
216225
if (fp_)
217226
{
218227
fclose(fp_);
219-
char seq[10];
220-
sprintf(seq,
221-
".%06llu",
222-
static_cast<long long unsigned int>(fileSeq_ % 1000000));
228+
char seq[12];
229+
snprintf(seq,
230+
sizeof(seq),
231+
".%06llu",
232+
static_cast<long long unsigned int>(fileSeq_ % 1000000));
223233
++fileSeq_;
224234
std::string newName =
225235
filePath_ + fileBaseName_ + "." +

trantor/utils/Date.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ int gettimeofday(timeval *tp, void *tzp)
4242
tm.tm_sec = wtm.wSecond;
4343
tm.tm_isdst = -1;
4444
clock = mktime(&tm);
45-
tp->tv_sec = clock;
45+
tp->tv_sec = static_cast<long>(clock);
4646
tp->tv_usec = wtm.wMilliseconds * 1000;
4747

4848
return (0);
@@ -64,7 +64,8 @@ const Date Date::date()
6464
}
6565
const Date Date::after(double second) const
6666
{
67-
return Date(microSecondsSinceEpoch_ + second * MICRO_SECONDS_PRE_SEC);
67+
return Date(static_cast<int64_t>(microSecondsSinceEpoch_ +
68+
second * MICRO_SECONDS_PRE_SEC));
6869
}
6970
const Date Date::roundSecond() const
7071
{
@@ -155,10 +156,10 @@ std::string Date::toCustomedFormattedString(const std::string &fmtStr,
155156
strftime(buf, sizeof(buf), fmtStr.c_str(), &tm_time);
156157
if (!showMicroseconds)
157158
return std::string(buf);
158-
char decimals[10] = {0};
159+
char decimals[12] = {0};
159160
int microseconds =
160161
static_cast<int>(microSecondsSinceEpoch_ % MICRO_SECONDS_PRE_SEC);
161-
sprintf(decimals, ".%06d", microseconds);
162+
snprintf(decimals, sizeof(decimals), ".%06d", microseconds);
162163
return std::string(buf) + decimals;
163164
}
164165
void Date::toCustomedFormattedString(const std::string &fmtStr,
@@ -287,10 +288,10 @@ std::string Date::toCustomedFormattedStringLocal(const std::string &fmtStr,
287288
strftime(buf, sizeof(buf), fmtStr.c_str(), &tm_time);
288289
if (!showMicroseconds)
289290
return std::string(buf);
290-
char decimals[10] = {0};
291+
char decimals[12] = {0};
291292
int microseconds =
292293
static_cast<int>(microSecondsSinceEpoch_ % MICRO_SECONDS_PRE_SEC);
293-
sprintf(decimals, ".%06d", microseconds);
294+
snprintf(decimals, sizeof(decimals), ".%06d", microseconds);
294295
return std::string(buf) + decimals;
295296
}
296297
Date::Date(unsigned int year,

trantor/utils/Funcs.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,21 @@
1313
*/
1414

1515
#pragma once
16-
17-
#ifdef _WIN32
18-
#include <winsock2.h>
19-
#else
20-
#include <netinet/in.h>
21-
#endif
16+
#include <algorithm>
2217
namespace trantor
2318
{
2419
inline uint64_t hton64(uint64_t n)
2520
{
26-
return (((uint64_t)htonl(n)) << 32) | htonl(n >> 32);
21+
static const int one = 1;
22+
static const char sig = *(char*)&one;
23+
if (sig == 0)
24+
return n; // for big endian machine just return the input
25+
char* ptr = reinterpret_cast<char*>(&n);
26+
std::reverse(ptr, ptr + sizeof(uint64_t));
27+
return n;
2728
}
2829
inline uint64_t ntoh64(uint64_t n)
2930
{
30-
return (((uint64_t)ntohl(n)) << 32) | ntohl(n >> 32);
31+
return hton64(n);
3132
}
3233
} // namespace trantor

trantor/utils/LogStream.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ class LogStream : NonCopyable
200200
return *this;
201201
}
202202

203-
void append(const char *data, int len)
203+
void append(const char *data, size_t len)
204204
{
205205
if (exBuffer_.empty())
206206
{

trantor/utils/Logger.cc

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,13 @@ class T
4242

4343
const char *strerror_tl(int savedErrno)
4444
{
45+
#ifndef _MSC_VER
4546
return strerror(savedErrno);
47+
#else
48+
static thread_local char errMsg[64];
49+
(void)strerror_s<sizeof errMsg>(errMsg, savedErrno);
50+
return errMsg;
51+
#endif
4652
}
4753

4854
inline LogStream &operator<<(LogStream &s, T v)
@@ -76,13 +82,23 @@ void Logger::formatTime()
7682
if (now != lastSecond_)
7783
{
7884
lastSecond_ = now;
85+
#ifndef _MSC_VER
7986
strncpy(lastTimeString_,
8087
date_.toFormattedString(false).c_str(),
8188
sizeof(lastTimeString_) - 1);
89+
#else
90+
strncpy_s<sizeof lastTimeString_>(
91+
lastTimeString_,
92+
date_.toFormattedString(false).c_str(),
93+
sizeof(lastTimeString_) - 1);
94+
#endif
8295
}
8396
logStream_ << T(lastTimeString_, 17);
8497
char tmp[32];
85-
sprintf(tmp, ".%06llu UTC ", static_cast<long long unsigned int>(microSec));
98+
snprintf(tmp,
99+
sizeof(tmp),
100+
".%06llu UTC ",
101+
static_cast<long long unsigned int>(microSec));
86102
logStream_ << T(tmp, 12);
87103
#ifdef __linux__
88104
if (threadId_ == 0)
@@ -140,7 +156,7 @@ Logger::Logger(SourceFile file, int line, bool)
140156
logStream_ << T(logLevelStr[level_], 7);
141157
if (errno != 0)
142158
{
143-
logStream_ << strerror(errno) << " (errno=" << errno << ") ";
159+
logStream_ << strerror_tl(errno) << " (errno=" << errno << ") ";
144160
}
145161
}
146162
Logger::~Logger()

0 commit comments

Comments
 (0)