From d08667c174fa302d7dd05ab0b33d61b91fcda63c Mon Sep 17 00:00:00 2001 From: Nicolas Pena Date: Mon, 16 Apr 2018 17:41:47 +0000 Subject: Check that CFX_DIBitmap::Create calls succeeds in fpdf_view MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CFX_DIBitmap::Create returns false upon failure, so the return value of this method should be checked most of the time. This CL fixes the calls from fpdf_view.cpp. Bug: pdfium:983 Change-Id: I5782d2859c5b2a2e0c540b80510d7c98c01c04f3 Reviewed-on: https://pdfium-review.googlesource.com/30750 Commit-Queue: Nicolás Peña Moreno Reviewed-by: dsinclair --- fpdfsdk/fpdf_view.cpp | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/fpdfsdk/fpdf_view.cpp b/fpdfsdk/fpdf_view.cpp index cd8edbbdd2..d21448bc53 100644 --- a/fpdfsdk/fpdf_view.cpp +++ b/fpdfsdk/fpdf_view.cpp @@ -460,7 +460,9 @@ RetainPtr GetMaskBitmap(CPDF_Page* pPage, // Create a new bitmap to transfer part of the page bitmap to. RetainPtr pDst = pdfium::MakeRetain(); - pDst->Create(bitmap_area->Width(), bitmap_area->Height(), FXDIB_Argb); + if (!pDst->Create(bitmap_area->Width(), bitmap_area->Height(), FXDIB_Argb)) + return nullptr; + pDst->Clear(0x00ffffff); pDst->TransferBitmap(0, 0, bitmap_area->Width(), bitmap_area->Height(), pSrc, bitmap_area->left, bitmap_area->top); @@ -477,7 +479,9 @@ void RenderBitmap(CFX_RenderDevice* device, // Create a new bitmap from the old one RetainPtr pDst = pdfium::MakeRetain(); - pDst->Create(size_x_bm, size_y_bm, FXDIB_Rgb32); + if (!pDst->Create(size_x_bm, size_y_bm, FXDIB_Rgb32)) + return; + pDst->Clear(0xffffffff); pDst->CompositeBitmap(0, 0, size_x_bm, size_y_bm, pSrc, 0, 0, FXDIB_BLEND_NORMAL, nullptr, false); @@ -520,6 +524,8 @@ FPDF_EXPORT void FPDF_CALLCONV FPDF_RenderPage(HDC dc, const bool bHasMask = pPage->HasImageMask() && !bNewBitmap; if (bNewBitmap || bHasMask) { pBitmap = pdfium::MakeRetain(); + // Create will probably work fine even if it fails here: we will just attach + // a zero-sized bitmap to |pDevice|. pBitmap->Create(size_x, size_y, FXDIB_Argb); pBitmap->Clear(0x00ffffff); CFX_DefaultRenderDevice* pDevice = new CFX_DefaultRenderDevice; @@ -580,17 +586,19 @@ FPDF_EXPORT void FPDF_CALLCONV FPDF_RenderPage(HDC dc, } } else if (bNewBitmap) { CFX_WindowsRenderDevice WinDC(dc); + bool bitsStretched = false; if (WinDC.GetDeviceCaps(FXDC_DEVICE_CLASS) == FXDC_PRINTER) { auto pDst = pdfium::MakeRetain(); - int pitch = pBitmap->GetPitch(); - pDst->Create(size_x, size_y, FXDIB_Rgb32); - memset(pDst->GetBuffer(), -1, pitch * size_y); - pDst->CompositeBitmap(0, 0, size_x, size_y, pBitmap, 0, 0, - FXDIB_BLEND_NORMAL, nullptr, false); - WinDC.StretchDIBits(pDst, 0, 0, size_x, size_y); - } else { - WinDC.SetDIBits(pBitmap, 0, 0); + if (pDst->Create(size_x, size_y, FXDIB_Rgb32)) { + memset(pDst->GetBuffer(), -1, pBitmap->GetPitch() * size_y); + pDst->CompositeBitmap(0, 0, size_x, size_y, pBitmap, 0, 0, + FXDIB_BLEND_NORMAL, nullptr, false); + WinDC.StretchDIBits(pDst, 0, 0, size_x, size_y); + bitsStretched = true; + } } + if (!bitsStretched) + WinDC.SetDIBits(pBitmap, 0, 0); } pPage->SetRenderContext(nullptr); @@ -814,8 +822,10 @@ FPDF_EXPORT FPDF_BITMAP FPDF_CALLCONV FPDFBitmap_CreateEx(int width, return nullptr; } auto pBitmap = pdfium::MakeRetain(); - pBitmap->Create(width, height, fx_format, static_cast(first_scan), - stride); + if (!pBitmap->Create(width, height, fx_format, + static_cast(first_scan), stride)) { + return nullptr; + } return pBitmap.Leak(); } -- cgit v1.2.3