update_engine: Skip post DownloadAction exclusions

Post |DownloadAction| don't have direct reference to |Payload|s held
within the |PayloadState|. Hence it's required to halt exclusions for
|Action|s post |DownloadAction|.

This is done by setting the |payload_index_| within |PayloadState| >= to
the |candidate_urls_|/|response_.packages| size.

DCHECKs added where |payload_index_| is used as usage may cause out of
bounds indexing.

This change removes the dangling reference to the last |Payload|, as
previously |NextPayload()| kept |payload_index_| pointing to the last
|Payload| within |PayloadState|.

BUG=chromium:928805
TEST=FEATURES=test emerge-$B update_engine

Change-Id: I3f6a9a3cc26bb84f94506e45e1d6e906624e5dd7
Reviewed-on: https://chromium-review.googlesource.com/c/aosp/platform/system/update_engine/+/2261292
Tested-by: Jae Hoon Kim <kimjae@chromium.org>
Reviewed-by: Amin Hassani <ahassani@chromium.org>
Commit-Queue: Amin Hassani <ahassani@chromium.org>
Auto-Submit: Jae Hoon Kim <kimjae@chromium.org>
diff --git a/payload_state.cc b/payload_state.cc
index 4b4120c..d626056 100644
--- a/payload_state.cc
+++ b/payload_state.cc
@@ -462,6 +462,7 @@
 }
 
 void PayloadState::IncrementFullPayloadAttemptNumber() {
+  DCHECK(payload_index_ < response_.packages.size());
   // Update the payload attempt number for full payloads and the backoff time.
   if (response_.packages[payload_index_].is_delta) {
     LOG(INFO) << "Not incrementing payload attempt number for delta payloads";
@@ -474,6 +475,7 @@
 }
 
 void PayloadState::IncrementUrlIndex() {
+  DCHECK(payload_index_ < candidate_urls_.size());
   size_t next_url_index = url_index_ + 1;
   size_t max_url_size = candidate_urls_[payload_index_].size();
   if (next_url_index < max_url_size) {
@@ -510,6 +512,10 @@
 }
 
 void PayloadState::ExcludeCurrentPayload() {
+  if (payload_index_ >= response_.packages.size()) {
+    LOG(INFO) << "Skipping exclusion of the current payload.";
+    return;
+  }
   const auto& package = response_.packages[payload_index_];
   if (!package.can_exclude) {
     LOG(INFO) << "Not excluding as marked non-excludable for package hash="
@@ -923,10 +929,12 @@
 }
 
 bool PayloadState::NextPayload() {
-  if (payload_index_ + 1 >= candidate_urls_.size())
+  if (payload_index_ >= candidate_urls_.size())
+    return false;
+  SetPayloadIndex(payload_index_ + 1);
+  if (payload_index_ >= candidate_urls_.size())
     return false;
   SetUrlIndex(0);
-  SetPayloadIndex(payload_index_ + 1);
   return true;
 }