From 085c97c986336bc622d6b3d72e6a587e1ae4b844 Mon Sep 17 00:00:00 2001 From: Noel Grandin Date: Wed, 26 May 2021 10:42:18 +0200 Subject: fix leak in SfxAutoRedactDialog Change-Id: I5ebfe5eba77de3380e991ced26f396821613b7f5 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/116189 Tested-by: Jenkins Reviewed-by: Noel Grandin --- sfx2/inc/SfxRedactionHelper.hxx | 8 ++++---- sfx2/inc/autoredactdialog.hxx | 2 +- sfx2/source/doc/SfxRedactionHelper.cxx | 37 ++++++++++++++-------------------- sfx2/source/doc/autoredactdialog.cxx | 5 +++-- sfx2/source/doc/objserv.cxx | 2 +- 5 files changed, 24 insertions(+), 30 deletions(-) diff --git a/sfx2/inc/SfxRedactionHelper.hxx b/sfx2/inc/SfxRedactionHelper.hxx index c6631a053566..07e752d173ad 100644 --- a/sfx2/inc/SfxRedactionHelper.hxx +++ b/sfx2/inc/SfxRedactionHelper.hxx @@ -76,7 +76,7 @@ public: addPagesToDraw(const uno::Reference& xComponent, sal_Int32 nPages, const std::vector& aMetaFiles, const std::vector<::Size>& aPageSizes, const PageMargins& aPageMargins, - const std::vector>& r_aTableTargets, + const std::vector>& r_aTableTargets, bool bIsAutoRedact); /* * Makes the Redaction toolbar visible to the user. @@ -102,7 +102,7 @@ public: * Used to find the text portions to be redacted. Returns a list of rectangles to cover those * areas to be redacted. Probably the most crucial part of the auto-redaction process. * */ - static void searchInMetaFile(const RedactionTarget* pRedactionTarget, const GDIMetaFile& rMtf, + static void searchInMetaFile(const RedactionTarget& rRedactionTarget, const GDIMetaFile& rMtf, std::vector& aRedactionRectangles, const uno::Reference& xComponent); @@ -117,14 +117,14 @@ public: * Search for the given term through the gdimetafile, which has the whole content of a draw page, * and draw redaction rectangles to the appropriate positions with suitable sizes. * */ - static void autoRedactPage(const RedactionTarget* pRedactionTarget, + static void autoRedactPage(const RedactionTarget& rRedactionTarget, const GDIMetaFile& rGDIMetaFile, const uno::Reference& xPage, const uno::Reference& xComponent); /// Fill the search options based on the given redaction target static void fillSearchOptions(i18nutil::SearchOptions2& rSearchOpt, - const RedactionTarget* pTarget); + const RedactionTarget& rTarget); private: static constexpr std::u16string_view m_aPredefinedTargets[6] = { diff --git a/sfx2/inc/autoredactdialog.hxx b/sfx2/inc/autoredactdialog.hxx index eef638538e80..657881979df2 100644 --- a/sfx2/inc/autoredactdialog.hxx +++ b/sfx2/inc/autoredactdialog.hxx @@ -132,7 +132,7 @@ public: * Does a shallow copy. * Returns true if successful. */ - bool getTargets(std::vector>& r_aTargets); + bool getTargets(std::vector>& r_aTargets); }; class SfxAddTargetDialog final : public weld::GenericDialogController diff --git a/sfx2/source/doc/SfxRedactionHelper.cxx b/sfx2/source/doc/SfxRedactionHelper.cxx index 98f5d9b37c6b..ca82fc484767 100644 --- a/sfx2/source/doc/SfxRedactionHelper.cxx +++ b/sfx2/source/doc/SfxRedactionHelper.cxx @@ -216,7 +216,7 @@ void SfxRedactionHelper::addPagesToDraw( const uno::Reference& xComponent, sal_Int32 nPages, const std::vector& aMetaFiles, const std::vector<::Size>& aPageSizes, const PageMargins& aPageMargins, - const std::vector>& r_aTableTargets, bool bIsAutoRedact) + const std::vector>& r_aTableTargets, bool bIsAutoRedact) { // Access the draw pages uno::Reference xDrawPagesSupplier(xComponent, uno::UNO_QUERY); @@ -413,14 +413,14 @@ SfxRedactionHelper::getPageMarginsForCalc(const css::uno::Reference& aRedactionRectangles, const uno::Reference& xComponent) { // Initialize search i18nutil::SearchOptions2 aSearchOptions; - fillSearchOptions(aSearchOptions, pRedactionTarget); + fillSearchOptions(aSearchOptions, rRedactionTarget); utl::TextSearch textSearch(aSearchOptions); static tools::Long aLastFontHeight = 0; @@ -508,17 +508,17 @@ void SfxRedactionHelper::addRedactionRectToPage( } } -void SfxRedactionHelper::autoRedactPage(const RedactionTarget* pRedactionTarget, +void SfxRedactionHelper::autoRedactPage(const RedactionTarget& rRedactionTarget, const GDIMetaFile& rGDIMetaFile, const uno::Reference& xPage, const uno::Reference& xComponent) { - if (pRedactionTarget == nullptr || pRedactionTarget->sContent.isEmpty()) + if (rRedactionTarget.sContent.isEmpty()) return; // Search for the redaction strings, and get the rectangle coordinates std::vector<::tools::Rectangle> aRedactionRectangles; - searchInMetaFile(pRedactionTarget, rGDIMetaFile, aRedactionRectangles, xComponent); + searchInMetaFile(rRedactionTarget, rGDIMetaFile, aRedactionRectangles, xComponent); // Add the redaction rectangles to the page addRedactionRectToPage(xComponent, xPage, aRedactionRectangles); @@ -530,17 +530,10 @@ const LanguageTag& GetAppLanguageTag() { return Application::GetSettings().GetLa } void SfxRedactionHelper::fillSearchOptions(i18nutil::SearchOptions2& rSearchOpt, - const RedactionTarget* pTarget) + const RedactionTarget& rTarget) { - if (!pTarget) - { - SAL_WARN("sfx.doc", - "pTarget (pointer to Redactiontarget) is null. This should never happen."); - return; - } - - if (pTarget->sType == RedactionTargetType::REDACTION_TARGET_REGEX - || pTarget->sType == RedactionTargetType::REDACTION_TARGET_PREDEFINED) + if (rTarget.sType == RedactionTargetType::REDACTION_TARGET_REGEX + || rTarget.sType == RedactionTargetType::REDACTION_TARGET_PREDEFINED) { rSearchOpt.algorithmType = util::SearchAlgorithms_REGEXP; rSearchOpt.AlgorithmType2 = util::SearchAlgorithms2::REGEXP; @@ -552,20 +545,20 @@ void SfxRedactionHelper::fillSearchOptions(i18nutil::SearchOptions2& rSearchOpt, } rSearchOpt.Locale = GetAppLanguageTag().getLocale(); - if (pTarget->sType == RedactionTargetType::REDACTION_TARGET_PREDEFINED) + if (rTarget.sType == RedactionTargetType::REDACTION_TARGET_PREDEFINED) { - auto nPredefIndex = pTarget->sContent.getToken(0, ';').toUInt32(); + auto nPredefIndex = rTarget.sContent.getToken(0, ';').toUInt32(); rSearchOpt.searchString = m_aPredefinedTargets[nPredefIndex]; } else - rSearchOpt.searchString = pTarget->sContent; + rSearchOpt.searchString = rTarget.sContent; rSearchOpt.replaceString.clear(); - if (!pTarget->bCaseSensitive && pTarget->sType != RedactionTargetType::REDACTION_TARGET_REGEX - && pTarget->sType != RedactionTargetType::REDACTION_TARGET_PREDEFINED) + if (!rTarget.bCaseSensitive && rTarget.sType != RedactionTargetType::REDACTION_TARGET_REGEX + && rTarget.sType != RedactionTargetType::REDACTION_TARGET_PREDEFINED) rSearchOpt.transliterateFlags |= TransliterationFlags::IGNORE_CASE; - if (pTarget->bWholeWords) + if (rTarget.bWholeWords) rSearchOpt.searchFlag |= util::SearchFlags::NORM_WORD_ONLY; } diff --git a/sfx2/source/doc/autoredactdialog.cxx b/sfx2/source/doc/autoredactdialog.cxx index 1466077e1dbd..32c9eb7c9502 100644 --- a/sfx2/source/doc/autoredactdialog.cxx +++ b/sfx2/source/doc/autoredactdialog.cxx @@ -644,12 +644,13 @@ bool SfxAutoRedactDialog::hasTargets() const return true; } -bool SfxAutoRedactDialog::getTargets(std::vector>& r_aTargets) +bool SfxAutoRedactDialog::getTargets(std::vector>& r_aTargets) { if (m_aTableTargets.empty()) return true; - r_aTargets = m_aTableTargets; + for (auto const& rPair : m_aTableTargets) + r_aTargets.push_back({ *rPair.first, rPair.second }); m_bTargetsCopied = true; return true; } diff --git a/sfx2/source/doc/objserv.cxx b/sfx2/source/doc/objserv.cxx index 46d69bd1dd4b..abb960ff5ffb 100644 --- a/sfx2/source/doc/objserv.cxx +++ b/sfx2/source/doc/objserv.cxx @@ -538,7 +538,7 @@ void SfxObjectShell::ExecFile_Impl(SfxRequest &rReq) bool bIsPDFExport = false; bool bIsAutoRedact = false; - std::vector> aRedactionTargets; + std::vector> aRedactionTargets; switch(nId) { case SID_VERSION: -- cgit