From 3f3c39d04fd68d8ce11f52baa3acae8e0522a2c4 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Tue, 1 May 2018 17:46:34 +0000 Subject: Check for NULL XFA context even when XFA Use strict typing for FPDF_Page to ensure we don't fall into code that expects the other page type when continuing from null context case. Change-Id: I7f028ef3e3d733f5557620030a87e22997da00d5 Reviewed-on: https://pdfium-review.googlesource.com/31770 Commit-Queue: Tom Sepez Reviewed-by: dsinclair --- fpdfsdk/fpdf_view.cpp | 58 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 22 deletions(-) (limited to 'fpdfsdk/fpdf_view.cpp') diff --git a/fpdfsdk/fpdf_view.cpp b/fpdfsdk/fpdf_view.cpp index 00c59bbda5..1eb014a427 100644 --- a/fpdfsdk/fpdf_view.cpp +++ b/fpdfsdk/fpdf_view.cpp @@ -257,8 +257,11 @@ FPDF_EXPORT int FPDF_CALLCONV FPDF_GetFormType(FPDF_DOCUMENT document) { #ifdef PDF_ENABLE_XFA FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDF_LoadXFA(FPDF_DOCUMENT document) { auto* pDoc = CPDFDocumentFromFPDFDocument(document); - return pDoc && - static_cast(pDoc->GetExtension())->LoadXFADoc(); + if (!pDoc) + return false; + + auto* pContext = static_cast(pDoc->GetExtension()); + return pContext && pContext->LoadXFADoc(); } #endif // PDF_ENABLE_XFA @@ -325,12 +328,14 @@ FPDF_EXPORT int FPDF_CALLCONV FPDF_GetPageCount(FPDF_DOCUMENT document) { auto* pDoc = CPDFDocumentFromFPDFDocument(document); if (!pDoc) return 0; + #ifdef PDF_ENABLE_XFA auto* pContext = static_cast(pDoc->GetExtension()); - return pContext ? pContext->GetPageCount() : 0; -#else + if (pContext) + return pContext->GetPageCount(); +#endif // PDF_ENABLE_XFA + return pDoc->GetPageCount(); -#endif } FPDF_EXPORT FPDF_PAGE FPDF_CALLCONV FPDF_LoadPage(FPDF_DOCUMENT document, @@ -343,9 +348,12 @@ FPDF_EXPORT FPDF_PAGE FPDF_CALLCONV FPDF_LoadPage(FPDF_DOCUMENT document, return nullptr; #ifdef PDF_ENABLE_XFA - return static_cast(pDoc->GetExtension()) - ->GetXFAPage(page_index) - .Leak(); + auto* pContext = static_cast(pDoc->GetExtension()); + if (pContext) + return FPDFPageFromUnderlying(pContext->GetXFAPage(page_index).Leak()); + + // Eventually, fallthrough into non-xfa case once page type made consistent. + return nullptr; #else // PDF_ENABLE_XFA CPDF_Dictionary* pDict = pDoc->GetPage(page_index); if (!pDict) @@ -353,7 +361,7 @@ FPDF_EXPORT FPDF_PAGE FPDF_CALLCONV FPDF_LoadPage(FPDF_DOCUMENT document, CPDF_Page* pPage = new CPDF_Page(pDoc, pDict, true); pPage->ParseContent(); - return pPage; + return FPDFPageFromUnderlying(pPage); #endif // PDF_ENABLE_XFA } @@ -752,13 +760,16 @@ FPDF_EXPORT void FPDF_CALLCONV FPDF_ClosePage(FPDF_PAGE page) { FPDF_EXPORT void FPDF_CALLCONV FPDF_CloseDocument(FPDF_DOCUMENT document) { auto* pDoc = CPDFDocumentFromFPDFDocument(document); + #if PDF_ENABLE_XFA // Deleting the extension will delete the document - if (pDoc) + if (pDoc && pDoc->GetExtension()) { delete pDoc->GetExtension(); -#else + return; + } +#endif // PDF_ENABLE_XFA + delete pDoc; -#endif } FPDF_EXPORT unsigned long FPDF_CALLCONV FPDF_GetLastError() { @@ -943,14 +954,19 @@ FPDF_EXPORT int FPDF_CALLCONV FPDF_GetPageSizeByIndex(FPDF_DOCUMENT document, #ifdef PDF_ENABLE_XFA if (page_index < 0 || page_index >= FPDF_GetPageCount(document)) return false; - RetainPtr pPage = - static_cast(pDoc->GetExtension()) - ->GetXFAPage(page_index); - if (!pPage) - return false; - *width = pPage->GetPageWidth(); - *height = pPage->GetPageHeight(); -#else // PDF_ENABLE_XFA + + auto* pContext = static_cast(pDoc->GetExtension()); + if (pContext) { + RetainPtr pPage = pContext->GetXFAPage(page_index); + if (!pPage) + return false; + + *width = pPage->GetPageWidth(); + *height = pPage->GetPageHeight(); + return true; + } +#endif // PDF_ENABLE_XFA + CPDF_Dictionary* pDict = pDoc->GetPage(page_index); if (!pDict) return false; @@ -958,8 +974,6 @@ FPDF_EXPORT int FPDF_CALLCONV FPDF_GetPageSizeByIndex(FPDF_DOCUMENT document, CPDF_Page page(pDoc, pDict, true); *width = page.GetPageWidth(); *height = page.GetPageHeight(); -#endif // PDF_ENABLE_XFA - return true; } -- cgit v1.2.3