Commit 4dd1dd68 authored by Christopher Dykes's avatar Christopher Dykes Committed by Facebook Github Bot

Start fixing implicit truncations

Summary:
Truncations should be explicit, but for some reason, MSVC seems to be the only compiler that will warn you when you implicitly truncate integer or float values.
This allows Folly to be compiled with warnings 4018, 4242, 4244 and 4305 enabled.
Technically 4018 is a sign mismatch warning, but there was only one place it was being triggered so I included it anyways. The other 3 are warnings for implicit truncation.

There is one other implicit truncation warning that currently triggers in Folly, 4267, but there are a lot more places where that triggers so I'll do that in a separate diff.

Reviewed By: yfeldblum

Differential Revision: D4249471

fbshipit-source-id: e18a93d85856c998576934a6229c9edd1638a54e
parent ea209427
...@@ -549,7 +549,7 @@ inline Expected<Tgt, ConversionCode> digits_to( ...@@ -549,7 +549,7 @@ inline Expected<Tgt, ConversionCode> digits_to(
UT result = 0; UT result = 0;
for (; e - b >= 4; b += 4) { for (; e - b >= 4; b += 4) {
result *= 10000; result *= static_cast<UT>(10000);
const int32_t r0 = shift1000[static_cast<size_t>(b[0])]; const int32_t r0 = shift1000[static_cast<size_t>(b[0])];
const int32_t r1 = shift100[static_cast<size_t>(b[1])]; const int32_t r1 = shift100[static_cast<size_t>(b[1])];
const int32_t r2 = shift10[static_cast<size_t>(b[2])]; const int32_t r2 = shift10[static_cast<size_t>(b[2])];
...@@ -558,7 +558,7 @@ inline Expected<Tgt, ConversionCode> digits_to( ...@@ -558,7 +558,7 @@ inline Expected<Tgt, ConversionCode> digits_to(
if (sum >= OOR) { if (sum >= OOR) {
goto outOfRange; goto outOfRange;
} }
result += sum; result += UT(sum);
} }
switch (e - b) { switch (e - b) {
......
...@@ -419,7 +419,7 @@ IPAddress::longestCommonPrefix(const CIDRNetwork& one, const CIDRNetwork& two) { ...@@ -419,7 +419,7 @@ IPAddress::longestCommonPrefix(const CIDRNetwork& one, const CIDRNetwork& two) {
} else { } else {
throw std::invalid_argument("Unknown address family"); throw std::invalid_argument("Unknown address family");
} }
return {IPAddress(0), 0}; return {IPAddress(0), uint8_t(0)};
} }
[[noreturn]] void IPAddress::asV4Throw() const { [[noreturn]] void IPAddress::asV4Throw() const {
......
...@@ -548,7 +548,7 @@ bool SocketAddress::prefixMatch(const SocketAddress& other, ...@@ -548,7 +548,7 @@ bool SocketAddress::prefixMatch(const SocketAddress& other,
if (other.getFamily() != getFamily()) { if (other.getFamily() != getFamily()) {
return false; return false;
} }
int mask_length = 128; uint8_t mask_length = 128;
switch (getFamily()) { switch (getFamily()) {
case AF_INET: case AF_INET:
mask_length = 32; mask_length = 32;
......
...@@ -31,7 +31,7 @@ fbstring submatch(const boost::cmatch& m, size_t idx) { ...@@ -31,7 +31,7 @@ fbstring submatch(const boost::cmatch& m, size_t idx) {
template <class String> template <class String>
void toLower(String& s) { void toLower(String& s) {
for (auto& c : s) { for (auto& c : s) {
c = tolower(c); c = char(tolower(c));
} }
} }
......
...@@ -28,7 +28,7 @@ inline int popcount(unsigned int x) { ...@@ -28,7 +28,7 @@ inline int popcount(unsigned int x) {
return __popcnt(x); return __popcnt(x);
} }
inline int popcountll(unsigned long long x) { inline int popcountll(unsigned long long x) {
return __popcnt64(x); return int(__popcnt64(x));
} }
#elif defined(__POPCNT__) #elif defined(__POPCNT__)
......
...@@ -38,7 +38,7 @@ ssize_t wrapNoInt(F f, Args... args) { ...@@ -38,7 +38,7 @@ ssize_t wrapNoInt(F f, Args... args) {
} }
inline void incr(ssize_t /* n */) {} inline void incr(ssize_t /* n */) {}
inline void incr(ssize_t n, off_t& offset) { offset += n; } inline void incr(ssize_t n, off_t& offset) { offset += off_t(n); }
// Wrap call to read/pread/write/pwrite(fd, buf, count, offset?) to retry on // Wrap call to read/pread/write/pwrite(fd, buf, count, offset?) to retry on
// incomplete reads / writes. The variadic argument magic is there to support // incomplete reads / writes. The variadic argument magic is there to support
......
...@@ -198,7 +198,7 @@ std::unique_ptr<folly::IOBuf> toBserIOBuf(folly::dynamic const& dyn, ...@@ -198,7 +198,7 @@ std::unique_ptr<folly::IOBuf> toBserIOBuf(folly::dynamic const& dyn,
// compute the length // compute the length
auto len = q.chainLength(); auto len = q.chainLength();
if (len > std::numeric_limits<int64_t>::max()) { if (len > uint64_t(std::numeric_limits<int64_t>::max())) {
throw std::range_error(folly::to<std::string>( throw std::range_error(folly::to<std::string>(
"serialized data size ", len, " is too large to represent as BSER")); "serialized data size ", len, " is too large to represent as BSER"));
} }
......
...@@ -96,7 +96,7 @@ size_t parsePageSizeValue(StringPiece value) { ...@@ -96,7 +96,7 @@ size_t parsePageSizeValue(StringPiece value) {
} }
char c = '\0'; char c = '\0';
if (match.length(2) != 0) { if (match.length(2) != 0) {
c = tolower(value[match.position(2)]); c = char(tolower(value[match.position(2)]));
} }
StringPiece numStr(value.data() + match.position(1), match.length(1)); StringPiece numStr(value.data() + match.position(1), match.length(1));
size_t size = to<size_t>(numStr); size_t size = to<size_t>(numStr);
......
...@@ -126,7 +126,8 @@ class AsyncSocket::BytesWriteRequest : public AsyncSocket::WriteRequest { ...@@ -126,7 +126,8 @@ class AsyncSocket::BytesWriteRequest : public AsyncSocket::WriteRequest {
currentOp->iov_len -= partialBytes_; currentOp->iov_len -= partialBytes_;
// Increment the totalBytesWritten_ count by bytesWritten_; // Increment the totalBytesWritten_ count by bytesWritten_;
totalBytesWritten_ += bytesWritten_; assert(bytesWritten_ >= 0);
totalBytesWritten_ += uint32_t(bytesWritten_);
} }
private: private:
......
...@@ -552,7 +552,7 @@ dynamic parseNumber(Input& in) { ...@@ -552,7 +552,7 @@ dynamic parseNumber(Input& in) {
} }
std::string decodeUnicodeEscape(Input& in) { std::string decodeUnicodeEscape(Input& in) {
auto hexVal = [&] (char c) -> unsigned { auto hexVal = [&] (int c) -> uint16_t {
return c >= '0' && c <= '9' ? c - '0' : return c >= '0' && c <= '9' ? c - '0' :
c >= 'a' && c <= 'f' ? c - 'a' + 10 : c >= 'a' && c <= 'f' ? c - 'a' + 10 :
c >= 'A' && c <= 'F' ? c - 'A' + 10 : c >= 'A' && c <= 'F' ? c - 'A' + 10 :
...@@ -645,7 +645,7 @@ std::string parseString(Input& in) { ...@@ -645,7 +645,7 @@ std::string parseString(Input& in) {
in.error("null byte in string"); in.error("null byte in string");
} }
ret.push_back(*in); ret.push_back(char(*in));
++in; ++in;
} }
......
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