Replace overly-strong DCHECK() in CPDF_FormField::SetItemSelection().

Early return in error should the condition occur.

-- Same for UseSelectedIndicesObject().

Bug: chromium:1477093
Change-Id: I0075020a4d843895e3f9b69e6b5a26467ab66baa
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/111552
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Nigi <nigi@chromium.org>
diff --git a/core/fpdfdoc/cpdf_formfield.cpp b/core/fpdfdoc/cpdf_formfield.cpp
index 705827d..f4a62bd 100644
--- a/core/fpdfdoc/cpdf_formfield.cpp
+++ b/core/fpdfdoc/cpdf_formfield.cpp
@@ -493,20 +493,24 @@
 }
 
 bool CPDF_FormField::IsItemSelected(int index) const {
-  DCHECK(GetType() == kComboBox || GetType() == kListBox);
-  if (index < 0 || index >= CountOptions())
+  if (GetType() != kComboBox && GetType() != kListBox) {
     return false;
-
+  }
+  if (index < 0 || index >= CountOptions()) {
+    return false;
+  }
   // First consider the /I entry if it is valid, then fall back to the /V entry.
   return m_bUseSelectedIndices ? IsSelectedIndex(index)
                                : IsSelectedOption(GetOptionValue(index));
 }
 
 bool CPDF_FormField::SetItemSelection(int index, NotificationOption notify) {
-  DCHECK(GetType() == kComboBox || GetType() == kListBox);
-  if (index < 0 || index >= CountOptions())
+  if (GetType() != kComboBox && GetType() != kListBox) {
     return false;
-
+  }
+  if (index < 0 || index >= CountOptions()) {
+    return false;
+  }
   WideString opt_value = GetOptionValue(index);
   if (notify == NotificationOption::kNotify &&
       !NotifyListOrComboBoxBeforeChange(opt_value)) {
diff --git a/fpdfsdk/fpdf_formfill_embeddertest.cpp b/fpdfsdk/fpdf_formfill_embeddertest.cpp
index 3f3f51e..259a4fb 100644
--- a/fpdfsdk/fpdf_formfill_embeddertest.cpp
+++ b/fpdfsdk/fpdf_formfill_embeddertest.cpp
@@ -1316,6 +1316,22 @@
   FORM_OnLButtonUp(form_handle(), page, 0, 140, 590);
   UnloadPage(page);
 }
+
+// Test passes if DCHECK() not hit.
+TEST_F(FPDFFormFillEmbedderTest, BUG_1477093) {
+  EmbedderTestTimerHandlingDelegate delegate;
+  SetDelegate(&delegate);
+
+  ASSERT_TRUE(OpenDocument("bug_1477093.pdf"));
+  FPDF_PAGE page = LoadPage(0);
+  EXPECT_TRUE(page);
+
+  DoOpenActions();
+  delegate.AdvanceTime(1000);
+  delegate.AdvanceTime(1000);
+  UnloadPage(page);
+}
+
 #endif  // PDF_ENABLE_V8
 
 TEST_F(FPDFFormFillEmbedderTest, FormText) {
diff --git a/testing/resources/bug_1477093.in b/testing/resources/bug_1477093.in
new file mode 100644
index 0000000..4260e7f
--- /dev/null
+++ b/testing/resources/bug_1477093.in
@@ -0,0 +1,66 @@
+{{header}}
+{{object 1 0}} <<
+  /Type /Catalog
+  /OpenAction 7 0 R
+  /Pages 2 0 R
+>>
+endobj
+{{object 2 0}} <<
+  /Type /Pages
+  /Kids [3 0 R]
+  /Count 1
+>>
+endobj
+{{object 3 0}} <<
+  /Type /Page
+  /Parent 2 0 R
+  /Annots [6 0 R]
+  /MediaBox [0 0 612 792]
+  /Contents [4 0 R]
+>>
+endobj
+{{object 4 0}} <<
+  {{streamlen}}
+>>
+stream
+% start the stream
+1 0 0 1 50 700 cm % text area(50,700)
+BT  % start  the text content
+ /F0 36 Tf         % font size
+ (Hope u are UseFul!) Tj % font content
+ET  % end the text content
+endstream
+endobj
+{{object 5 0}}  <<
+  {{streamlen}}
+>>
+stream
+  function run() {
+    this.getField('bad_field').value = 'Apple';
+  }
+  app.setTimeOut('run()', 1000 );
+endstream
+endobj
+{{object 6 0}} <<
+  /Type /Annot
+  /Subtype /Widget
+  /F 4
+  /FT /Tx
+  /Opt [(Apple) (Banana)]
+  /Rect [200 200 400 400]
+  /T (bad_field)
+  /V <<
+    /A (b)
+  >>
+>>
+endobj
+{{object 7 0}} <<
+  /Type /Action
+  /JS 5 0 R
+  /S /JavaScript
+>>
+endobj
+{{xref}}
+{{trailer}}
+{{startxref}}
+%%EOF
diff --git a/testing/resources/bug_1477093.pdf b/testing/resources/bug_1477093.pdf
new file mode 100644
index 0000000..c04402b
--- /dev/null
+++ b/testing/resources/bug_1477093.pdf
@@ -0,0 +1,80 @@
+%PDF-1.7
+% ò¤ô
+1 0 obj <<
+  /Type /Catalog
+  /OpenAction 7 0 R
+  /Pages 2 0 R
+>>
+endobj
+2 0 obj <<
+  /Type /Pages
+  /Kids [3 0 R]
+  /Count 1
+>>
+endobj
+3 0 obj <<
+  /Type /Page
+  /Parent 2 0 R
+  /Annots [6 0 R]
+  /MediaBox [0 0 612 792]
+  /Contents [4 0 R]
+>>
+endobj
+4 0 obj <<
+  /Length 189
+>>
+stream
+% start the stream
+1 0 0 1 50 700 cm % text area(50,700)
+BT  % start  the text content
+ /F0 36 Tf         % font size
+ (Hope u are UseFul!) Tj % font content
+ET  % end the text content
+endstream
+endobj
+5 0 obj  <<
+  /Length 105
+>>
+stream
+  function run() {
+    this.getField('bad_field').value = 'Apple';
+  }
+  app.setTimeOut('run()', 1000 );
+endstream
+endobj
+6 0 obj <<
+  /Type /Annot
+  /Subtype /Widget
+  /F 4
+  /FT /Tx
+  /Opt [(Apple) (Banana)]
+  /Rect [200 200 400 400]
+  /T (bad_field)
+  /V <<
+    /A (b)
+  >>
+>>
+endobj
+7 0 obj <<
+  /Type /Action
+  /JS 5 0 R
+  /S /JavaScript
+>>
+endobj
+xref
+0 8
+0000000000 65535 f 
+0000000015 00000 n 
+0000000088 00000 n 
+0000000151 00000 n 
+0000000266 00000 n 
+0000000507 00000 n 
+0000000665 00000 n 
+0000000830 00000 n 
+trailer <<
+  /Root 1 0 R
+  /Size 8
+>>
+startxref
+896
+%%EOF