From b8db5115e37898a2e399714db062707e8cc0a021 Mon Sep 17 00:00:00 2001 From: thestig Date: Wed, 6 Apr 2016 12:12:52 -0700 Subject: FPDF_GetSecurityHandlerRevision() should not crash. Same for FPDF_GetDocPermissions(). Empty documents do not have parsers. BUG=pdfium:465 Review URL: https://codereview.chromium.org/1865523003 --- fpdfsdk/fpdfview.cpp | 4 ++-- fpdfsdk/fpdfview_embeddertest.cpp | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/fpdfsdk/fpdfview.cpp b/fpdfsdk/fpdfview.cpp index df7518902f..3f02feeadf 100644 --- a/fpdfsdk/fpdfview.cpp +++ b/fpdfsdk/fpdfview.cpp @@ -463,7 +463,7 @@ DLLEXPORT FPDF_BOOL STDCALL FPDF_GetFileVersion(FPDF_DOCUMENT doc, // header). DLLEXPORT unsigned long STDCALL FPDF_GetDocPermissions(FPDF_DOCUMENT document) { CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); - if (!pDoc) + if (!pDoc || !pDoc->GetParser()) #ifndef PDF_ENABLE_XFA return 0; #else // PDF_ENABLE_XFA @@ -476,7 +476,7 @@ DLLEXPORT unsigned long STDCALL FPDF_GetDocPermissions(FPDF_DOCUMENT document) { DLLEXPORT int STDCALL FPDF_GetSecurityHandlerRevision(FPDF_DOCUMENT document) { CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); - if (!pDoc) + if (!pDoc || !pDoc->GetParser()) return -1; CPDF_Dictionary* pDict = pDoc->GetParser()->GetEncryptDict(); diff --git a/fpdfsdk/fpdfview_embeddertest.cpp b/fpdfsdk/fpdfview_embeddertest.cpp index 10fe3aa822..2ccbfd00d3 100644 --- a/fpdfsdk/fpdfview_embeddertest.cpp +++ b/fpdfsdk/fpdfview_embeddertest.cpp @@ -29,6 +29,36 @@ TEST_F(FPDFViewEmbeddertest, Document) { EXPECT_EQ(-1, FPDF_GetSecurityHandlerRevision(document())); } +// See bug 465. +TEST_F(FPDFViewEmbeddertest, EmptyDocument) { + EXPECT_TRUE(CreateEmptyDocument()); + + { + int version = 42; + EXPECT_FALSE(FPDF_GetFileVersion(document(), &version)); + EXPECT_EQ(0, version); + } + + { +#ifndef PDF_ENABLE_XFA + const unsigned long kExpected = 0; +#else + const unsigned long kExpected = static_cast(-1); +#endif + EXPECT_EQ(kExpected, FPDF_GetDocPermissions(document())); + } + + EXPECT_EQ(-1, FPDF_GetSecurityHandlerRevision(document())); + + EXPECT_EQ(0, FPDF_GetPageCount(document())); + + EXPECT_TRUE(FPDF_VIEWERREF_GetPrintScaling(document())); + EXPECT_EQ(1, FPDF_VIEWERREF_GetNumCopies(document())); + EXPECT_EQ(DuplexUndefined, FPDF_VIEWERREF_GetDuplex(document())); + + EXPECT_EQ(0, FPDF_CountNamedDests(document())); +} + TEST_F(FPDFViewEmbeddertest, Page) { EXPECT_TRUE(OpenDocument("about_blank.pdf")); FPDF_PAGE page = LoadPage(0); -- cgit v1.2.3