From 47fb8c06acd0ff9ea50c8c2d7f67510ea5c28577 Mon Sep 17 00:00:00 2001 From: tsepez Date: Thu, 15 Dec 2016 13:51:34 -0800 Subject: Return unique_ptr from CFX_BinaryBuf::DetachBuffer() In turn, make CPDF_Stream() take an unique_ptr. Review-Url: https://codereview.chromium.org/2584683002 --- core/fpdfapi/page/cpdf_image.cpp | 17 +++++++++-------- core/fpdfapi/page/cpdf_streamparser.cpp | 18 ++++++++++-------- 2 files changed, 19 insertions(+), 16 deletions(-) (limited to 'core/fpdfapi/page') diff --git a/core/fpdfapi/page/cpdf_image.cpp b/core/fpdfapi/page/cpdf_image.cpp index 97969551c6..a0109f22a8 100644 --- a/core/fpdfapi/page/cpdf_image.cpp +++ b/core/fpdfapi/page/cpdf_image.cpp @@ -219,8 +219,9 @@ void CPDF_Image::SetImage(const CFX_DIBitmap* pBitmap) { pCS->AddNew("Indexed"); pCS->AddNew("DeviceRGB"); pCS->AddNew(iPalette - 1); - uint8_t* pColorTable = FX_Alloc2D(uint8_t, iPalette, 3); - uint8_t* ptr = pColorTable; + std::unique_ptr pColorTable( + FX_Alloc2D(uint8_t, iPalette, 3)); + uint8_t* ptr = pColorTable.get(); for (int32_t i = 0; i < iPalette; i++) { uint32_t argb = pBitmap->GetPaletteArgb(i); ptr[0] = (uint8_t)(argb >> 16); @@ -231,7 +232,7 @@ void CPDF_Image::SetImage(const CFX_DIBitmap* pBitmap) { auto pNewDict = pdfium::MakeUnique(m_pDocument->GetByteStringPool()); CPDF_Stream* pCTS = m_pDocument->NewIndirect( - pColorTable, iPalette * 3, std::move(pNewDict)); + std::move(pColorTable), iPalette * 3, std::move(pNewDict)); pCS->AddNew(m_pDocument, pCTS->GetObjNum()); pDict->SetNewFor("ColorSpace", m_pDocument, pCS->GetObjNum()); @@ -254,7 +255,7 @@ void CPDF_Image::SetImage(const CFX_DIBitmap* pBitmap) { if (pMaskBitmap) { int32_t maskWidth = pMaskBitmap->GetWidth(); int32_t maskHeight = pMaskBitmap->GetHeight(); - uint8_t* mask_buf = nullptr; + std::unique_ptr mask_buf; FX_STRSIZE mask_size = 0; auto pMaskDict = pdfium::MakeUnique(m_pDocument->GetByteStringPool()); @@ -265,16 +266,16 @@ void CPDF_Image::SetImage(const CFX_DIBitmap* pBitmap) { pMaskDict->SetNewFor("ColorSpace", "DeviceGray"); pMaskDict->SetNewFor("BitsPerComponent", 8); if (pMaskBitmap->GetFormat() != FXDIB_1bppMask) { - mask_buf = FX_Alloc2D(uint8_t, maskHeight, maskWidth); + mask_buf.reset(FX_Alloc2D(uint8_t, maskHeight, maskWidth)); mask_size = maskHeight * maskWidth; // Safe since checked alloc returned. for (int32_t a = 0; a < maskHeight; a++) { - FXSYS_memcpy(mask_buf + a * maskWidth, pMaskBitmap->GetScanline(a), - maskWidth); + FXSYS_memcpy(mask_buf.get() + a * maskWidth, + pMaskBitmap->GetScanline(a), maskWidth); } } pMaskDict->SetNewFor("Length", mask_size); CPDF_Stream* pNewStream = m_pDocument->NewIndirect( - mask_buf, mask_size, std::move(pMaskDict)); + std::move(mask_buf), mask_size, std::move(pMaskDict)); pDict->SetNewFor("SMask", m_pDocument, pNewStream->GetObjNum()); } diff --git a/core/fpdfapi/page/cpdf_streamparser.cpp b/core/fpdfapi/page/cpdf_streamparser.cpp index fd5267b111..cf87ce9954 100644 --- a/core/fpdfapi/page/cpdf_streamparser.cpp +++ b/core/fpdfapi/page/cpdf_streamparser.cpp @@ -178,21 +178,22 @@ std::unique_ptr CPDF_StreamParser::ReadInlineStream( return nullptr; OrigSize *= height; - uint8_t* pData = nullptr; + std::unique_ptr pData; uint32_t dwStreamSize; if (Decoder.IsEmpty()) { if (OrigSize > m_Size - m_Pos) OrigSize = m_Size - m_Pos; - pData = FX_Alloc(uint8_t, OrigSize); - FXSYS_memcpy(pData, m_pBuf + m_Pos, OrigSize); + pData.reset(FX_Alloc(uint8_t, OrigSize)); + FXSYS_memcpy(pData.get(), m_pBuf + m_Pos, OrigSize); dwStreamSize = OrigSize; m_Pos += OrigSize; } else { + uint8_t* pIgnore; uint32_t dwDestSize = OrigSize; dwStreamSize = PDF_DecodeInlineStream(m_pBuf + m_Pos, m_Size - m_Pos, width, height, - Decoder, pParam, pData, dwDestSize); - FX_Free(pData); + Decoder, pParam, pIgnore, dwDestSize); + FX_Free(pIgnore); if (static_cast(dwStreamSize) < 0) return nullptr; @@ -216,12 +217,13 @@ std::unique_ptr CPDF_StreamParser::ReadInlineStream( dwStreamSize += m_Pos - dwPrevPos; } m_Pos = dwSavePos; - pData = FX_Alloc(uint8_t, dwStreamSize); - FXSYS_memcpy(pData, m_pBuf + m_Pos, dwStreamSize); + pData.reset(FX_Alloc(uint8_t, dwStreamSize)); + FXSYS_memcpy(pData.get(), m_pBuf + m_Pos, dwStreamSize); m_Pos += dwStreamSize; } pDict->SetNewFor("Length", (int)dwStreamSize); - return pdfium::MakeUnique(pData, dwStreamSize, std::move(pDict)); + return pdfium::MakeUnique(std::move(pData), dwStreamSize, + std::move(pDict)); } CPDF_StreamParser::SyntaxType CPDF_StreamParser::ParseNextElement() { -- cgit v1.2.3