Assert CPDF_AnnotContext always has a valid dictionary and page.

With this assertion, many nullptr checks in callers to CPDF_AnnotContext
getter methods can go away.

Change-Id: I4f4535f35a4cb7c91a5ad506da43660fb3051f3d
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/51330
Reviewed-by: Henrique Nakashima <hnakashima@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfdoc/cpdf_annot.cpp b/core/fpdfdoc/cpdf_annot.cpp
index 354caa2..9c7649c 100644
--- a/core/fpdfdoc/cpdf_annot.cpp
+++ b/core/fpdfdoc/cpdf_annot.cpp
@@ -184,11 +184,13 @@
 
 CPDF_Stream* GetAnnotAP(CPDF_Dictionary* pAnnotDict,
                         CPDF_Annot::AppearanceMode eMode) {
+  ASSERT(pAnnotDict);
   return GetAnnotAPInternal(pAnnotDict, eMode, true);
 }
 
 CPDF_Stream* GetAnnotAPNoFallback(CPDF_Dictionary* pAnnotDict,
                                   CPDF_Annot::AppearanceMode eMode) {
+  ASSERT(pAnnotDict);
   return GetAnnotAPInternal(pAnnotDict, eMode, false);
 }
 
diff --git a/fpdfsdk/cpdf_annotcontext.cpp b/fpdfsdk/cpdf_annotcontext.cpp
index c50435e..0beebe1 100644
--- a/fpdfsdk/cpdf_annotcontext.cpp
+++ b/fpdfsdk/cpdf_annotcontext.cpp
@@ -14,7 +14,10 @@
 
 CPDF_AnnotContext::CPDF_AnnotContext(CPDF_Dictionary* pAnnotDict,
                                      CPDF_Page* pPage)
-    : m_pAnnotDict(pAnnotDict), m_pPage(pPage) {}
+    : m_pAnnotDict(pAnnotDict), m_pPage(pPage) {
+  ASSERT(m_pAnnotDict);
+  ASSERT(m_pPage);
+}
 
 CPDF_AnnotContext::~CPDF_AnnotContext() = default;
 
diff --git a/fpdfsdk/cpdf_annotcontext.h b/fpdfsdk/cpdf_annotcontext.h
index 5071210..cab81e7 100644
--- a/fpdfsdk/cpdf_annotcontext.h
+++ b/fpdfsdk/cpdf_annotcontext.h
@@ -24,13 +24,17 @@
   void SetForm(CPDF_Stream* pStream);
   bool HasForm() const { return !!m_pAnnotForm; }
   CPDF_Form* GetForm() const { return m_pAnnotForm.get(); }
+
+  // Never nullptr.
   CPDF_Dictionary* GetAnnotDict() const { return m_pAnnotDict.Get(); }
+
+  // Never nullptr.
   CPDF_Page* GetPage() const { return m_pPage.Get(); }
 
  private:
   std::unique_ptr<CPDF_Form> m_pAnnotForm;
-  UnownedPtr<CPDF_Dictionary> m_pAnnotDict;
-  UnownedPtr<CPDF_Page> m_pPage;
+  UnownedPtr<CPDF_Dictionary> const m_pAnnotDict;
+  UnownedPtr<CPDF_Page> const m_pPage;
 };
 
 #endif  // FPDFSDK_CPDF_ANNOTCONTEXT_H_
diff --git a/fpdfsdk/cpdfsdk_helpers.cpp b/fpdfsdk/cpdfsdk_helpers.cpp
index ed6513b..63ee361 100644
--- a/fpdfsdk/cpdfsdk_helpers.cpp
+++ b/fpdfsdk/cpdfsdk_helpers.cpp
@@ -200,16 +200,14 @@
 
 const CPDF_Array* GetQuadPointsArrayFromDictionary(
     const CPDF_Dictionary* dict) {
-  return dict ? dict->GetArrayFor("QuadPoints") : nullptr;
+  return dict->GetArrayFor("QuadPoints");
 }
 
 CPDF_Array* GetQuadPointsArrayFromDictionary(CPDF_Dictionary* dict) {
-  return dict ? dict->GetArrayFor("QuadPoints") : nullptr;
+  return dict->GetArrayFor("QuadPoints");
 }
 
 CPDF_Array* AddQuadPointsArrayToDictionary(CPDF_Dictionary* dict) {
-  if (!dict)
-    return nullptr;
   return dict->SetNewFor<CPDF_Array>(kQuadPoints);
 }
 
diff --git a/fpdfsdk/fpdf_annot.cpp b/fpdfsdk/fpdf_annot.cpp
index 62b294d..22d09f1 100644
--- a/fpdfsdk/fpdf_annot.cpp
+++ b/fpdfsdk/fpdf_annot.cpp
@@ -197,6 +197,7 @@
 }
 
 void UpdateBBox(CPDF_Dictionary* annot_dict) {
+  ASSERT(annot_dict);
   // Update BBox entry in appearance stream based on the bounding rectangle
   // of the annotation's quadpoints.
   CPDF_Stream* pStream =
@@ -343,7 +344,7 @@
 FPDFAnnot_UpdateObject(FPDF_ANNOTATION annot, FPDF_PAGEOBJECT obj) {
   CPDF_AnnotContext* pAnnot = CPDFAnnotContextFromFPDFAnnotation(annot);
   CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(obj);
-  if (!pAnnot || !pAnnot->GetAnnotDict() || !pAnnot->HasForm() || !pObj)
+  if (!pAnnot || !pAnnot->HasForm() || !pObj)
     return false;
 
   // Check that the annotation type is supported by this method.
@@ -380,20 +381,17 @@
   if (!pAnnot || !pObj)
     return false;
 
-  CPDF_Dictionary* pAnnotDict = pAnnot->GetAnnotDict();
-  CPDF_Page* pPage = pAnnot->GetPage();
-  if (!pAnnotDict || !pPage)
-    return false;
-
   // Check that the annotation type is supported by this method.
   if (!FPDFAnnot_IsObjectSupportedSubtype(FPDFAnnot_GetSubtype(annot)))
     return false;
 
   // If the annotation does not have an AP stream yet, generate and set it.
+  CPDF_Dictionary* pAnnotDict = pAnnot->GetAnnotDict();
   CPDF_Stream* pStream =
-      GetAnnotAP(pAnnot->GetAnnotDict(), CPDF_Annot::AppearanceMode::Normal);
+      GetAnnotAP(pAnnotDict, CPDF_Annot::AppearanceMode::Normal);
   if (!pStream) {
-    CPVT_GenerateAP::GenerateEmptyAP(pPage->GetDocument(), pAnnotDict);
+    CPVT_GenerateAP::GenerateEmptyAP(pAnnot->GetPage()->GetDocument(),
+                                     pAnnotDict);
     pStream = GetAnnotAP(pAnnotDict, CPDF_Annot::AppearanceMode::Normal);
     if (!pStream)
       return false;
@@ -428,7 +426,7 @@
 
 FPDF_EXPORT int FPDF_CALLCONV FPDFAnnot_GetObjectCount(FPDF_ANNOTATION annot) {
   CPDF_AnnotContext* pAnnot = CPDFAnnotContextFromFPDFAnnotation(annot);
-  if (!pAnnot || !pAnnot->GetAnnotDict())
+  if (!pAnnot)
     return 0;
 
   if (!pAnnot->HasForm()) {
@@ -445,7 +443,7 @@
 FPDF_EXPORT FPDF_PAGEOBJECT FPDF_CALLCONV
 FPDFAnnot_GetObject(FPDF_ANNOTATION annot, int index) {
   CPDF_AnnotContext* pAnnot = CPDFAnnotContextFromFPDFAnnotation(annot);
-  if (!pAnnot || !pAnnot->GetAnnotDict() || index < 0)
+  if (!pAnnot || index < 0)
     return nullptr;
 
   if (!pAnnot->HasForm()) {
@@ -464,7 +462,7 @@
 FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
 FPDFAnnot_RemoveObject(FPDF_ANNOTATION annot, int index) {
   CPDF_AnnotContext* pAnnot = CPDFAnnotContextFromFPDFAnnotation(annot);
-  if (!pAnnot || !pAnnot->GetAnnotDict() || !pAnnot->HasForm() || index < 0)
+  if (!pAnnot || !pAnnot->HasForm() || index < 0)
     return false;
 
   // Check that the annotation type is supported by this method.
@@ -646,9 +644,6 @@
 
   CPDF_Dictionary* pAnnotDict =
       CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
-  if (!pAnnotDict)
-    return false;
-
   const CPDF_Array* pArray = GetQuadPointsArrayFromDictionary(pAnnotDict);
   if (!pArray)
     return false;
@@ -798,7 +793,7 @@
 FPDF_EXPORT FPDF_ANNOTATION FPDF_CALLCONV
 FPDFAnnot_GetLinkedAnnot(FPDF_ANNOTATION annot, FPDF_BYTESTRING key) {
   CPDF_AnnotContext* pAnnot = CPDFAnnotContextFromFPDFAnnotation(annot);
-  if (!pAnnot || !pAnnot->GetAnnotDict())
+  if (!pAnnot)
     return nullptr;
 
   CPDF_Dictionary* pLinkedDict = pAnnot->GetAnnotDict()->GetDictFor(key);