Fix various nits inside fpdf_edittext.cpp

- Use a checked_cast() to ensure a span's size is not too large.
- Use CHECK() to ensure FXSYS_ToUTF16BE() result handling does not go
  out of bounds.
- Remove a comment about compressing a stream. That's done by the code
  which writes out streams.
- Other trivial cleanups.

Change-Id: I8cd93d997c540be4efe6848efe8e5be41a88f91c
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/115791
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Thomas Sepez <tsepez@google.com>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/page/cpdf_colorspace.cpp b/core/fpdfapi/page/cpdf_colorspace.cpp
index 1b3e987..21bf30a 100644
--- a/core/fpdfapi/page/cpdf_colorspace.cpp
+++ b/core/fpdfapi/page/cpdf_colorspace.cpp
@@ -525,9 +525,6 @@
 
   if (const CPDF_Stream* pStream = pObj->AsStream()) {
     RetainPtr<const CPDF_Dictionary> pDict = pStream->GetDict();
-    if (!pDict)
-      return nullptr;
-
     CPDF_DictionaryLocker locker(std::move(pDict));
     for (const auto& it : locker) {
       RetainPtr<const CPDF_Name> pValue = ToName(it.second);
@@ -946,7 +943,7 @@
   // PDF viewers tolerate invalid values, Acrobat does not, so be consistent
   // with Acrobat and reject bad values.
   RetainPtr<const CPDF_Dictionary> pDict = pStream->GetDict();
-  const int32_t nDictComponents = pDict ? pDict->GetIntegerFor("N") : 0;
+  const int32_t nDictComponents = pDict->GetIntegerFor("N");
   if (!fxcodec::IccTransform::IsValidIccComponents(nDictComponents)) {
     return 0;
   }
diff --git a/core/fpdfapi/page/cpdf_dib.cpp b/core/fpdfapi/page/cpdf_dib.cpp
index cb8de1c..c538e43 100644
--- a/core/fpdfapi/page/cpdf_dib.cpp
+++ b/core/fpdfapi/page/cpdf_dib.cpp
@@ -791,9 +791,6 @@
     return false;
 
   m_pDict = m_pStream->GetDict();
-  if (!m_pDict)
-    return false;
-
   m_Width = m_pDict->GetIntegerFor("Width");
   m_Height = m_pDict->GetIntegerFor("Height");
   if (!IsValidDimension(m_Width) || !IsValidDimension(m_Height))
diff --git a/core/fpdfapi/page/cpdf_docpagedata.cpp b/core/fpdfapi/page/cpdf_docpagedata.cpp
index cfe78fe..01ea481 100644
--- a/core/fpdfapi/page/cpdf_docpagedata.cpp
+++ b/core/fpdfapi/page/cpdf_docpagedata.cpp
@@ -347,37 +347,33 @@
 RetainPtr<CPDF_Pattern> CPDF_DocPageData::GetPattern(
     RetainPtr<CPDF_Object> pPatternObj,
     const CFX_Matrix& matrix) {
-  if (!pPatternObj)
-    return nullptr;
+  CHECK(pPatternObj->IsDictionary() || pPatternObj->IsStream());
 
   auto it = m_PatternMap.find(pPatternObj);
   if (it != m_PatternMap.end() && it->second)
     return pdfium::WrapRetain(it->second.Get());
 
-  RetainPtr<const CPDF_Dictionary> pDict = pPatternObj->GetDict();
-  if (!pDict)
-    return nullptr;
-
-  RetainPtr<CPDF_Pattern> pPattern;
-  int type = pDict->GetIntegerFor("PatternType");
-  if (type == CPDF_Pattern::kTiling) {
-    pPattern = pdfium::MakeRetain<CPDF_TilingPattern>(GetDocument(),
-                                                      pPatternObj, matrix);
-  } else if (type == CPDF_Pattern::kShading) {
-    pPattern = pdfium::MakeRetain<CPDF_ShadingPattern>(
-        GetDocument(), pPatternObj, false, matrix);
-  } else {
-    return nullptr;
+  RetainPtr<CPDF_Pattern> pattern;
+  switch (pPatternObj->GetDict()->GetIntegerFor("PatternType")) {
+    case CPDF_Pattern::kTiling:
+      pattern = pdfium::MakeRetain<CPDF_TilingPattern>(GetDocument(),
+                                                       pPatternObj, matrix);
+      break;
+    case CPDF_Pattern::kShading:
+      pattern = pdfium::MakeRetain<CPDF_ShadingPattern>(
+          GetDocument(), pPatternObj, false, matrix);
+      break;
+    default:
+      return nullptr;
   }
-  m_PatternMap[pPatternObj].Reset(pPattern.Get());
-  return pPattern;
+  m_PatternMap[pPatternObj].Reset(pattern.Get());
+  return pattern;
 }
 
 RetainPtr<CPDF_ShadingPattern> CPDF_DocPageData::GetShading(
     RetainPtr<CPDF_Object> pPatternObj,
     const CFX_Matrix& matrix) {
-  if (!pPatternObj)
-    return nullptr;
+  CHECK(pPatternObj->IsDictionary() || pPatternObj->IsStream());
 
   auto it = m_PatternMap.find(pPatternObj);
   if (it != m_PatternMap.end() && it->second)
diff --git a/core/fpdfapi/page/cpdf_expintfunc.cpp b/core/fpdfapi/page/cpdf_expintfunc.cpp
index a8de8b9..6f642c9 100644
--- a/core/fpdfapi/page/cpdf_expintfunc.cpp
+++ b/core/fpdfapi/page/cpdf_expintfunc.cpp
@@ -22,10 +22,8 @@
 CPDF_ExpIntFunc::~CPDF_ExpIntFunc() = default;
 
 bool CPDF_ExpIntFunc::v_Init(const CPDF_Object* pObj, VisitedSet* pVisited) {
+  CHECK(pObj->IsDictionary() || pObj->IsStream());
   RetainPtr<const CPDF_Dictionary> pDict = pObj->GetDict();
-  if (!pDict)
-    return false;
-
   RetainPtr<const CPDF_Number> pExponent = pDict->GetNumberFor("N");
   if (!pExponent)
     return false;
diff --git a/core/fpdfapi/page/cpdf_function.h b/core/fpdfapi/page/cpdf_function.h
index 2039e61..8fc4e72 100644
--- a/core/fpdfapi/page/cpdf_function.h
+++ b/core/fpdfapi/page/cpdf_function.h
@@ -62,6 +62,7 @@
       RetainPtr<const CPDF_Object> pFuncObj,
       VisitedSet* pVisited);
   bool Init(const CPDF_Object* pObj, VisitedSet* pVisited);
+  // `pObj` is guaranteed to be either a dictionary or a stream.
   virtual bool v_Init(const CPDF_Object* pObj, VisitedSet* pVisited) = 0;
   virtual bool v_Call(pdfium::span<const float> inputs,
                       pdfium::span<float> results) const = 0;
diff --git a/core/fpdfapi/page/cpdf_stitchfunc.cpp b/core/fpdfapi/page/cpdf_stitchfunc.cpp
index 2a303bf..21e5c80 100644
--- a/core/fpdfapi/page/cpdf_stitchfunc.cpp
+++ b/core/fpdfapi/page/cpdf_stitchfunc.cpp
@@ -28,10 +28,8 @@
   if (m_nInputs != kRequiredNumInputs)
     return false;
 
+  CHECK(pObj->IsDictionary() || pObj->IsStream());
   RetainPtr<const CPDF_Dictionary> pDict = pObj->GetDict();
-  if (!pDict)
-    return false;
-
   RetainPtr<const CPDF_Array> pFunctionsArray = pDict->GetArrayFor("Functions");
   if (!pFunctionsArray)
     return false;
diff --git a/core/fpdfapi/page/cpdf_streamcontentparser.cpp b/core/fpdfapi/page/cpdf_streamcontentparser.cpp
index 21a8428..38fead5 100644
--- a/core/fpdfapi/page/cpdf_streamcontentparser.cpp
+++ b/core/fpdfapi/page/cpdf_streamcontentparser.cpp
@@ -746,10 +746,7 @@
   if (!pXObject)
     return;
 
-  ByteString type;
-  if (pXObject->GetDict())
-    type = pXObject->GetDict()->GetByteStringFor("Subtype");
-
+  const ByteString type = pXObject->GetDict()->GetByteStringFor("Subtype");
   if (type == "Form") {
     if (m_RecursionState->form_count > kFormCountLimit) {
       return;
diff --git a/core/fpdfapi/parser/cpdf_hint_tables.cpp b/core/fpdfapi/parser/cpdf_hint_tables.cpp
index 282ec89..2e854f8 100644
--- a/core/fpdfapi/parser/cpdf_hint_tables.cpp
+++ b/core/fpdfapi/parser/cpdf_hint_tables.cpp
@@ -444,11 +444,8 @@
   if (!pHintStream || !m_pLinearized->HasHintTable())
     return false;
 
-  RetainPtr<const CPDF_Dictionary> pDict = pHintStream->GetDict();
-  if (!pDict)
-    return false;
-
-  RetainPtr<const CPDF_Object> pOffset = pDict->GetObjectFor("S");
+  RetainPtr<const CPDF_Object> pOffset =
+      pHintStream->GetDict()->GetObjectFor("S");
   if (!pOffset || !pOffset->IsNumber())
     return false;
 
diff --git a/core/fpdfapi/parser/cpdf_object_unittest.cpp b/core/fpdfapi/parser/cpdf_object_unittest.cpp
index cf1f1b1..ed366a2 100644
--- a/core/fpdfapi/parser/cpdf_object_unittest.cpp
+++ b/core/fpdfapi/parser/cpdf_object_unittest.cpp
@@ -157,8 +157,6 @@
       case CPDF_Object::kStream: {
         RetainPtr<const CPDF_Stream> stream1(obj1->AsStream());
         RetainPtr<const CPDF_Stream> stream2(obj2->AsStream());
-        if (!stream1->GetDict() && !stream2->GetDict())
-          return true;
         // Compare dictionaries.
         if (!Equal(stream1->GetDict().Get(), stream2->GetDict().Get()))
           return false;
diff --git a/core/fpdfapi/parser/cpdf_stream.cpp b/core/fpdfapi/parser/cpdf_stream.cpp
index cfa7dfc..d1fa557 100644
--- a/core/fpdfapi/parser/cpdf_stream.cpp
+++ b/core/fpdfapi/parser/cpdf_stream.cpp
@@ -105,7 +105,7 @@
 
   RetainPtr<const CPDF_Dictionary> pDict = GetDict();
   RetainPtr<CPDF_Dictionary> pNewDict;
-  if (pDict && !pdfium::Contains(*pVisited, pDict.Get())) {
+  if (!pdfium::Contains(*pVisited, pDict.Get())) {
     pNewDict = ToDictionary(static_cast<const CPDF_Object*>(pDict.Get())
                                 ->CloneNonCyclic(bDirect, pVisited));
   }
diff --git a/core/fpdfdoc/cpdf_filespec.cpp b/core/fpdfdoc/cpdf_filespec.cpp
index 2e982b2..f650daf 100644
--- a/core/fpdfdoc/cpdf_filespec.cpp
+++ b/core/fpdfdoc/cpdf_filespec.cpp
@@ -154,11 +154,7 @@
 
 RetainPtr<const CPDF_Dictionary> CPDF_FileSpec::GetParamsDict() const {
   RetainPtr<const CPDF_Stream> pStream = GetFileStream();
-  if (!pStream)
-    return nullptr;
-
-  RetainPtr<const CPDF_Dictionary> pDict = pStream->GetDict();
-  return pDict ? pDict->GetDictFor("Params") : nullptr;
+  return pStream ? pStream->GetDict()->GetDictFor("Params") : nullptr;
 }
 
 RetainPtr<CPDF_Dictionary> CPDF_FileSpec::GetMutableParamsDict() {
diff --git a/core/fpdfdoc/cpdf_generateap.cpp b/core/fpdfdoc/cpdf_generateap.cpp
index b6a24fd..abb58db 100644
--- a/core/fpdfdoc/cpdf_generateap.cpp
+++ b/core/fpdfdoc/cpdf_generateap.cpp
@@ -1312,9 +1312,6 @@
 
   pNormalStream->SetDataFromStringstreamAndRemoveFilter(&sAppStream);
   pStreamDict = pNormalStream->GetMutableDict();
-  if (!pStreamDict)
-    return;
-
   pStreamDict->SetMatrixFor("Matrix", matrix);
   pStreamDict->SetRectFor("BBox", rcBBox);
   RetainPtr<CPDF_Dictionary> pStreamResList =
diff --git a/core/fpdfdoc/cpdf_icon.cpp b/core/fpdfdoc/cpdf_icon.cpp
index 8c4909a..65a678c 100644
--- a/core/fpdfdoc/cpdf_icon.cpp
+++ b/core/fpdfdoc/cpdf_icon.cpp
@@ -17,26 +17,14 @@
 CPDF_Icon::~CPDF_Icon() = default;
 
 CFX_SizeF CPDF_Icon::GetImageSize() const {
-  RetainPtr<const CPDF_Dictionary> pDict = m_pStream->GetDict();
-  if (!pDict)
-    return CFX_SizeF();
-
-  CFX_FloatRect rect = pDict->GetRectFor("BBox");
+  CFX_FloatRect rect = m_pStream->GetDict()->GetRectFor("BBox");
   return {rect.right - rect.left, rect.top - rect.bottom};
 }
 
 CFX_Matrix CPDF_Icon::GetImageMatrix() const {
-  RetainPtr<const CPDF_Dictionary> pDict = m_pStream->GetDict();
-  if (!pDict)
-    return CFX_Matrix();
-
-  return pDict->GetMatrixFor("Matrix");
+  return m_pStream->GetDict()->GetMatrixFor("Matrix");
 }
 
 ByteString CPDF_Icon::GetImageAlias() const {
-  RetainPtr<const CPDF_Dictionary> pDict = m_pStream->GetDict();
-  if (!pDict)
-    return ByteString();
-
-  return pDict->GetByteStringFor("Name");
+  return m_pStream->GetDict()->GetByteStringFor("Name");
 }
diff --git a/fpdfsdk/cpdfsdk_appstream.cpp b/fpdfsdk/cpdfsdk_appstream.cpp
index f076104..d89fb80 100644
--- a/fpdfsdk/cpdfsdk_appstream.cpp
+++ b/fpdfsdk/cpdfsdk_appstream.cpp
@@ -1092,9 +1092,6 @@
     return;
 
   RetainPtr<CPDF_Dictionary> pImageDict = pIcon->GetMutableDict();
-  if (!pImageDict)
-    return;
-
   if (pImageDict->KeyExist("Name"))
     return;
 
@@ -1801,14 +1798,11 @@
 }
 
 void CPDFSDK_AppStream::AddImage(const ByteString& sAPType,
-                                 CPDF_Stream* pImage) {
+                                 const CPDF_Stream* pImage) {
   RetainPtr<CPDF_Stream> pStream = dict_->GetMutableStreamFor(sAPType);
   RetainPtr<CPDF_Dictionary> pStreamDict = pStream->GetMutableDict();
-  ByteString sImageAlias = "IMG";
 
-  RetainPtr<const CPDF_Dictionary> pImageDict = pImage->GetDict();
-  if (pImageDict)
-    sImageAlias = pImageDict->GetByteStringFor("Name");
+  const ByteString sImageAlias = pImage->GetDict()->GetByteStringFor("Name");
 
   RetainPtr<CPDF_Dictionary> pStreamResList =
       pStreamDict->GetOrCreateDictFor("Resources");
diff --git a/fpdfsdk/cpdfsdk_appstream.h b/fpdfsdk/cpdfsdk_appstream.h
index aabef24..d3cc07c 100644
--- a/fpdfsdk/cpdfsdk_appstream.h
+++ b/fpdfsdk/cpdfsdk_appstream.h
@@ -29,7 +29,7 @@
   void SetAsTextField(absl::optional<WideString> sValue);
 
  private:
-  void AddImage(const ByteString& sAPType, CPDF_Stream* pImage);
+  void AddImage(const ByteString& sAPType, const CPDF_Stream* pImage);
   void Write(const ByteString& sAPType,
              const ByteString& sContents,
              const ByteString& sAPState);
diff --git a/fpdfsdk/fpdf_flatten.cpp b/fpdfsdk/fpdf_flatten.cpp
index c62a4ba..d3303da 100644
--- a/fpdfsdk/fpdf_flatten.cpp
+++ b/fpdfsdk/fpdf_flatten.cpp
@@ -338,59 +338,65 @@
     if (!pAnnotAP)
       continue;
 
-    RetainPtr<CPDF_Stream> pAPStream = pAnnotAP->GetMutableStreamFor("N");
-    if (!pAPStream) {
-      RetainPtr<CPDF_Dictionary> pAPDict = pAnnotAP->GetMutableDictFor("N");
-      if (!pAPDict)
+    RetainPtr<CPDF_Stream> original_ap_stream =
+        pAnnotAP->GetMutableStreamFor("N");
+    if (!original_ap_stream) {
+      RetainPtr<CPDF_Dictionary> original_ap_dict =
+          pAnnotAP->GetMutableDictFor("N");
+      if (!original_ap_dict) {
         continue;
+      }
 
       if (!sAnnotState.IsEmpty()) {
-        pAPStream = pAPDict->GetMutableStreamFor(sAnnotState);
+        original_ap_stream = original_ap_dict->GetMutableStreamFor(sAnnotState);
       } else {
-        if (pAPDict->size() > 0) {
-          CPDF_DictionaryLocker locker(pAPDict);
+        if (original_ap_dict->size() > 0) {
+          CPDF_DictionaryLocker locker(original_ap_dict);
           RetainPtr<CPDF_Object> pFirstObj = locker.begin()->second;
           if (pFirstObj) {
             if (pFirstObj->IsReference())
               pFirstObj = pFirstObj->GetMutableDirect();
             if (!pFirstObj->IsStream())
               continue;
-            pAPStream.Reset(pFirstObj->AsMutableStream());
+            original_ap_stream.Reset(pFirstObj->AsMutableStream());
           }
         }
       }
     }
-    if (!pAPStream)
+    if (!original_ap_stream) {
       continue;
+    }
 
-    RetainPtr<const CPDF_Dictionary> pAPDict = pAPStream->GetDict();
+    RetainPtr<const CPDF_Dictionary> original_ap_stream_dict =
+        original_ap_stream->GetDict();
     CFX_FloatRect rcStream;
-    if (pAPDict->KeyExist("Rect"))
-      rcStream = pAPDict->GetRectFor("Rect");
-    else if (pAPDict->KeyExist("BBox"))
-      rcStream = pAPDict->GetRectFor("BBox");
+    if (original_ap_stream_dict->KeyExist("Rect")) {
+      rcStream = original_ap_stream_dict->GetRectFor("Rect");
+    } else if (original_ap_stream_dict->KeyExist("BBox")) {
+      rcStream = original_ap_stream_dict->GetRectFor("BBox");
+    }
     rcStream.Normalize();
 
     if (rcStream.IsEmpty())
       continue;
 
-    RetainPtr<CPDF_Object> pObj = pAPStream;
-    if (pObj->IsInline()) {
-      pObj = pObj->Clone();
-      pDocument->AddIndirectObject(pObj);
+    RetainPtr<CPDF_Stream> ap_stream;
+    if (original_ap_stream->IsInline()) {
+      ap_stream = ToStream(original_ap_stream->Clone());
+      pDocument->AddIndirectObject(ap_stream);
+    } else {
+      ap_stream = original_ap_stream;
     }
 
-    RetainPtr<CPDF_Dictionary> pObjDict = pObj->GetMutableDict();
-    if (pObjDict) {
-      pObjDict->SetNewFor<CPDF_Name>("Type", "XObject");
-      pObjDict->SetNewFor<CPDF_Name>("Subtype", "Form");
-    }
+    RetainPtr<CPDF_Dictionary> ap_stream_dict = ap_stream->GetMutableDict();
+    ap_stream_dict->SetNewFor<CPDF_Name>("Type", "XObject");
+    ap_stream_dict->SetNewFor<CPDF_Name>("Subtype", "Form");
 
     RetainPtr<CPDF_Dictionary> pXObject =
         pNewXORes->GetOrCreateDictFor("XObject");
     ByteString sFormName = ByteString::Format("F%d", i);
     pXObject->SetNewFor<CPDF_Reference>(sFormName, pDocument,
-                                        pObj->GetObjNum());
+                                        ap_stream->GetObjNum());
 
     ByteString sStream;
     {
@@ -398,7 +404,7 @@
       pAcc->LoadAllDataFiltered();
       sStream = ByteString(pAcc->GetSpan());
     }
-    CFX_Matrix matrix = pAPDict->GetMatrixFor("Matrix");
+    CFX_Matrix matrix = original_ap_stream_dict->GetMatrixFor("Matrix");
     CFX_Matrix m = GetMatrix(rcAnnot, rcStream, matrix);
     m.b = 0;
     m.c = 0;
diff --git a/fpdfsdk/fpdf_ppo.cpp b/fpdfsdk/fpdf_ppo.cpp
index ddfbb2a..e45b036 100644
--- a/fpdfsdk/fpdf_ppo.cpp
+++ b/fpdfsdk/fpdf_ppo.cpp
@@ -329,9 +329,7 @@
       return true;
     }
     case CPDF_Object::kStream: {
-      CPDF_Stream* pStream = pObj->AsMutableStream();
-      RetainPtr<CPDF_Dictionary> pDict = pStream->GetMutableDict();
-      return pDict && UpdateReference(std::move(pDict));
+      return UpdateReference(pObj->AsMutableStream()->GetMutableDict());
     }
     default:
       return true;