Fix some implicit truncation and implicit sign coersion warnings in the Windows porta...
authorChristopher Dykes <cdykes@fb.com>
Fri, 16 Dec 2016 23:09:24 +0000 (15:09 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Fri, 16 Dec 2016 23:18:06 +0000 (15:18 -0800)
Summary: More progress towards being able to build Folly cleanly with MSVC's truncation and sign coersion warnings enabled.

Reviewed By: yfeldblum

Differential Revision: D4288060

fbshipit-source-id: ce42d2099163ed5a9c9a9bb736a80bb2407f7595

folly/portability/Sockets.cpp
folly/portability/SysFile.cpp
folly/portability/SysMman.cpp
folly/portability/SysUio.cpp
folly/portability/Time.cpp
folly/portability/Unistd.cpp
folly/portability/Unistd.h

index 84317bf72de19f64295ac48f8e599a910dd773b0..127f237a3ce8730dac48fb224bea50f08a1d5523 100755 (executable)
@@ -144,7 +144,7 @@ int listen(int s, int backlog) {
 int poll(struct pollfd fds[], nfds_t nfds, int timeout) {
   // TODO: Allow both file descriptors and SOCKETs in this.
   for (int i = 0; i < nfds; i++) {
-    fds[i].fd = fd_to_socket(fds[i].fd);
+    fds[i].fd = fd_to_socket((int)fds[i].fd);
   }
   return ::WSAPoll(fds, (ULONG)nfds, timeout);
 }
@@ -192,7 +192,7 @@ ssize_t recvfrom(
 
     WSABUF wBuf{};
     wBuf.buf = (CHAR*)buf;
-    wBuf.len = len;
+    wBuf.len = (ULONG)len;
     WSAMSG wMsg{};
     wMsg.dwBufferCount = 1;
     wMsg.lpBuffers = &wBuf;
index 71287e7533394e2e987caa6ead3289c47052b6be..db0e99a742b5c550366a992aff5708730d2ec059 100755 (executable)
@@ -33,7 +33,7 @@ extern "C" int flock(int fd, int operation) {
       return -1;
     }
   } else {
-    int flags = 0
+    DWORD flags = 0
         | (operation & LOCK_NB ? LOCKFILE_FAIL_IMMEDIATELY : 0)
         | (operation & LOCK_EX ? LOCKFILE_EXCLUSIVE_LOCK : 0)
         ;
index 844002688a212eaf43917a4a295e4994104e274e..a360355e51b811bada906da1eb0a558118468509 100755 (executable)
@@ -127,7 +127,7 @@ void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t off) {
     ret = MapViewOfFileEx(
         fmh,
         accessFlags,
-        (DWORD)((off >> 32) & 0xFFFFFFFF),
+        (DWORD)(0), // off_t is only 32-bit :(
         (DWORD)(off & 0xFFFFFFFF),
         0,
         addr);
index 17c2b12e960628844974b0e64484823596fd8b19..387a088e9461e078adb94e711a83287f29722a71 100755 (executable)
@@ -80,7 +80,7 @@ static ssize_t doVecOperation(int fd, const iovec* iov, int count) {
   void* curBase = iov[0].iov_base;
   size_t curLen = iov[0].iov_len;
   while (curIov < count) {
-    int res = 0;
+    ssize_t res = 0;
     if (isRead) {
       res = read(fd, curBase, (unsigned int)curLen);
       if (res == 0 && curLen != 0) {
index 11eb1be4128d7d0055120e4ae61e107018589133..61851a67672b0e823d21ca19b2e4fc8b108f490d 100755 (executable)
@@ -25,10 +25,11 @@ template <typename _Rep, typename _Period>
 static void duration_to_ts(
     std::chrono::duration<_Rep, _Period> d,
     struct timespec* ts) {
-  ts->tv_sec = std::chrono::duration_cast<std::chrono::seconds>(d).count();
-  ts->tv_nsec = std::chrono::duration_cast<std::chrono::nanoseconds>(
-                    d % std::chrono::seconds(1))
-                    .count();
+  ts->tv_sec =
+      time_t(std::chrono::duration_cast<std::chrono::seconds>(d).count());
+  ts->tv_nsec = long(std::chrono::duration_cast<std::chrono::nanoseconds>(
+                         d % std::chrono::seconds(1))
+                         .count());
 }
 
 #if !FOLLY_HAVE_CLOCK_GETTIME
@@ -215,8 +216,9 @@ extern "C" int clock_gettime(clockid_t clock_id, struct timespec* tp) {
 
   const auto unanosToTimespec = [](timespec* tp, unsigned_nanos t) -> int {
     static constexpr unsigned_nanos one_sec(std::chrono::seconds(1));
-    tp->tv_sec = std::chrono::duration_cast<std::chrono::seconds>(t).count();
-    tp->tv_nsec = (t % one_sec).count();
+    tp->tv_sec =
+        time_t(std::chrono::duration_cast<std::chrono::seconds>(t).count());
+    tp->tv_nsec = long((t % one_sec).count());
     return 0;
   };
 
index b4f259294ae7578feb6725e62a8af50a34544ffa..df382dca15fef3ac85b45dd7ccdff714483c2911 100755 (executable)
@@ -147,7 +147,7 @@ int getdtablesize() { return _getmaxstdio(); }
 
 int getgid() { return 1; }
 
-pid_t getpid() { return pid_t(GetCurrentProcessId()); }
+pid_t getpid() { return (pid_t)uint64_t(GetCurrentProcessId()); }
 
 // No major need to implement this, and getting a non-potentially
 // stale ID on windows is a bit involved.
@@ -177,18 +177,18 @@ int pwrite(int fd, const void* buf, size_t count, off_t offset) {
   return wrapPositional(_write, fd, offset, buf, (unsigned int)count);
 }
 
-int read(int fh, void* buf, unsigned int mcc) {
+ssize_t read(int fh, void* buf, size_t count) {
   if (folly::portability::sockets::is_fh_socket(fh)) {
     SOCKET s = (SOCKET)_get_osfhandle(fh);
     if (s != INVALID_SOCKET) {
-      auto r = folly::portability::sockets::recv(fh, buf, (size_t)mcc, 0);
+      auto r = folly::portability::sockets::recv(fh, buf, count, 0);
       if (r == -1 && WSAGetLastError() == WSAEWOULDBLOCK) {
         errno = EAGAIN;
       }
       return r;
     }
   }
-  auto r = _read(fh, buf, mcc);
+  auto r = _read(fh, buf, unsigned int(count));
   if (r == -1 && GetLastError() == ERROR_NO_DATA) {
     // This only happens if the file was non-blocking and
     // no data was present. We have to translate the error
@@ -214,7 +214,8 @@ ssize_t readlink(const char* path, char* buf, size_t buflen) {
     return -1;
   }
 
-  DWORD ret = GetFinalPathNameByHandleA(h, buf, buflen - 1, VOLUME_NAME_DOS);
+  DWORD ret =
+      GetFinalPathNameByHandleA(h, buf, DWORD(buflen - 1), VOLUME_NAME_DOS);
   if (ret >= buflen || ret >= MAX_PATH || !ret) {
     CloseHandle(h);
     return -1;
@@ -270,7 +271,7 @@ int usleep(unsigned int ms) {
   return 0;
 }
 
-int write(int fh, void const* buf, unsigned int count) {
+ssize_t write(int fh, void const* buf, size_t count) {
   if (folly::portability::sockets::is_fh_socket(fh)) {
     SOCKET s = (SOCKET)_get_osfhandle(fh);
     if (s != INVALID_SOCKET) {
@@ -281,7 +282,7 @@ int write(int fh, void const* buf, unsigned int count) {
       return r;
     }
   }
-  auto r = _write(fh, buf, count);
+  auto r = _write(fh, buf, unsigned int(count));
   if ((r > 0 && r != count) || (r == -1 && errno == ENOSPC)) {
     // Writing to a pipe with a full buffer doesn't generate
     // any error type, unless it caused us to write exactly 0
index 4b0097e3d72b502c711e29ef69e717bc192905a6..364fca9d256c2e46ec531f5e71fdb3ead7061618 100755 (executable)
@@ -69,7 +69,7 @@ int getuid();
 int isatty(int fh);
 int lockf(int fd, int cmd, off_t len);
 long lseek(int fh, long off, int orig);
-int read(int fh, void* buf, unsigned int mcc);
+ssize_t read(int fh, void* buf, size_t mcc);
 int rmdir(const char* path);
 int pipe(int pth[2]);
 int pread(int fd, void* buf, size_t count, off_t offset);
@@ -82,7 +82,7 @@ size_t sysconf(int tp);
 long tell(int fh);
 int truncate(const char* path, off_t len);
 int usleep(unsigned int ms);
-int write(int fh, void const* buf, unsigned int mcc);
+ssize_t write(int fh, void const* buf, size_t count);
 }
 }
 }