From: Ranjeeth Dasineni Date: Wed, 20 Aug 2014 19:38:57 +0000 (-0700) Subject: rfc : -Wshorten-64-to-32 warnings in folly liger dependencies X-Git-Tag: v0.22.0~340 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=6fc7a48c7e3a5cdb0c97196b2ff0f08c6ee073d7;p=folly.git rfc : -Wshorten-64-to-32 warnings in folly liger dependencies Summary: This is not a fix but me seeking advice here. The context here is that in liger (our common network stack for mobile platforms), we saw bugs due to truncation and want to enable these warnings. To find those in our code, I had to first resolve the folly ones. I just got around by making the truncation explicit so that I can get to real errors. Ideally I would like owners to examine each case and fix the original/accept the explicit truncate if its intended. Let me know what you think. The last (least preferred) is for us to keep this as a liger only change. We have a couple of ways to do that but it would be nice to fix. N.B : this covers only liger dependencies Test Plan: errors resolved after these changes. Reviewed By: njormrod@fb.com Subscribers: trunkagent, doug, shilin, njormrod, seanc, pgriess FB internal diff: D1509355 --- diff --git a/folly/Conv.h b/folly/Conv.h index 798c2732..4ed41394 100644 --- a/folly/Conv.h +++ b/folly/Conv.h @@ -153,10 +153,10 @@ digitsEnough() { return ceil((double(sizeof(IntegerType) * CHAR_BIT) * M_LN2) / M_LN10); } -inline unsigned int -unsafeTelescope128(char * buffer, unsigned int room, unsigned __int128 x) { +inline size_t +unsafeTelescope128(char * buffer, size_t room, unsigned __int128 x) { typedef unsigned __int128 Usrc; - unsigned int p = room - 1; + size_t p = room - 1; while (x >= (Usrc(1) << 64)) { // Using 128-bit division while needed const auto y = x / 10; @@ -348,7 +348,7 @@ void toAppend(__int128 value, Tgt * result) { typedef unsigned __int128 Usrc; char buffer[detail::digitsEnough() + 1]; - unsigned int p; + size_t p; if (value < 0) { p = detail::unsafeTelescope128(buffer, sizeof(buffer), Usrc(-value)); @@ -364,7 +364,7 @@ template void toAppend(unsigned __int128 value, Tgt * result) { char buffer[detail::digitsEnough()]; - unsigned int p; + size_t p; p = detail::unsafeTelescope128(buffer, sizeof(buffer), value); @@ -1230,8 +1230,9 @@ to(StringPiece *const src) { FOLLY_RANGE_CHECK(!src->empty(), "No digits found in input string"); int length; - auto result = conv.StringToDouble(src->data(), src->size(), - &length); // processed char count + auto result = conv.StringToDouble(src->data(), + static_cast(src->size()), + &length); // processed char count if (!std::isnan(result)) { src->advance(length); diff --git a/folly/Format-inl.h b/folly/Format-inl.h index f9de1200..f05a5c72 100644 --- a/folly/Format-inl.h +++ b/folly/Format-inl.h @@ -326,7 +326,7 @@ void formatString(StringPiece val, FormatArg& arg, FormatCallback& cb) { int padRemaining = 0; if (arg.width != FormatArg::kDefaultWidth && val.size() < arg.width) { char fill = arg.fill == FormatArg::kDefaultFill ? ' ' : arg.fill; - int padChars = arg.width - val.size(); + int padChars = static_cast (arg.width - val.size()); memset(padBuf, fill, std::min(padBufSize, padChars)); switch (arg.align) { @@ -634,7 +634,7 @@ class FormatValue { DoubleToStringConverter::kMaxFixedDigitsAfterPoint), (8 + DoubleToStringConverter::kMaxExponentialDigits), (7 + DoubleToStringConverter::kMaxPrecisionDigits)})]; - StringBuilder builder(buf + 1, sizeof(buf) - 1); + StringBuilder builder(buf + 1, static_cast (sizeof(buf) - 1)); char plusSign; switch (arg.sign) { diff --git a/folly/Hash.h b/folly/Hash.h index 0cc326a4..1a16f3c8 100644 --- a/folly/Hash.h +++ b/folly/Hash.h @@ -217,11 +217,11 @@ inline uint32_t fnv32(const char* s, } inline uint32_t fnv32_buf(const void* buf, - int n, + size_t n, uint32_t hash = FNV_32_HASH_START) { const char* char_buf = reinterpret_cast(buf); - for (int i = 0; i < n; ++i) { + for (size_t i = 0; i < n; ++i) { hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24); hash ^= char_buf[i]; @@ -246,11 +246,11 @@ inline uint64_t fnv64(const char* s, } inline uint64_t fnv64_buf(const void* buf, - int n, + size_t n, uint64_t hash = FNV_64_HASH_START) { const char* char_buf = reinterpret_cast(buf); - for (int i = 0; i < n; ++i) { + for (size_t i = 0; i < n; ++i) { hash += (hash << 1) + (hash << 4) + (hash << 5) + (hash << 7) + (hash << 8) + (hash << 40); hash ^= char_buf[i]; @@ -269,11 +269,11 @@ inline uint64_t fnv64(const std::string& str, #define get16bits(d) (*((const uint16_t*) (d))) -inline uint32_t hsieh_hash32_buf(const void* buf, int len) { +inline uint32_t hsieh_hash32_buf(const void* buf, size_t len) { const char* s = reinterpret_cast(buf); - uint32_t hash = len; + uint32_t hash = static_cast(len); uint32_t tmp; - int rem; + size_t rem; if (len <= 0 || buf == 0) { return 0; diff --git a/folly/String-inl.h b/folly/String-inl.h index 28aead22..27b02a61 100644 --- a/folly/String-inl.h +++ b/folly/String-inl.h @@ -320,8 +320,8 @@ void internalSplit(DelimT delim, StringPiece sp, OutputIterator out, ignoreEmpty); } - int tokenStartPos = 0; - int tokenSize = 0; + size_t tokenStartPos = 0; + size_t tokenSize = 0; for (size_t i = 0; i <= strSize - dSize; ++i) { if (atDelim(&s[i], delim)) { if (!ignoreEmpty || tokenSize > 0) { @@ -615,7 +615,7 @@ bool hexlify(const InputString& input, OutputString& output, if (!append_output) output.clear(); static char hexValues[] = "0123456789abcdef"; - int j = output.size(); + auto j = output.size(); output.resize(2 * input.size() + output.size()); for (size_t i = 0; i < input.size(); ++i) { int ch = input[i];