From 3e0ea1021a934b16df1f4848815b45ec671f3541 Mon Sep 17 00:00:00 2001
From: Christopher Dykes <cdykes@fb.com>
Date: Mon, 22 May 2017 16:54:41 -0700
Subject: [PATCH] Make a few implicit truncations either explicit, or not
 truncate

Summary: This silences a few warnings under MSVC, and gets Folly closer to being able to compile with `-Wconversion` enabled.

Reviewed By: yfeldblum

Differential Revision: D5100686

fbshipit-source-id: e5e04c7aa143597e151a56a4d1f90dda26d0033b
---
 folly/IPAddressV6.cpp                          |  4 ++--
 folly/experimental/EliasFanoCoding.h           |  2 +-
 folly/experimental/JemallocNodumpAllocator.cpp |  2 +-
 folly/io/async/AsyncSSLSocket.cpp              | 10 +++++-----
 folly/io/async/ssl/OpenSSLUtils.cpp            |  2 +-
 folly/io/test/IOBufCursorTest.cpp              |  2 +-
 folly/io/test/IOBufQueueTest.cpp               |  2 +-
 folly/test/RandomTest.cpp                      |  2 +-
 folly/test/ThreadCachedArenaTest.cpp           | 10 ++++------
 9 files changed, 17 insertions(+), 19 deletions(-)

