From 481749905d444745ee0ba76d9bcf42b3b009bb27 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Mon, 16 Jul 2018 16:54:02 +0000 Subject: Get rid of some loose allocs/free in CPDF_Document. Use std::vector<> as a manager for contiguous buffers. Change-Id: Icaacbd4b7010b928237aa71485411ade7539412a Reviewed-on: https://pdfium-review.googlesource.com/37012 Reviewed-by: dsinclair Commit-Queue: Tom Sepez --- core/fpdfapi/parser/cpdf_document.cpp | 39 ++++++++++++++++------------------- 1 file changed, 18 insertions(+), 21 deletions(-) (limited to 'core/fpdfapi/parser') diff --git a/core/fpdfapi/parser/cpdf_document.cpp b/core/fpdfapi/parser/cpdf_document.cpp index 4f699139a3..311e017148 100644 --- a/core/fpdfapi/parser/cpdf_document.cpp +++ b/core/fpdfapi/parser/cpdf_document.cpp @@ -39,30 +39,29 @@ namespace { const int FX_MAX_PAGE_LEVEL = 1024; -void InsertWidthArrayImpl(int* widths, int size, CPDF_Array* pWidthArray) { - int i; - for (i = 1; i < size; i++) { - if (widths[i] != *widths) +void InsertWidthArrayImpl(std::vector widths, CPDF_Array* pWidthArray) { + size_t i; + for (i = 1; i < widths.size(); i++) { + if (widths[i] != widths[0]) break; } - if (i == size) { + if (i == widths.size()) { int first = pWidthArray->GetIntegerAt(pWidthArray->GetCount() - 1); - pWidthArray->AddNew(first + size - 1); - pWidthArray->AddNew(*widths); - } else { - CPDF_Array* pWidthArray1 = pWidthArray->AddNew(); - for (i = 0; i < size; i++) - pWidthArray1->AddNew(widths[i]); + pWidthArray->AddNew(first + static_cast(widths.size()) - + 1); + pWidthArray->AddNew(widths[0]); + return; } - FX_Free(widths); + CPDF_Array* pWidthArray1 = pWidthArray->AddNew(); + for (int w : widths) + pWidthArray1->AddNew(w); } #if _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ void InsertWidthArray(HDC hDC, int start, int end, CPDF_Array* pWidthArray) { - int size = end - start + 1; - int* widths = FX_Alloc(int, size); - GetCharWidth(hDC, start, end, widths); - InsertWidthArrayImpl(widths, size, pWidthArray); + std::vector widths(end - start + 1); + GetCharWidth(hDC, start, end, widths.data()); + InsertWidthArrayImpl(std::move(widths), pWidthArray); } ByteString FPDF_GetPSNameFromTT(HDC hDC) { @@ -83,14 +82,12 @@ void InsertWidthArray1(CFX_Font* pFont, wchar_t start, wchar_t end, CPDF_Array* pWidthArray) { - int size = end - start + 1; - int* widths = FX_Alloc(int, size); - int i; - for (i = 0; i < size; i++) { + std::vector widths(end - start + 1); + for (size_t i = 0; i < widths.size(); ++i) { int glyph_index = pEncoding->GlyphFromCharCode(start + i); widths[i] = pFont->GetGlyphWidth(glyph_index); } - InsertWidthArrayImpl(widths, size, pWidthArray); + InsertWidthArrayImpl(std::move(widths), pWidthArray); } int CountPages(CPDF_Dictionary* pPages, -- cgit v1.2.3