Fix regression where FPDFText_FindNext() cannot find the search term

Relax the check added to fix crbug.com/pdfium/2104 and only check the
page text's bounds when the search term has a trailing space. This
effectively implements what the bug reporter for crbug.com/pdfium/2104
suggested as the fix.

Bug: pdfium:2111
Change-Id: I2eb8dc09fa394e0916ecc1e9616d4df3d037abed
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/114931
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdftext/cpdf_textpagefind.cpp b/core/fpdftext/cpdf_textpagefind.cpp
index 9d8a534..92810ca 100644
--- a/core/fpdftext/cpdf_textpagefind.cpp
+++ b/core/fpdftext/cpdf_textpagefind.cpp
@@ -227,6 +227,9 @@
     WideString csWord = m_csFindWhatArray[iWord];
     if (csWord.IsEmpty()) {
       if (iWord == nCount - 1) {
+        if (nStartPos >= strLen) {
+          return false;
+        }
         wchar_t strInsert = m_strText[nStartPos];
         if (strInsert == L'\n' || strInsert == L' ' || strInsert == L'\r' ||
             strInsert == kNonBreakingSpace) {
@@ -287,9 +290,6 @@
       size_t index = bSpaceStart ? 1 : 0;
       nStartPos = m_resStart + m_csFindWhatArray[index].GetLength();
     }
-    if (nStartPos >= strLen) {
-      return false;
-    }
   }
   m_resEnd = nResultPos.value() + m_csFindWhatArray.back().GetLength() - 1;
   if (m_options.bConsecutive) {
diff --git a/fpdfsdk/fpdf_text_embeddertest.cpp b/fpdfsdk/fpdf_text_embeddertest.cpp
index 6e9470c..1a5ac32 100644
--- a/fpdfsdk/fpdf_text_embeddertest.cpp
+++ b/fpdfsdk/fpdf_text_embeddertest.cpp
@@ -512,9 +512,9 @@
     EXPECT_EQ(7, FPDFText_GetSchResultIndex(search.get()));
     EXPECT_EQ(6, FPDFText_GetSchCount(search.get()));
 
-    // TODO(crbug.com/pdfium/2111): This should return true, and the result has
-    // index = 24 and count = 6.
-    EXPECT_FALSE(FPDFText_FindNext(search.get()));
+    EXPECT_TRUE(FPDFText_FindNext(search.get()));
+    EXPECT_EQ(24, FPDFText_GetSchResultIndex(search.get()));
+    EXPECT_EQ(6, FPDFText_GetSchCount(search.get()));
   }
 
   UnloadPage(page);