diff --git a/folly/IPAddressV6.cpp b/folly/IPAddressV6.cpp
index 57d00aa4..6f1eb595 100644
--- a/folly/IPAddressV6.cpp
+++ b/folly/IPAddressV6.cpp
@@ -188,8 +188,8 @@ IPAddressV6 IPAddressV6::fromInverseArpaName(const std::string& arpaname) {
   std::array<char, IPAddressV6::kToFullyQualifiedSize> ip;
   size_t pos = 0;
   int count = 0;
-  for (int p = pieces.size() - 1; p >= 0; p--) {
-    ip[pos] = pieces[p][0];
+  for (size_t i = 1; i <= pieces.size(); i++) {
+    ip[pos] = pieces[pieces.size() - i][0];
     pos++;
     count++;
     // add ':' every 4 chars
diff --git a/folly/experimental/EliasFanoCoding.h b/folly/experimental/EliasFanoCoding.h
index f7bef3a4..aa4d4029 100644
--- a/folly/experimental/EliasFanoCoding.h
+++ b/folly/experimental/EliasFanoCoding.h
@@ -145,7 +145,7 @@ struct EliasFanoEncoderV2 {
         forwardPointers_(reinterpret_cast<SkipValueType*>(
               result.forwardPointers)),
         result_(result) {
-    std::fill(result.data.begin(), result.data.end(), 0);
+    std::fill(result.data.begin(), result.data.end(), '\0');
   }
 
   EliasFanoEncoderV2(size_t size, ValueType upperBound)
diff --git a/folly/experimental/JemallocNodumpAllocator.cpp b/folly/experimental/JemallocNodumpAllocator.cpp
index 94db0e72..80f8c272 100644
--- a/folly/experimental/JemallocNodumpAllocator.cpp
+++ b/folly/experimental/JemallocNodumpAllocator.cpp
@@ -108,7 +108,7 @@ void JemallocNodumpAllocator::deallocate(void* p) {
 
 void JemallocNodumpAllocator::deallocate(void* p, void* userData) {
   const uint64_t flags = reinterpret_cast<uint64_t>(userData);
-  dallocx != nullptr ? dallocx(p, flags) : free(p);
+  dallocx != nullptr ? dallocx(p, static_cast<int>(flags)) : free(p);
 }
 
 JemallocNodumpAllocator& globalJemallocNodumpAllocator() {
diff --git a/folly/io/async/AsyncSSLSocket.cpp b/folly/io/async/AsyncSSLSocket.cpp
index 3791db7d..234ab5ae 100644
--- a/folly/io/async/AsyncSSLSocket.cpp
+++ b/folly/io/async/AsyncSSLSocket.cpp
@@ -702,10 +702,10 @@ void AsyncSSLSocket::connect(
   assert(sslState_ == STATE_UNINIT);
   noTransparentTls_ = true;
   totalConnectTimeout_ = totalConnectTimeout;
-  AsyncSSLSocketConnector* connector =
-      new AsyncSSLSocketConnector(this, callback, totalConnectTimeout.count());
+  AsyncSSLSocketConnector* connector = new AsyncSSLSocketConnector(
+      this, callback, int(totalConnectTimeout.count()));
   AsyncSocket::connect(
-      connector, address, connectTimeout.count(), options, bindAddr);
+      connector, address, int(connectTimeout.count()), options, bindAddr);
 }
 
 bool AsyncSSLSocket::needsPeerVerification() const {
@@ -1701,9 +1701,9 @@ int AsyncSSLSocket::bioRead(BIO* b, char* out, int outl) {
     queue.append(std::move(sslSock->preReceivedData_));
     queue.trimStart(len);
     sslSock->preReceivedData_ = queue.move();
-    return len;
+    return static_cast<int>(len);
   } else {
-    auto result = recv(OpenSSLUtils::getBioFd(b, nullptr), out, outl, 0);
+    auto result = int(recv(OpenSSLUtils::getBioFd(b, nullptr), out, outl, 0));
     if (result <= 0 && OpenSSLUtils::getBioShouldRetryWrite(result)) {
       BIO_set_retry_read(b);
     }
diff --git a/folly/io/async/ssl/OpenSSLUtils.cpp b/folly/io/async/ssl/OpenSSLUtils.cpp
index 72285ac9..c7a0346e 100644
--- a/folly/io/async/ssl/OpenSSLUtils.cpp
+++ b/folly/io/async/ssl/OpenSSLUtils.cpp
@@ -120,7 +120,7 @@ bool OpenSSLUtils::validatePeerCertNames(X509* cert,
     }
   }
 
-  for (size_t i = 0; i < (size_t)sk_GENERAL_NAME_num(altNames); i++) {
+  for (int i = 0; i < sk_GENERAL_NAME_num(altNames); i++) {
     auto name = sk_GENERAL_NAME_value(altNames, i);
     if ((addr4 != nullptr || addr6 != nullptr) && name->type == GEN_IPADD) {
       // Extra const-ness for paranoia
diff --git a/folly/io/test/IOBufCursorTest.cpp b/folly/io/test/IOBufCursorTest.cpp
index 36002b2a..cfc5109d 100644
--- a/folly/io/test/IOBufCursorTest.cpp
+++ b/folly/io/test/IOBufCursorTest.cpp
@@ -538,7 +538,7 @@ TEST(IOBuf, QueueAppenderPushAtMostFillBuffer) {
   QueueAppender appender{&queue, 125};
   std::vector<uint8_t> data;
   data.resize(1000);
-  std::iota(data.begin(), data.end(), 0);
+  std::iota(data.begin(), data.end(), uint8_t(0));
   // Add 100 byte
   appender.pushAtMost(data.data(), 100);
   // Add 900 bytes
diff --git a/folly/io/test/IOBufQueueTest.cpp b/folly/io/test/IOBufQueueTest.cpp
index 3b000ea8..a22715da 100644
--- a/folly/io/test/IOBufQueueTest.cpp
+++ b/folly/io/test/IOBufQueueTest.cpp
@@ -177,7 +177,7 @@ TEST(IOBufQueue, SplitZero) {
 TEST(IOBufQueue, Preallocate) {
   IOBufQueue queue(clOptions);
   queue.append(string("Hello"));
-  pair<void*,uint32_t> writable = queue.preallocate(2, 64, 64);
+  pair<void*, uint64_t> writable = queue.preallocate(2, 64, 64);
   checkConsistency(queue);
   EXPECT_NE((void*)nullptr, writable.first);
   EXPECT_LE(2, writable.second);
diff --git a/folly/test/RandomTest.cpp b/folly/test/RandomTest.cpp
index a51baffe..25a2070b 100644
--- a/folly/test/RandomTest.cpp
+++ b/folly/test/RandomTest.cpp
@@ -135,6 +135,6 @@ TEST(Random, sanity) {
     }
     EXPECT_EQ(
         vals.size(),
-        std::unordered_set<uint32_t>(vals.begin(), vals.end()).size());
+        std::unordered_set<uint64_t>(vals.begin(), vals.end()).size());
   }
 }
diff --git a/folly/test/ThreadCachedArenaTest.cpp b/folly/test/ThreadCachedArenaTest.cpp
index ea978dc1..e835ae99 100644
--- a/folly/test/ThreadCachedArenaTest.cpp
+++ b/folly/test/ThreadCachedArenaTest.cpp
@@ -58,16 +58,14 @@ void ArenaTester::allocate(size_t count, size_t maxSize) {
   for (size_t i = 0; i < count; i++) {
     size_t size = sizeDist(rnd);
     uint8_t* p = static_cast<uint8_t*>(arena_->allocate(size));
-    areas_.emplace_back(rnd() & 0xff, Range<uint8_t*>(p, size));
+    areas_.emplace_back(uint8_t(rnd() & 0xff), Range<uint8_t*>(p, size));
   }
 
   // Fill each area with a different value, to prove that they don't overlap
   // Fill in random order.
-  std::random_shuffle(
-      areas_.begin(), areas_.end(),
-      [&rnd] (int n) -> int {
-        return std::uniform_int_distribution<uint32_t>(0, n-1)(rnd);
-      });
+  std::random_shuffle(areas_.begin(), areas_.end(), [&rnd](ptrdiff_t n) {
+    return std::uniform_int_distribution<uint32_t>(0, n - 1)(rnd);
+  });
 
   for (auto& p : areas_) {
     std::fill(p.second.begin(), p.second.end(), p.first);
-- 
2.34.1