rfc : -Wshorten-64-to-32 warnings in folly liger dependencies
authorRanjeeth Dasineni <ranjeeth@fb.com>
Wed, 20 Aug 2014 19:38:57 +0000 (12:38 -0700)
committerAnton Likhtarov <alikhtarov@fb.com>
Fri, 26 Sep 2014 22:20:40 +0000 (15:20 -0700)
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

folly/Conv.h
folly/Format-inl.h
folly/Hash.h
folly/String-inl.h

index 798c27323c2e2b87f2528ff677e570149eda3989..4ed4139423ccd3130d229cae8f21f8ef255a8a2e 100644 (file)
@@ -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<unsigned __int128>() + 1];
-  unsigned int p;
+  size_t p;
 
   if (value < 0) {
     p = detail::unsafeTelescope128(buffer, sizeof(buffer), Usrc(-value));
@@ -364,7 +364,7 @@ template <class Tgt>
 void
 toAppend(unsigned __int128 value, Tgt * result) {
   char buffer[detail::digitsEnough<unsigned __int128>()];
-  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<int>(src->size()),
+                                    &length); // processed char count
 
   if (!std::isnan(result)) {
     src->advance(length);
index f9de120081597f9a0a53327dea8108f045755a72..f05a5c723331edf0649d878900d54ebb1c212af5 100644 (file)
@@ -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<int> (arg.width - val.size());
     memset(padBuf, fill, std::min(padBufSize, padChars));
 
     switch (arg.align) {
@@ -634,7 +634,7 @@ class FormatValue<double> {
          DoubleToStringConverter::kMaxFixedDigitsAfterPoint),
         (8 + DoubleToStringConverter::kMaxExponentialDigits),
         (7 + DoubleToStringConverter::kMaxPrecisionDigits)})];
-    StringBuilder builder(buf + 1, sizeof(buf) - 1);
+    StringBuilder builder(buf + 1, static_cast<int> (sizeof(buf) - 1));
 
     char plusSign;
     switch (arg.sign) {
index 0cc326a42bb64da68cf6a97b1427a22b081104b0..1a16f3c8e1a11d14ed7d6bf6298495c86b168895 100644 (file)
@@ -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<const char*>(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<const char*>(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<const char*>(buf);
-  uint32_t hash = len;
+  uint32_t hash = static_cast<uint32_t>(len);
   uint32_t tmp;
-  int rem;
+  size_t rem;
 
   if (len <= 0 || buf == 0) {
     return 0;
index 28aead22360baa8e9b64ccf5218960f1b0f17f8c..27b02a614f96f4fffe949cd6ccdfc6bc19a279c0 100644 (file)
@@ -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];