From 4dd1dd685b0397a36b21a057f588191ac0cfe6c6 Mon Sep 17 00:00:00 2001 From: Christopher Dykes Date: Wed, 30 Nov 2016 11:17:39 -0800 Subject: [PATCH] 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 --- folly/Conv.cpp | 4 ++-- folly/IPAddress.cpp | 2 +- folly/SocketAddress.cpp | 2 +- folly/Uri.cpp | 2 +- folly/detail/BitsDetail.h | 2 +- folly/detail/FileUtilDetail.h | 2 +- folly/experimental/bser/Dump.cpp | 2 +- folly/experimental/io/HugePages.cpp | 2 +- folly/io/async/AsyncSocket.cpp | 3 ++- folly/json.cpp | 4 ++-- 10 files changed, 13 insertions(+), 12 deletions(-) diff --git a/folly/Conv.cpp b/folly/Conv.cpp index 1e6da78a..3e5214a8 100644 --- a/folly/Conv.cpp +++ b/folly/Conv.cpp @@ -549,7 +549,7 @@ inline Expected digits_to( UT result = 0; for (; e - b >= 4; b += 4) { - result *= 10000; + result *= static_cast(10000); const int32_t r0 = shift1000[static_cast(b[0])]; const int32_t r1 = shift100[static_cast(b[1])]; const int32_t r2 = shift10[static_cast(b[2])]; @@ -558,7 +558,7 @@ inline Expected digits_to( if (sum >= OOR) { goto outOfRange; } - result += sum; + result += UT(sum); } switch (e - b) { diff --git a/folly/IPAddress.cpp b/folly/IPAddress.cpp index c4a51007..e1ac5b89 100644 --- a/folly/IPAddress.cpp +++ b/folly/IPAddress.cpp @@ -419,7 +419,7 @@ IPAddress::longestCommonPrefix(const CIDRNetwork& one, const CIDRNetwork& two) { } else { throw std::invalid_argument("Unknown address family"); } - return {IPAddress(0), 0}; + return {IPAddress(0), uint8_t(0)}; } [[noreturn]] void IPAddress::asV4Throw() const { diff --git a/folly/SocketAddress.cpp b/folly/SocketAddress.cpp index 1107e0e0..9e0eb0e4 100644 --- a/folly/SocketAddress.cpp +++ b/folly/SocketAddress.cpp @@ -548,7 +548,7 @@ bool SocketAddress::prefixMatch(const SocketAddress& other, if (other.getFamily() != getFamily()) { return false; } - int mask_length = 128; + uint8_t mask_length = 128; switch (getFamily()) { case AF_INET: mask_length = 32; diff --git a/folly/Uri.cpp b/folly/Uri.cpp index bc94a31f..f278619b 100644 --- a/folly/Uri.cpp +++ b/folly/Uri.cpp @@ -31,7 +31,7 @@ fbstring submatch(const boost::cmatch& m, size_t idx) { template void toLower(String& s) { for (auto& c : s) { - c = tolower(c); + c = char(tolower(c)); } } diff --git a/folly/detail/BitsDetail.h b/folly/detail/BitsDetail.h index d89a25ef..06136547 100644 --- a/folly/detail/BitsDetail.h +++ b/folly/detail/BitsDetail.h @@ -28,7 +28,7 @@ inline int popcount(unsigned int x) { return __popcnt(x); } inline int popcountll(unsigned long long x) { - return __popcnt64(x); + return int(__popcnt64(x)); } #elif defined(__POPCNT__) diff --git a/folly/detail/FileUtilDetail.h b/folly/detail/FileUtilDetail.h index bcebc5fc..5b70d65f 100644 --- a/folly/detail/FileUtilDetail.h +++ b/folly/detail/FileUtilDetail.h @@ -38,7 +38,7 @@ ssize_t wrapNoInt(F f, Args... args) { } 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 // incomplete reads / writes. The variadic argument magic is there to support diff --git a/folly/experimental/bser/Dump.cpp b/folly/experimental/bser/Dump.cpp index ae820af2..078438d9 100644 --- a/folly/experimental/bser/Dump.cpp +++ b/folly/experimental/bser/Dump.cpp @@ -198,7 +198,7 @@ std::unique_ptr toBserIOBuf(folly::dynamic const& dyn, // compute the length auto len = q.chainLength(); - if (len > std::numeric_limits::max()) { + if (len > uint64_t(std::numeric_limits::max())) { throw std::range_error(folly::to( "serialized data size ", len, " is too large to represent as BSER")); } diff --git a/folly/experimental/io/HugePages.cpp b/folly/experimental/io/HugePages.cpp index 901571e9..656c7ea9 100644 --- a/folly/experimental/io/HugePages.cpp +++ b/folly/experimental/io/HugePages.cpp @@ -96,7 +96,7 @@ size_t parsePageSizeValue(StringPiece value) { } char c = '\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)); size_t size = to(numStr); diff --git a/folly/io/async/AsyncSocket.cpp b/folly/io/async/AsyncSocket.cpp index a51e08ea..987dfa5a 100644 --- a/folly/io/async/AsyncSocket.cpp +++ b/folly/io/async/AsyncSocket.cpp @@ -126,7 +126,8 @@ class AsyncSocket::BytesWriteRequest : public AsyncSocket::WriteRequest { currentOp->iov_len -= partialBytes_; // Increment the totalBytesWritten_ count by bytesWritten_; - totalBytesWritten_ += bytesWritten_; + assert(bytesWritten_ >= 0); + totalBytesWritten_ += uint32_t(bytesWritten_); } private: diff --git a/folly/json.cpp b/folly/json.cpp index 105b1ca7..0e6b0136 100644 --- a/folly/json.cpp +++ b/folly/json.cpp @@ -552,7 +552,7 @@ dynamic parseNumber(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' : c >= 'a' && c <= 'f' ? c - 'a' + 10 : c >= 'A' && c <= 'F' ? c - 'A' + 10 : @@ -645,7 +645,7 @@ std::string parseString(Input& in) { in.error("null byte in string"); } - ret.push_back(*in); + ret.push_back(char(*in)); ++in; } -- 2.34.1