Commit efd0e727 authored by Wez Furlong's avatar Wez Furlong Committed by Facebook GitHub Bot

folly: to_narrow for windows compatibility

Summary:
The default warning settings for the MSVC compiler result in an
extremely noisy build output, to the point where it can take minutes of reading
through pages of output to locate the actual error when debugging a build
problem.  The majority of these warnings are the result of implicit integer
narrowing that is silently permitted in our posix builds.

Rather than blanket suppress these conversion warnings and mask potential bugs,
this commit makes a pass over the code and adds `folly::to_narrow` to a number
of call sites to make it explicit that a narrowing operation is taking place
and that it is (probably!) intentional.   We make a lot of assumptions
throughout our various projects that we won't allow data larger than 2GB
through eg: thrift and as a result, a lot of wire/serialization related code
uses `uint32_t` for sizes instead of `size_t`.

Reviewed By: yfeldblum

Differential Revision: D20562029

fbshipit-source-id: 706ba9cf330d40a2b3f911e5e9a8b9de325ebc80
parent 940c095e
......@@ -330,6 +330,8 @@ class to_narrow_convertible {
int> = 0>
/* implicit */ constexpr operator Dst() const noexcept {
FOLLY_PUSH_WARNING
FOLLY_MSVC_DISABLE_WARNING(4244) // lossy conversion: arguments
FOLLY_MSVC_DISABLE_WARNING(4267) // lossy conversion: variables
FOLLY_GNU_DISABLE_WARNING("-Wconversion")
return value_;
FOLLY_POP_WARNING
......
......@@ -31,6 +31,7 @@
#include <folly/Conv.h>
#include <folly/Expected.h>
#include <folly/Utility.h>
#include <folly/portability/SysTime.h>
#include <folly/portability/SysTypes.h>
......@@ -134,10 +135,10 @@ static Expected<std::pair<time_t, long>, ConversionCode> durationToPosixTime(
if (sec.hasError()) {
return makeUnexpected(sec.error());
}
auto secTimeT = sec.value();
auto secTimeT = static_cast<time_t>(sec.value());
auto remainder = duration.count() - (secTimeT * Denominator);
auto subsec = (remainder * SubsecondRatio::den) / Denominator;
long subsec = (remainder * SubsecondRatio::den) / Denominator;
if (UNLIKELY(duration.count() < 0) && remainder != 0) {
if (secTimeT == std::numeric_limits<time_t>::lowest()) {
return makeUnexpected(ConversionCode::NEGATIVE_OVERFLOW);
......@@ -569,7 +570,7 @@ Expected<Tgt, ConversionCode> tryPosixTimeToDuration(
return makeUnexpected(ConversionCode::NEGATIVE_OVERFLOW);
}
seconds = seconds - 1 + overflowSeconds;
subseconds = remainder + SubsecondRatio::den;
subseconds = to_narrow(remainder + SubsecondRatio::den);
} else if (UNLIKELY(subseconds >= SubsecondRatio::den)) {
const auto overflowSeconds = (subseconds / SubsecondRatio::den);
const auto remainder = (subseconds % SubsecondRatio::den);
......@@ -577,7 +578,7 @@ Expected<Tgt, ConversionCode> tryPosixTimeToDuration(
return makeUnexpected(ConversionCode::POSITIVE_OVERFLOW);
}
seconds += overflowSeconds;
subseconds = remainder;
subseconds = to_narrow(remainder);
}
return posixTimeToDuration<SubsecondRatio>(seconds, subseconds, Tgt{});
......
......@@ -17,6 +17,7 @@
#include <folly/io/async/AsyncPipe.h>
#include <folly/FileUtil.h>
#include <folly/Utility.h>
#include <folly/detail/FileUtilDetail.h>
#include <folly/io/async/AsyncSocketException.h>
......@@ -60,7 +61,7 @@ static int recv_internal(NetworkSocket s, void* buf, size_t count) {
if (r == -1 && WSAGetLastError() == WSAEWOULDBLOCK) {
errno = EAGAIN;
}
return r;
return folly::to_narrow(r);
}
#endif
......@@ -242,7 +243,7 @@ static int send_internal(NetworkSocket s, const void* buf, size_t count) {
if (r == -1 && WSAGetLastError() == WSAEWOULDBLOCK) {
errno = EAGAIN;
}
return r;
return folly::to_narrow(r);
}
#endif
......
......@@ -17,6 +17,7 @@
#include <folly/io/async/AsyncUDPSocket.h>
#include <folly/Likely.h>
#include <folly/Utility.h>
#include <folly/io/SocketOptionMap.h>
#include <folly/io/async/EventBase.h>
#include <folly/portability/Fcntl.h>
......@@ -482,7 +483,7 @@ int AsyncUDPSocket::writeImpl(
FOLLY_POP_WARNING
fillMsgVec(
&addrStorage,
address.getActualSize(),
folly::to_narrow(address.getActualSize()),
bufs,
count,
msgvec,
......@@ -495,7 +496,7 @@ int AsyncUDPSocket::writeImpl(
std::unique_ptr<iovec[]> iov(new iovec[iov_count]);
fillMsgVec(
&addrStorage,
address.getActualSize(),
folly::to_narrow(address.getActualSize()),
bufs,
count,
msgvec,
......
......@@ -16,6 +16,7 @@
#include <folly/logging/BridgeFromGoogleLogging.h>
#include <folly/Utility.h>
#include <folly/logging/Logger.h>
#include <folly/logging/xlog.h>
......@@ -93,7 +94,7 @@ void BridgeFromGoogleLogging::send(
pTime,
message,
message_len,
usecs.count());
folly::to_narrow(usecs.count()));
}
} // namespace logging
......
......@@ -96,7 +96,7 @@ constexpr std::array<FormatKeys, 11> formatKeys{{
FormatKeys(/* key */ "USECS", /* argIndex */ 6, /* width */ 6),
FormatKeys(/* key */ "m", /* argIndex */ 1, /* width */ 2),
}};
constexpr int messageIndex = formatKeys.size();
constexpr size_t messageIndex = formatKeys.size();
} // namespace
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment