Add nullptr checks prior to accesses of CPDF_Document::GetDocExtension()

Currently the CPDFXFA_Context creation occurs in LoadDocumentImpl()
regardless of whether --disable-xfa has been passed to pdfium_test.
In a future CL, we want to defer the context creation to
FPDFDOC_InitFormFillEnvironment() and make it conditional to whether the
runtime flag has been passed. However, certain parts of the code inside
PDF_ENABLE_XFA ifdefs always assume that the context exists and perform
no nullptr checks prior to accessing.

Along the way, fix a typo and change some unclear instances of auto
types to explicit types.

Bug: pdfium:1433
Change-Id: Ib1d63587ea6fc62d66cd933321c36182c3a21f42
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/64750
Commit-Queue: Daniel Hosseinian <dhoss@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fxcrt/fx_unicode.cpp b/core/fxcrt/fx_unicode.cpp
index 2aa33d4..cbd4f0d 100644
--- a/core/fxcrt/fx_unicode.cpp
+++ b/core/fxcrt/fx_unicode.cpp
@@ -75,7 +75,7 @@
   return 0;
 }
 
-#endif  // PDF_ENBABLE_XFA
+#endif  // PDF_ENABLE_XFA
 
 constexpr uint16_t kFXTextLayoutBidiMirror[] = {
     0x0029, 0x0028, 0x003E, 0x003C, 0x005D, 0x005B, 0x007D, 0x007B, 0x00BB,
diff --git a/fpdfsdk/cpdfsdk_pageview.cpp b/fpdfsdk/cpdfsdk_pageview.cpp
index 465cc00..9e4adc8 100644
--- a/fpdfsdk/cpdfsdk_pageview.cpp
+++ b/fpdfsdk/cpdfsdk_pageview.cpp
@@ -70,11 +70,9 @@
 
 #ifdef PDF_ENABLE_XFA
   IPDF_Page* pPage = GetXFAPage();
-  if (!pPage)
-    return;
-
-  CPDF_Document::Extension* pContext = pPage->GetDocument()->GetExtension();
-  if (pContext->ContainsExtensionFullForm()) {
+  CPDF_Document::Extension* pContext =
+      pPage ? pPage->GetDocument()->GetExtension() : nullptr;
+  if (pContext && pContext->ContainsExtensionFullForm()) {
     static_cast<CPDFXFA_Page*>(pPage)->DrawFocusAnnot(pDevice, GetFocusAnnot(),
                                                       mtUser2Device, pClip);
     return;
@@ -144,7 +142,7 @@
     return false;
 
   CPDF_Document::Extension* pContext = pPage->GetDocument()->GetExtension();
-  if (!pContext->ContainsExtensionForm())
+  if (pContext && !pContext->ContainsExtensionForm())
     return false;
 
   ObservedPtr<CPDFSDK_Annot> pObserved(pAnnot);
@@ -482,8 +480,8 @@
 
 #ifdef PDF_ENABLE_XFA
   RetainPtr<CPDFXFA_Page> protector(ToXFAPage(m_page));
-  auto* pContext = m_pFormFillEnv->GetDocExtension();
-  if (pContext->ContainsExtensionFullForm()) {
+  CPDF_Document::Extension* pContext = m_pFormFillEnv->GetDocExtension();
+  if (pContext && pContext->ContainsExtensionFullForm()) {
     CXFA_FFPageView* pageView = protector->GetXFAPageView();
     std::unique_ptr<IXFA_WidgetIterator> pWidgetHandler =
         pageView->CreateFormWidgetIterator(XFA_WidgetStatus_Visible |
@@ -535,7 +533,7 @@
 int CPDFSDK_PageView::GetPageIndex() const {
 #ifdef PDF_ENABLE_XFA
   CPDF_Document::Extension* pContext = m_page->GetDocument()->GetExtension();
-  if (pContext->ContainsExtensionFullForm()) {
+  if (pContext && pContext->ContainsExtensionFullForm()) {
     CXFA_FFPageView* pPageView = m_page->AsXFAPage()->GetXFAPageView();
     return pPageView ? pPageView->GetLayoutItem()->GetPageIndex() : -1;
   }
diff --git a/fpdfsdk/cpdfsdk_widget.cpp b/fpdfsdk/cpdfsdk_widget.cpp
index 102fdc1..6d92b74 100644
--- a/fpdfsdk/cpdfsdk_widget.cpp
+++ b/fpdfsdk/cpdfsdk_widget.cpp
@@ -52,8 +52,9 @@
 
 #ifdef PDF_ENABLE_XFA
 CXFA_FFWidget* CPDFSDK_Widget::GetMixXFAWidget() const {
-  auto* pContext = m_pPageView->GetFormFillEnv()->GetDocExtension();
-  if (!pContext->ContainsExtensionForegroundForm())
+  CPDF_Document::Extension* pContext =
+      m_pPageView->GetFormFillEnv()->GetDocExtension();
+  if (!pContext || !pContext->ContainsExtensionForegroundForm())
     return nullptr;
 
   CXFA_FFDocView* pDocView =
@@ -77,8 +78,9 @@
 }
 
 CXFA_FFWidget* CPDFSDK_Widget::GetGroupMixXFAWidget() const {
-  auto* pContext = m_pPageView->GetFormFillEnv()->GetDocExtension();
-  if (!pContext->ContainsExtensionForegroundForm())
+  CPDF_Document::Extension* pContext =
+      m_pPageView->GetFormFillEnv()->GetDocExtension();
+  if (!pContext || !pContext->ContainsExtensionForegroundForm())
     return nullptr;
 
   CXFA_FFDocView* pDocView =
@@ -91,8 +93,9 @@
 }
 
 CXFA_FFWidgetHandler* CPDFSDK_Widget::GetXFAWidgetHandler() const {
-  auto* pContext = m_pPageView->GetFormFillEnv()->GetDocExtension();
-  if (!pContext->ContainsExtensionForegroundForm())
+  CPDF_Document::Extension* pContext =
+      m_pPageView->GetFormFillEnv()->GetDocExtension();
+  if (!pContext || !pContext->ContainsExtensionForegroundForm())
     return nullptr;
 
   if (!m_pWidgetHandler) {
@@ -208,6 +211,8 @@
                                   CPDFSDK_PageView* pPageView) {
   auto* pContext = static_cast<CPDFXFA_Context*>(
       m_pPageView->GetFormFillEnv()->GetDocExtension());
+  if (!pContext)
+    return false;
 
   ObservedPtr<CXFA_FFWidget> pWidget(GetMixXFAWidget());
   if (!pWidget)
@@ -345,8 +350,9 @@
 
 bool CPDFSDK_Widget::IsAppearanceValid() {
 #ifdef PDF_ENABLE_XFA
-  auto* pContext = m_pPageView->GetFormFillEnv()->GetDocExtension();
-  if (pContext->ContainsExtensionFullForm())
+  CPDF_Document::Extension* pContext =
+      m_pPageView->GetFormFillEnv()->GetDocExtension();
+  if (pContext && pContext->ContainsExtensionFullForm())
     return true;
 #endif  // PDF_ENABLE_XFA
   return CPDFSDK_BAAnnot::IsAppearanceValid();
@@ -792,27 +798,30 @@
 #ifdef PDF_ENABLE_XFA
   auto* pContext =
       static_cast<CPDFXFA_Context*>(pFormFillEnv->GetDocExtension());
-  if (CXFA_FFWidget* hWidget = GetMixXFAWidget()) {
-    XFA_EVENTTYPE eEventType = GetXFAEventType(type, data->bWillCommit);
-
-    if (eEventType != XFA_EVENT_Unknown) {
-      if (CXFA_FFWidgetHandler* pXFAWidgetHandler = GetXFAWidgetHandler()) {
-        CXFA_EventParam param;
-        param.m_eType = eEventType;
-        param.m_wsChange = data->sChange;
-        param.m_iCommitKey = 0;
-        param.m_bShift = data->bShift;
-        param.m_iSelStart = data->nSelStart;
-        param.m_iSelEnd = data->nSelEnd;
-        param.m_wsFullText = data->sValue;
-        param.m_bKeyDown = data->bKeyDown;
-        param.m_bModifier = data->bModifier;
-        param.m_wsPrevText = data->sValue;
-        bool ret = hWidget->ProcessEventUnderHandler(&param, pXFAWidgetHandler);
-        if (CXFA_FFDocView* pDocView = pContext->GetXFADocView())
-          pDocView->UpdateDocView();
-        if (ret)
-          return true;
+  if (pContext) {
+    CXFA_FFWidget* hWidget = GetMixXFAWidget();
+    if (hWidget) {
+      XFA_EVENTTYPE eEventType = GetXFAEventType(type, data->bWillCommit);
+      if (eEventType != XFA_EVENT_Unknown) {
+        if (CXFA_FFWidgetHandler* pXFAWidgetHandler = GetXFAWidgetHandler()) {
+          CXFA_EventParam param;
+          param.m_eType = eEventType;
+          param.m_wsChange = data->sChange;
+          param.m_iCommitKey = 0;
+          param.m_bShift = data->bShift;
+          param.m_iSelStart = data->nSelStart;
+          param.m_iSelEnd = data->nSelEnd;
+          param.m_wsFullText = data->sValue;
+          param.m_bKeyDown = data->bKeyDown;
+          param.m_bModifier = data->bModifier;
+          param.m_wsPrevText = data->sValue;
+          bool ret =
+              hWidget->ProcessEventUnderHandler(&param, pXFAWidgetHandler);
+          if (CXFA_FFDocView* pDocView = pContext->GetXFADocView())
+            pDocView->UpdateDocView();
+          if (ret)
+            return true;
+        }
       }
     }
   }
diff --git a/fpdfsdk/cpdfsdk_widgethandler.cpp b/fpdfsdk/cpdfsdk_widgethandler.cpp
index 1e4462d..a3b2394 100644
--- a/fpdfsdk/cpdfsdk_widgethandler.cpp
+++ b/fpdfsdk/cpdfsdk_widgethandler.cpp
@@ -211,7 +211,7 @@
 #ifdef PDF_ENABLE_XFA
   CPDFSDK_PageView* pPageView = pAnnot->GetPageView();
   auto* pContext = pPageView->GetFormFillEnv()->GetDocExtension();
-  if (pContext->ContainsExtensionForegroundForm()) {
+  if (pContext && pContext->ContainsExtensionForegroundForm()) {
     if (!pWidget->IsAppearanceValid() && !pWidget->GetValue().IsEmpty())
       pWidget->ResetXFAAppearance(false);
   }