Use optional<size_t> for CFX_XMLParser::entity_start_

Avoid use of ints for sizes, and use optional in place of a -1
sentinel now that the value is unsigned.

Change-Id: I4abb4d62dcaa770421b72955969e17fe74180380
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/86897
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fxcrt/xml/cfx_xmlparser.cpp b/core/fxcrt/xml/cfx_xmlparser.cpp
index 146c78b..6b324f0 100644
--- a/core/fxcrt/xml/cfx_xmlparser.cpp
+++ b/core/fxcrt/xml/cfx_xmlparser.cpp
@@ -262,7 +262,7 @@
           break;
         case FDE_XmlSyntaxState::AttriValue:
           if (ch == current_quote_character) {
-            if (entity_start_ > -1)
+            if (entity_start_.has_value())
               return false;
 
             current_quote_character = 0;
@@ -470,27 +470,27 @@
 void CFX_XMLParser::ProcessTextChar(wchar_t character) {
   current_text_.push_back(character);
 
-  if (entity_start_ > -1 && character == L';') {
+  if (entity_start_.has_value() && character == L';') {
     // Copy the entity out into a string and remove from the vector. When we
     // copy the entity we don't want to copy out the & or the ; so we start
     // shifted by one and want to copy 2 less characters in total.
-    WideString csEntity(current_text_.data() + entity_start_ + 1,
-                        current_text_.size() - entity_start_ - 2);
-    current_text_.erase(current_text_.begin() + entity_start_,
+    WideString csEntity(current_text_.data() + entity_start_.value() + 1,
+                        current_text_.size() - entity_start_.value() - 2);
+    current_text_.erase(current_text_.begin() + entity_start_.value(),
                         current_text_.end());
 
-    int32_t iLen = csEntity.GetLength();
+    size_t iLen = csEntity.GetLength();
     if (iLen > 0) {
       if (csEntity[0] == L'#') {
         uint32_t ch = 0;
         if (iLen > 1 && csEntity[1] == L'x') {
-          for (int32_t i = 2; i < iLen; i++) {
+          for (size_t i = 2; i < iLen; i++) {
             if (!FXSYS_IsHexDigit(csEntity[i]))
               break;
             ch = (ch << 4) + FXSYS_HexCharToInt(csEntity[i]);
           }
         } else {
-          for (int32_t i = 1; i < iLen; i++) {
+          for (size_t i = 1; i < iLen; i++) {
             if (!FXSYS_IsDecimalDigit(csEntity[i]))
               break;
             ch = ch * 10 + FXSYS_DecimalCharToInt(csEntity[i]);
@@ -516,9 +516,8 @@
         }
       }
     }
-
-    entity_start_ = -1;
-  } else if (entity_start_ < 0 && character == L'&') {
+    entity_start_ = absl::nullopt;
+  } else if (!entity_start_.has_value() && character == L'&') {
     entity_start_ = current_text_.size() - 1;
   }
 }
@@ -535,7 +534,7 @@
 
 WideString CFX_XMLParser::GetTextData() {
   WideString ret(current_text_.data(), current_text_.size());
-  entity_start_ = -1;
+  entity_start_ = absl::nullopt;
   current_text_.clear();
   current_text_.reserve(kCurrentTextReserve);
   return ret;
diff --git a/core/fxcrt/xml/cfx_xmlparser.h b/core/fxcrt/xml/cfx_xmlparser.h
index 7e1cba7..81490f2 100644
--- a/core/fxcrt/xml/cfx_xmlparser.h
+++ b/core/fxcrt/xml/cfx_xmlparser.h
@@ -13,6 +13,7 @@
 #include "core/fxcrt/fx_memory_wrappers.h"
 #include "core/fxcrt/fx_string.h"
 #include "core/fxcrt/retain_ptr.h"
+#include "third_party/abseil-cpp/absl/types/optional.h"
 
 class CFX_SeekableStreamProxy;
 class CFX_XMLDocument;
@@ -57,7 +58,7 @@
   RetainPtr<CFX_SeekableStreamProxy> stream_;
   std::vector<wchar_t, FxAllocAllocator<wchar_t>> current_text_;
   size_t xml_plane_size_ = 1024;
-  int32_t entity_start_ = -1;
+  absl::optional<size_t> entity_start_;
 };
 
 #endif  // CORE_FXCRT_XML_CFX_XMLPARSER_H_