applypatch: Fix comparison of integers of different signs.

bootable/recovery/applypatch/imgpatch.cpp:57:3: error: comparison of integers of different signs: 'unsigned int' and 'int' [-Werror,-Wsign-compare]
  CHECK_GT(expected_target_length, 0);
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

bootable/recovery/applypatch/freecache.cpp:145:50: error: comparison of integers of different signs: 'long' and '__fsblkcnt64_t' (aka 'unsigned long') [-Werror,-Wsign-compare]
  if (sf.f_bsize == 0 || free_space / sf.f_bsize != sf.f_bavail) {
                         ~~~~~~~~~~~~~~~~~~~~~~~ ^  ~~~~~~~~~~~
bootable/recovery/applypatch/freecache.cpp:190:16: error: comparison of integers of different signs: 'int64_t' (aka 'long') and 'size_t' (aka 'unsigned long') [-Werror,-Wsign-compare]
  if (free_now >= bytes_needed) {
      ~~~~~~~~ ^  ~~~~~~~~~~~~
bootable/recovery/applypatch/freecache.cpp:233:18: error: comparison of integers of different signs: 'int64_t' (aka 'long') and 'size_t' (aka 'unsigned long') [-Werror,-Wsign-compare]
    if (free_now >= bytes_needed) {
        ~~~~~~~~ ^  ~~~~~~~~~~~~

Test: `mmma -j bootable/recovery/applypatch` with -Wsign-compare
Test: Run recovery_unit_test on marlin.
Change-Id: I4aa1fd0f9b7205b9e4e50874fc4bccb62951e7fe
diff --git a/applypatch/freecache.cpp b/applypatch/freecache.cpp
index e487865..3868ef2 100644
--- a/applypatch/freecache.cpp
+++ b/applypatch/freecache.cpp
@@ -141,8 +141,9 @@
     return -1;
   }
 
-  int64_t free_space = static_cast<int64_t>(sf.f_bsize) * sf.f_bavail;
-  if (sf.f_bsize == 0 || free_space / sf.f_bsize != sf.f_bavail) {
+  auto f_bsize = static_cast<int64_t>(sf.f_bsize);
+  auto free_space = sf.f_bsize * sf.f_bavail;
+  if (f_bsize == 0 || free_space / f_bsize != static_cast<int64_t>(sf.f_bavail)) {
     LOG(ERROR) << "Invalid block size or overflow (sf.f_bsize " << sf.f_bsize << ", sf.f_bavail "
                << sf.f_bavail << ")";
     return -1;
@@ -170,6 +171,13 @@
 
 bool RemoveFilesInDirectory(size_t bytes_needed, const std::string& dirname,
                             const std::function<int64_t(const std::string&)>& space_checker) {
+  // The requested size cannot exceed max int64_t.
+  if (static_cast<uint64_t>(bytes_needed) >
+      static_cast<uint64_t>(std::numeric_limits<int64_t>::max())) {
+    LOG(ERROR) << "Invalid arg of bytes_needed: " << bytes_needed;
+    return false;
+  }
+
   struct stat st;
   if (stat(dirname.c_str(), &st) == -1) {
     PLOG(ERROR) << "Failed to stat " << dirname;
@@ -187,7 +195,7 @@
   }
   LOG(INFO) << free_now << " bytes free on " << dirname << " (" << bytes_needed << " needed)";
 
-  if (free_now >= bytes_needed) {
+  if (free_now >= static_cast<int64_t>(bytes_needed)) {
     return true;
   }
 
@@ -230,7 +238,7 @@
       return false;
     }
     LOG(INFO) << "Deleted " << file << "; now " << free_now << " bytes free";
-    if (free_now >= bytes_needed) {
+    if (free_now >= static_cast<int64_t>(bytes_needed)) {
       return true;
     }
   }
diff --git a/applypatch/imgpatch.cpp b/applypatch/imgpatch.cpp
index e6be39a..f4c33e5 100644
--- a/applypatch/imgpatch.cpp
+++ b/applypatch/imgpatch.cpp
@@ -54,7 +54,7 @@
                                             const Value& patch, size_t patch_offset,
                                             const char* deflate_header, SinkFn sink) {
   size_t expected_target_length = static_cast<size_t>(Read8(deflate_header + 32));
-  CHECK_GT(expected_target_length, 0);
+  CHECK_GT(expected_target_length, static_cast<size_t>(0));
   int level = Read4(deflate_header + 40);
   int method = Read4(deflate_header + 44);
   int window_bits = Read4(deflate_header + 48);