update_engine: Fix issues reported by clang-tidy

Fix uninitialized variables and some other minor issues reported by
clang-tidy.

BUG=chromium:982837
TEST=cros_workon_make update_engine --test

Change-Id: I305dedb058c9b0787ba2f68feff42afe6810a276
Reviewed-on: https://chromium-review.googlesource.com/c/aosp/platform/system/update_engine/+/2122683
Reviewed-by: Amin Hassani <ahassani@chromium.org>
Tested-by: Andrew Lassalle <andrewlassalle@chromium.org>
Commit-Queue: Andrew Lassalle <andrewlassalle@chromium.org>
diff --git a/common/hwid_override.cc b/common/hwid_override.cc
index 8800e94..1bb0f8f 100644
--- a/common/hwid_override.cc
+++ b/common/hwid_override.cc
@@ -16,14 +16,12 @@
 
 #include "update_engine/common/hwid_override.h"
 
-#include <map>
 #include <string>
 
 #include <base/files/file_path.h>
 #include <base/files/file_util.h>
 #include <brillo/key_value_store.h>
 
-using std::map;
 using std::string;
 
 namespace chromeos_update_engine {
diff --git a/common/subprocess.cc b/common/subprocess.cc
index 5206503..24ad2d9 100644
--- a/common/subprocess.cc
+++ b/common/subprocess.cc
@@ -36,7 +36,6 @@
 
 #include "update_engine/common/utils.h"
 
-using brillo::MessageLoop;
 using std::string;
 using std::unique_ptr;
 using std::vector;
diff --git a/common_service.cc b/common_service.cc
index b94e734..347833b 100644
--- a/common_service.cc
+++ b/common_service.cc
@@ -217,7 +217,7 @@
 }
 
 bool UpdateEngineService::SetCohortHint(ErrorPtr* error,
-                                        string in_cohort_hint) {
+                                        const string& in_cohort_hint) {
   PrefsInterface* prefs = system_state_->prefs();
 
   // It is ok to override the cohort hint with an invalid value since it is
diff --git a/common_service.h b/common_service.h
index a74c46b..6c742a5 100644
--- a/common_service.h
+++ b/common_service.h
@@ -109,7 +109,8 @@
   // Sets the current "cohort hint" value to |in_cohort_hint|. The cohort hint
   // is sent back to Omaha on every request and can be used as a hint of what
   // cohort should we be put on.
-  bool SetCohortHint(brillo::ErrorPtr* error, std::string in_cohort_hint);
+  bool SetCohortHint(brillo::ErrorPtr* error,
+                     const std::string& in_cohort_hint);
 
   // Return the current cohort hint. This value can be set with SetCohortHint()
   // and can also be updated from Omaha on every update check request.
diff --git a/dbus_service.cc b/dbus_service.cc
index cd71488..a5fec74 100644
--- a/dbus_service.cc
+++ b/dbus_service.cc
@@ -36,7 +36,6 @@
 using update_engine::Operation;
 using update_engine::StatusResult;
 using update_engine::UpdateEngineStatus;
-using update_engine::UpdateStatus;
 
 namespace {
 // Converts the internal |UpdateEngineStatus| to the protobuf |StatusResult|.
diff --git a/omaha_request_action.cc b/omaha_request_action.cc
index 8890c7c..50fe3cc 100644
--- a/omaha_request_action.cc
+++ b/omaha_request_action.cc
@@ -59,7 +59,6 @@
 using base::TimeDelta;
 using chromeos_update_manager::kRollforwardInfinity;
 using std::map;
-using std::numeric_limits;
 using std::string;
 using std::vector;
 
diff --git a/omaha_request_params.cc b/omaha_request_params.cc
index 1cfbc9c..b6c18a6 100644
--- a/omaha_request_params.cc
+++ b/omaha_request_params.cc
@@ -39,9 +39,7 @@
 
 #define CALL_MEMBER_FN(object, member) ((object).*(member))
 
-using std::map;
 using std::string;
-using std::vector;
 
 namespace chromeos_update_engine {
 
diff --git a/p2p_manager.cc b/p2p_manager.cc
index 5de91d1..00ff8ce 100644
--- a/p2p_manager.cc
+++ b/p2p_manager.cc
@@ -65,7 +65,6 @@
 using chromeos_update_manager::EvalStatus;
 using chromeos_update_manager::Policy;
 using chromeos_update_manager::UpdateManager;
-using std::map;
 using std::pair;
 using std::string;
 using std::unique_ptr;
diff --git a/payload_consumer/delta_performer.cc b/payload_consumer/delta_performer.cc
index 3263ff7..11cf006 100644
--- a/payload_consumer/delta_performer.cc
+++ b/payload_consumer/delta_performer.cc
@@ -533,7 +533,7 @@
 #define OP_DURATION_HISTOGRAM(_op_name, _start_time)                         \
   LOCAL_HISTOGRAM_CUSTOM_TIMES(                                              \
       "UpdateEngine.DownloadAction.InstallOperation::" _op_name ".Duration", \
-      base::TimeTicks::Now() - _start_time,                                  \
+      (base::TimeTicks::Now() - _start_time),                                \
       base::TimeDelta::FromMilliseconds(10),                                 \
       base::TimeDelta::FromMinutes(5),                                       \
       20);
diff --git a/payload_consumer/postinstall_runner_action.cc b/payload_consumer/postinstall_runner_action.cc
index 264161c..9ecda48 100644
--- a/payload_consumer/postinstall_runner_action.cc
+++ b/payload_consumer/postinstall_runner_action.cc
@@ -49,7 +49,6 @@
 
 namespace chromeos_update_engine {
 
-using brillo::MessageLoop;
 using std::string;
 using std::vector;
 
diff --git a/payload_generator/deflate_utils.cc b/payload_generator/deflate_utils.cc
index 8db67ce..5d7a766 100644
--- a/payload_generator/deflate_utils.cc
+++ b/payload_generator/deflate_utils.cc
@@ -46,7 +46,7 @@
 // TODO(*): Optimize this so we don't have to read all extents into memory in
 // case it is large.
 bool CopyExtentsToFile(const string& in_path,
-                       const vector<Extent> extents,
+                       const vector<Extent>& extents,
                        const string& out_path,
                        size_t block_size) {
   brillo::Blob data(utils::BlocksInExtents(extents) * block_size);
diff --git a/payload_generator/delta_diff_utils.cc b/payload_generator/delta_diff_utils.cc
index db69d74..53a3cf1 100644
--- a/payload_generator/delta_diff_utils.cc
+++ b/payload_generator/delta_diff_utils.cc
@@ -250,12 +250,13 @@
   if (old_file_iter != old_files_map.end())
     return old_file_iter->second;
 
-  // No old file match for the new file name, use a similar file with the
-  // shortest levenshtein distance.
+  // No old file matches the new file name. Use a similar file with the
+  // shortest levenshtein distance instead.
   // This works great if the file has version number in it, but even for
   // a completely new file, using a similar file can still help.
-  int min_distance = new_file_name.size();
-  const FilesystemInterface::File* old_file;
+  int min_distance =
+      LevenshteinDistance(new_file_name, old_files_map.begin()->first);
+  const FilesystemInterface::File* old_file = &old_files_map.begin()->second;
   for (const auto& pair : old_files_map) {
     int distance = LevenshteinDistance(new_file_name, pair.first);
     if (distance < min_distance) {
@@ -580,6 +581,11 @@
   InstallOperation operation;
 
   uint64_t total_blocks = utils::BlocksInExtents(new_extents);
+  if (chunk_blocks == 0) {
+    LOG(ERROR) << "Invalid number of chunk_blocks. Cannot be 0.";
+    return false;
+  }
+
   if (chunk_blocks == -1)
     chunk_blocks = total_blocks;
 
diff --git a/payload_generator/delta_diff_utils_unittest.cc b/payload_generator/delta_diff_utils_unittest.cc
index bc3dca5..8a97b1b 100644
--- a/payload_generator/delta_diff_utils_unittest.cc
+++ b/payload_generator/delta_diff_utils_unittest.cc
@@ -595,6 +595,8 @@
       "update_engine");
   EXPECT_EQ(diff_utils::GetOldFile(old_files_map, "bin/delta_generator").name,
             "delta_generator");
+  // Check file name with minimum size.
+  EXPECT_EQ(diff_utils::GetOldFile(old_files_map, "a").name, "filename");
 }
 
 }  // namespace chromeos_update_engine
diff --git a/payload_generator/extent_ranges.cc b/payload_generator/extent_ranges.cc
index 0e3f087..4600efe 100644
--- a/payload_generator/extent_ranges.cc
+++ b/payload_generator/extent_ranges.cc
@@ -27,7 +27,6 @@
 #include "update_engine/payload_consumer/payload_constants.h"
 #include "update_engine/payload_generator/extent_utils.h"
 
-using std::set;
 using std::vector;
 
 namespace chromeos_update_engine {
diff --git a/update_attempter.cc b/update_attempter.cc
index f5885c9..29d256c 100644
--- a/update_attempter.cc
+++ b/update_attempter.cc
@@ -742,7 +742,7 @@
     }
   }
   std::map<std::string, OmahaRequestParams::AppParams> dlc_apps_params;
-  for (auto dlc_id : dlc_module_ids_) {
+  for (const auto& dlc_id : dlc_module_ids_) {
     OmahaRequestParams::AppParams dlc_params{
         .active_counting_type = OmahaRequestParams::kDateBased,
         .name = dlc_id,
diff --git a/update_engine_client.cc b/update_engine_client.cc
index eb71841..a721f7a 100644
--- a/update_engine_client.cc
+++ b/update_engine_client.cc
@@ -202,7 +202,7 @@
 
 void UpdateWaitHandler::HandleStatusUpdate(const UpdateEngineStatus& status) {
   if (exit_on_error_ && status.status == UpdateStatus::IDLE) {
-    int last_attempt_error;
+    int last_attempt_error = static_cast<int>(ErrorCode::kSuccess);
     ErrorCode code = ErrorCode::kSuccess;
     if (client_ && client_->GetLastAttemptError(&last_attempt_error))
       code = static_cast<ErrorCode>(last_attempt_error);
diff --git a/update_manager/staging_utils.cc b/update_manager/staging_utils.cc
index 4835ab2..f4f685c 100644
--- a/update_manager/staging_utils.cc
+++ b/update_manager/staging_utils.cc
@@ -32,7 +32,6 @@
 using base::TimeDelta;
 using chromeos_update_engine::kPrefsWallClockStagingWaitPeriod;
 using chromeos_update_engine::PrefsInterface;
-using chromeos_update_engine::SystemState;
 using policy::DevicePolicy;
 
 namespace chromeos_update_manager {