From fe91c6c8211cec39f871d9202556e1957bf81983 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Tue, 16 May 2017 15:33:20 -0700 Subject: Be skeptical of bare |new|s. In particular, prefer an explicit .release() call when handing ownership of an object to a caller across a C-API. Change-Id: Ic3784e9d0b2d378a08d388989eaea7c9166bacd1 Reviewed-on: https://pdfium-review.googlesource.com/5470 Commit-Queue: Tom Sepez Reviewed-by: Lei Zhang --- fpdfsdk/fpdfeditpage.cpp | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) (limited to 'fpdfsdk/fpdfeditpage.cpp') diff --git a/fpdfsdk/fpdfeditpage.cpp b/fpdfsdk/fpdfeditpage.cpp index 66ff028141..df752d4029 100644 --- a/fpdfsdk/fpdfeditpage.cpp +++ b/fpdfsdk/fpdfeditpage.cpp @@ -63,14 +63,13 @@ bool IsPageObject(CPDF_Page* pPage) { } // namespace DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_CreateNewDocument() { - CPDF_Document* pDoc = new CPDF_Document(nullptr); + auto pDoc = pdfium::MakeUnique(nullptr); pDoc->CreateNewDoc(); - time_t currentTime; + time_t currentTime; CFX_ByteString DateStr; - if (FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS)) { - if (-1 != time(¤tTime)) { + if (time(¤tTime) != -1) { tm* pTM = localtime(¤tTime); if (pTM) { DateStr.Format("D:%04d%02d%02d%02d%02d%02d", pTM->tm_year + 1900, @@ -80,15 +79,15 @@ DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_CreateNewDocument() { } } - CPDF_Dictionary* pInfoDict = nullptr; - pInfoDict = pDoc->GetInfo(); + CPDF_Dictionary* pInfoDict = pDoc->GetInfo(); if (pInfoDict) { if (FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS)) pInfoDict->SetNewFor("CreationDate", DateStr, false); pInfoDict->SetNewFor("Creator", L"PDFium"); } - return FPDFDocumentFromCPDFDocument(pDoc); + // Caller takes ownership of pDoc. + return FPDFDocumentFromCPDFDocument(pDoc.release()); } DLLEXPORT void STDCALL FPDFPage_Delete(FPDF_DOCUMENT document, int page_index) { @@ -121,19 +120,17 @@ DLLEXPORT FPDF_PAGE STDCALL FPDFPage_New(FPDF_DOCUMENT document, auto pXFAPage = pdfium::MakeRetain( static_cast(document), page_index); pXFAPage->LoadPDFPage(pPageDict); - return pXFAPage.Leak(); + return pXFAPage.Leak(); // Caller takes ownership. #else // PDF_ENABLE_XFA - CPDF_Page* pPage = new CPDF_Page(pDoc, pPageDict, true); + auto pPage = pdfium::MakeUnique(pDoc, pPageDict, true); pPage->ParseContent(); - return pPage; + return pPage.release(); // Caller takes ownership. #endif // PDF_ENABLE_XFA } DLLEXPORT int STDCALL FPDFPage_GetRotation(FPDF_PAGE page) { CPDF_Page* pPage = CPDFPageFromFPDFPage(page); - if (!IsPageObject(pPage)) - return -1; - return pPage->GetPageRotation(); + return IsPageObject(pPage) ? pPage->GetPageRotation() : -1; } DLLEXPORT void STDCALL FPDFPage_InsertObject(FPDF_PAGE page, -- cgit v1.2.3