diff options
author | Noel Grandin <noel.grandin@collabora.co.uk> | 2018-09-14 15:13:03 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2018-09-15 14:34:41 +0200 |
commit | 8629850eb98ec7f3501f0904063958d61b370747 (patch) | |
tree | 11644d5006d28338948250cb1adaa0f5493f6ee5 /sw | |
parent | 4e59ef9864cc659e7f5560648ed800c3575f805a (diff) |
loplugin:useuniqueptr pass SwTextBlocks around by unique_ptr
fix leaks in SwGlossaryHdl::NewGlossary and SwGlossaryHdl::DelGlossary
and SwGlossaryHdl::InsertGlossary error paths
Change-Id: Iaf98fea08cd44bf68885e053854cf65372fcfc2c
Reviewed-on: https://gerrit.libreoffice.org/60495
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'sw')
-rw-r--r-- | sw/source/ui/misc/glossary.cxx | 4 | ||||
-rw-r--r-- | sw/source/uibase/dochdl/gloshdl.cxx | 73 | ||||
-rw-r--r-- | sw/source/uibase/inc/glosdoc.hxx | 5 | ||||
-rw-r--r-- | sw/source/uibase/inc/gloshdl.hxx | 2 | ||||
-rw-r--r-- | sw/source/uibase/misc/glosdoc.cxx | 14 | ||||
-rw-r--r-- | sw/source/uibase/misc/glshell.cxx | 3 | ||||
-rw-r--r-- | sw/source/uibase/uno/unoatxt.cxx | 6 | ||||
-rw-r--r-- | sw/source/uibase/utlui/gloslst.cxx | 3 |
8 files changed, 58 insertions, 52 deletions
diff --git a/sw/source/ui/misc/glossary.cxx b/sw/source/ui/misc/glossary.cxx index 5e7cee384d25..58ec33857f9c 100644 --- a/sw/source/ui/misc/glossary.cxx +++ b/sw/source/ui/misc/glossary.cxx @@ -771,8 +771,8 @@ IMPL_LINK_NOARG(SwGlossaryDlg, EditHdl, MenuButton *, void) // EndDialog must not be called in MenuHdl if (m_pEditBtn->GetCurItemIdent() == "edit") { - SwTextBlocks *pGroup = ::GetGlossaries()->GetGroupDoc ( GetCurrGrpName () ); - delete pGroup; + std::unique_ptr<SwTextBlocks> pGroup = ::GetGlossaries()->GetGroupDoc ( GetCurrGrpName () ); + pGroup.reset(); EndDialog(RET_EDIT); } } diff --git a/sw/source/uibase/dochdl/gloshdl.cxx b/sw/source/uibase/dochdl/gloshdl.cxx index f18c68f4c436..13a21cf5ceb5 100644 --- a/sw/source/uibase/dochdl/gloshdl.cxx +++ b/sw/source/uibase/dochdl/gloshdl.cxx @@ -138,7 +138,7 @@ void SwGlossaryHdl::SetCurGroup(const OUString &rGrp, bool bApi, bool bAlwaysCre aCurGrp = sGroup; if(!bApi) { - pCurGrp.reset( rStatGlossaries.GetGroupDoc(aCurGrp, true) ); + pCurGrp = rStatGlossaries.GetGroupDoc(aCurGrp, true); } } @@ -152,7 +152,7 @@ OUString SwGlossaryHdl::GetGroupName( size_t nId, OUString* pTitle ) OUString sRet = rStatGlossaries.GetGroupName(nId); if(pTitle) { - SwTextBlocks* pGroup = rStatGlossaries.GetGroupDoc(sRet); + std::unique_ptr<SwTextBlocks> pGroup = rStatGlossaries.GetGroupDoc(sRet); if (pGroup && !pGroup->GetError()) { *pTitle = pGroup->GetName(); @@ -161,11 +161,9 @@ OUString SwGlossaryHdl::GetGroupName( size_t nId, OUString* pTitle ) *pTitle = sRet.getToken(0, GLOS_DELIM); pGroup->SetName(*pTitle); } - delete pGroup; } else { - delete pGroup; sRet.clear(); } } @@ -186,11 +184,10 @@ void SwGlossaryHdl::RenameGroup(const OUString& rOld, OUString& rNew, const OUSt FindGroupName(sOldGroup); if(rOld == rNew) { - SwTextBlocks* pGroup = rStatGlossaries.GetGroupDoc(sOldGroup); + std::unique_ptr<SwTextBlocks> pGroup = rStatGlossaries.GetGroupDoc(sOldGroup); if(pGroup) { pGroup->SetName(rNewTitle); - delete pGroup; } } else @@ -208,12 +205,10 @@ void SwGlossaryHdl::RenameGroup(const OUString& rOld, OUString& rNew, const OUSt bool SwGlossaryHdl::CopyOrMove( const OUString& rSourceGroupName, OUString& rSourceShortName, const OUString& rDestGroupName, const OUString& rLongName, bool bMove ) { - SwTextBlocks* pSourceGroup = rStatGlossaries.GetGroupDoc(rSourceGroupName); - SwTextBlocks* pDestGroup = rStatGlossaries.GetGroupDoc(rDestGroupName); + std::unique_ptr<SwTextBlocks> pSourceGroup = rStatGlossaries.GetGroupDoc(rSourceGroupName); + std::unique_ptr<SwTextBlocks> pDestGroup = rStatGlossaries.GetGroupDoc(rDestGroupName); if (pDestGroup->IsReadOnly() || (bMove && pSourceGroup->IsReadOnly()) ) { - delete pDestGroup; - delete pSourceGroup; return false; } @@ -226,8 +221,6 @@ bool SwGlossaryHdl::CopyOrMove( const OUString& rSourceGroupName, OUString& rSou // the index must be existing nRet = pSourceGroup->Delete( nDeleteIdx ) ? ERRCODE_NONE : ErrCode(1); } - delete pSourceGroup; - delete pDestGroup; return !nRet; } @@ -272,7 +265,7 @@ OUString SwGlossaryHdl::GetGlossaryShortName(const OUString &rName) { OUString sReturn; SwTextBlocks *pTmp = - pCurGrp ? pCurGrp.get() : rStatGlossaries.GetGroupDoc( aCurGrp ); + pCurGrp ? pCurGrp.get() : rStatGlossaries.GetGroupDoc( aCurGrp ).release(); if(pTmp) { sal_uInt16 nIdx = pTmp->GetLongIndex( rName ); @@ -288,7 +281,7 @@ OUString SwGlossaryHdl::GetGlossaryShortName(const OUString &rName) bool SwGlossaryHdl::HasShortName(const OUString& rShortName) const { SwTextBlocks *pBlock = pCurGrp ? pCurGrp.get() - : rStatGlossaries.GetGroupDoc( aCurGrp ); + : rStatGlossaries.GetGroupDoc( aCurGrp ).release(); bool bRet = pBlock->GetIndex( rShortName ) != sal_uInt16(-1); if( !pCurGrp ) delete pBlock; @@ -300,17 +293,25 @@ bool SwGlossaryHdl::NewGlossary(const OUString& rName, const OUString& rShortNam bool bCreateGroup, bool bNoAttr) { SwTextBlocks *pTmp = - pCurGrp ? pCurGrp.get() : rStatGlossaries.GetGroupDoc( aCurGrp, bCreateGroup ); + pCurGrp ? pCurGrp.get() : rStatGlossaries.GetGroupDoc( aCurGrp, bCreateGroup ).release(); //pTmp == 0 if the AutoText path setting is wrong if(!pTmp) + { + if (!pCurGrp) + delete pTmp; return false; + } OUString sOnlyText; OUString* pOnlyText = nullptr; if( bNoAttr ) { if( !pWrtShell->GetSelectedText( sOnlyText, ParaBreakType::ToOnlyCR )) + { + if (!pCurGrp) + delete pTmp; return false; + } pOnlyText = &sOnlyText; } @@ -333,10 +334,14 @@ bool SwGlossaryHdl::NewGlossary(const OUString& rName, const OUString& rShortNam bool SwGlossaryHdl::DelGlossary(const OUString &rShortName) { SwTextBlocks *pGlossary = pCurGrp ? pCurGrp.get() - : rStatGlossaries.GetGroupDoc(aCurGrp); + : rStatGlossaries.GetGroupDoc(aCurGrp).release(); //pTmp == 0 if the AutoText path setting is wrong if(!pGlossary) + { + if( !pCurGrp ) + delete pGlossary; return false; + } sal_uInt16 nIdx = pGlossary->GetIndex( rShortName ); if( nIdx != sal_uInt16(-1) ) @@ -350,13 +355,12 @@ bool SwGlossaryHdl::DelGlossary(const OUString &rShortName) bool SwGlossaryHdl::ExpandGlossary() { OSL_ENSURE(pWrtShell->CanInsert(), "illegal"); - SwTextBlocks *pGlossary; SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create(); ::GlossaryGetCurrGroup fnGetCurrGroup = pFact->GetGlossaryCurrGroupFunc(); OUString sGroupName( (*fnGetCurrGroup)() ); if (sGroupName.indexOf(GLOS_DELIM)<0) FindGroupName(sGroupName); - pGlossary = rStatGlossaries.GetGroupDoc(sGroupName); + std::unique_ptr<SwTextBlocks> pGlossary = rStatGlossaries.GetGroupDoc(sGroupName); OUString aShortName; @@ -379,12 +383,12 @@ bool SwGlossaryHdl::ExpandGlossary() if(pWrtShell->IsSelection()) aShortName = pWrtShell->GetSelText(); } - return pGlossary && Expand( aShortName, &rStatGlossaries, pGlossary ); + return pGlossary && Expand( aShortName, &rStatGlossaries, std::move(pGlossary) ); } bool SwGlossaryHdl::Expand( const OUString& rShortName, SwGlossaries *pGlossaries, - SwTextBlocks *pGlossary ) + std::unique_ptr<SwTextBlocks> pGlossary ) { std::vector<TextBlockInfo_Impl> aFoundArr; OUString aShortName( rShortName ); @@ -422,7 +426,7 @@ bool SwGlossaryHdl::Expand( const OUString& rShortName, } if( !aFoundArr.empty() ) // one was found { - delete pGlossary; + pGlossary.reset(); if (1 == aFoundArr.size()) { TextBlockInfo_Impl& rData = aFoundArr.front(); @@ -462,7 +466,7 @@ bool SwGlossaryHdl::Expand( const OUString& rShortName, { if( !bCancel ) { - delete pGlossary; + pGlossary.reset(); const sal_Int32 nMaxLen = 50; if(pWrtShell->IsSelection() && aShortName.getLength() > nMaxLen) @@ -483,7 +487,7 @@ bool SwGlossaryHdl::Expand( const OUString& rShortName, { SvxMacro aStartMacro(aEmptyOUStr, aEmptyOUStr, STARBASIC); SvxMacro aEndMacro(aEmptyOUStr, aEmptyOUStr, STARBASIC); - GetMacros( aShortName, aStartMacro, aEndMacro, pGlossary ); + GetMacros( aShortName, aStartMacro, aEndMacro, pGlossary.get() ); // StartAction must not be before HasSelection and DelRight, // otherwise the possible Shell change gets delayed and @@ -511,7 +515,6 @@ bool SwGlossaryHdl::Expand( const OUString& rShortName, if( aFieldLst.BuildSortLst() ) pWrtShell->UpdateInputFields( &aFieldLst ); } - delete pGlossary; return true; } @@ -521,10 +524,14 @@ bool SwGlossaryHdl::InsertGlossary(const OUString &rName) OSL_ENSURE(pWrtShell->CanInsert(), "illegal"); SwTextBlocks *pGlos = - pCurGrp ? pCurGrp.get() : rStatGlossaries.GetGroupDoc(aCurGrp); + pCurGrp ? pCurGrp.get() : rStatGlossaries.GetGroupDoc(aCurGrp).release(); if (!pGlos) + { + if (!pCurGrp) + delete pGlos; return false; + } SvxMacro aStartMacro(aEmptyOUStr, aEmptyOUStr, STARBASIC); SvxMacro aEndMacro(aEmptyOUStr, aEmptyOUStr, STARBASIC); @@ -567,7 +574,7 @@ void SwGlossaryHdl::SetMacros(const OUString& rShortName, { SwTextBlocks *pGlos = pGlossary ? pGlossary : pCurGrp ? pCurGrp.get() - : rStatGlossaries.GetGroupDoc( aCurGrp ); + : rStatGlossaries.GetGroupDoc( aCurGrp ).release(); SvxMacroTableDtor aMacroTable; if( pStart ) aMacroTable.Insert( SvMacroItemId::SwStartInsGlossary, *pStart); @@ -588,7 +595,7 @@ void SwGlossaryHdl::GetMacros( const OUString &rShortName, { SwTextBlocks *pGlos = pGlossary ? pGlossary : pCurGrp ? pCurGrp.get() - : rStatGlossaries.GetGroupDoc(aCurGrp); + : rStatGlossaries.GetGroupDoc(aCurGrp).release(); sal_uInt16 nIndex = pGlos->GetIndex( rShortName ); if( nIndex != USHRT_MAX ) { @@ -628,7 +635,7 @@ bool SwGlossaryHdl::Rename(const OUString& rOldShort, const OUString& rNewShortN { bool bRet = false; SwTextBlocks *pGlossary = pCurGrp ? pCurGrp.get() - : rStatGlossaries.GetGroupDoc(aCurGrp); + : rStatGlossaries.GetGroupDoc(aCurGrp).release(); if(pGlossary) { sal_uInt16 nIdx = pGlossary->GetIndex( rOldShort ); @@ -653,11 +660,11 @@ bool SwGlossaryHdl::IsReadOnly( const OUString* pGrpNm ) const SwTextBlocks *pGlossary = nullptr; if (pGrpNm) - pGlossary = rStatGlossaries.GetGroupDoc( *pGrpNm ); + pGlossary = rStatGlossaries.GetGroupDoc( *pGrpNm ).release(); else if (pCurGrp) pGlossary = pCurGrp.get(); else - pGlossary = rStatGlossaries.GetGroupDoc(aCurGrp); + pGlossary = rStatGlossaries.GetGroupDoc(aCurGrp).release(); const bool bRet = !pGlossary || pGlossary->IsReadOnly(); if( pGrpNm || !pCurGrp ) @@ -668,7 +675,7 @@ bool SwGlossaryHdl::IsReadOnly( const OUString* pGrpNm ) const bool SwGlossaryHdl::IsOld() const { SwTextBlocks *pGlossary = pCurGrp ? pCurGrp.get() - : rStatGlossaries.GetGroupDoc(aCurGrp); + : rStatGlossaries.GetGroupDoc(aCurGrp).release(); bool bRet = pGlossary && pGlossary->IsOld(); if( !pCurGrp ) delete pGlossary; @@ -684,7 +691,7 @@ bool SwGlossaryHdl::FindGroupName(OUString& rGroup) bool SwGlossaryHdl::CopyToClipboard(SwWrtShell& rSh, const OUString& rShortName) { SwTextBlocks *pGlossary = pCurGrp ? pCurGrp.get() - : rStatGlossaries.GetGroupDoc(aCurGrp); + : rStatGlossaries.GetGroupDoc(aCurGrp).release(); rtl::Reference<SwTransferable> pTransfer = new SwTransferable( rSh ); @@ -709,7 +716,7 @@ bool SwGlossaryHdl::ImportGlossaries( const OUString& rName ) pMed->SetFilter( pFilter ); Reader* pR = SwReaderWriter::GetReader( pFilter->GetUserData() ); if( pR && nullptr != ( pGlossary = pCurGrp ? pCurGrp.get() - : rStatGlossaries.GetGroupDoc(aCurGrp)) ) + : rStatGlossaries.GetGroupDoc(aCurGrp).release()) ) { SwReader aReader( *pMed, rName ); if( aReader.HasGlossaries( *pR ) ) diff --git a/sw/source/uibase/inc/glosdoc.hxx b/sw/source/uibase/inc/glosdoc.hxx index 2ed0ef9fade9..41ff64825b7d 100644 --- a/sw/source/uibase/inc/glosdoc.hxx +++ b/sw/source/uibase/inc/glosdoc.hxx @@ -51,7 +51,7 @@ class SW_DLLPUBLIC SwGlossaries std::vector<OUString> m_GlosArr; bool m_bError; - SAL_DLLPRIVATE SwTextBlocks* GetGlosDoc(const OUString &rName, bool bCreate = true) const; + SAL_DLLPRIVATE std::unique_ptr<SwTextBlocks> GetGlosDoc(const OUString &rName, bool bCreate = true) const; SAL_DLLPRIVATE std::vector<OUString> & GetNameList(); // implementation in unoatxt.cxx @@ -102,7 +102,8 @@ public: bool FindGroupName(OUString& rGroup); - SwTextBlocks* GetGroupDoc(const OUString &rName, + std::unique_ptr<SwTextBlocks> + GetGroupDoc(const OUString &rName, bool bCreate = false); static OUString GetDefName(); static OUString GetExtension(); diff --git a/sw/source/uibase/inc/gloshdl.hxx b/sw/source/uibase/inc/gloshdl.hxx index dc7f3b0e740a..87e5c807a698 100644 --- a/sw/source/uibase/inc/gloshdl.hxx +++ b/sw/source/uibase/inc/gloshdl.hxx @@ -41,7 +41,7 @@ class SW_DLLPUBLIC SwGlossaryHdl SAL_DLLPRIVATE bool Expand( const OUString& rShortName, SwGlossaries* pGlossaries, - SwTextBlocks *pGlossary ); + std::unique_ptr<SwTextBlocks> pGlossary ); public: void GlossaryDlg(); diff --git a/sw/source/uibase/misc/glosdoc.cxx b/sw/source/uibase/misc/glosdoc.cxx index b2037bc4e61b..c0d17e3cc95e 100644 --- a/sw/source/uibase/misc/glosdoc.cxx +++ b/sw/source/uibase/misc/glosdoc.cxx @@ -145,17 +145,16 @@ OUString SwGlossaries::GetGroupTitle( const OUString& rGroupName ) OUString sGroup(rGroupName); if (sGroup.indexOf(GLOS_DELIM)<0) FindGroupName(sGroup); - SwTextBlocks* pGroup = GetGroupDoc(sGroup); + std::unique_ptr<SwTextBlocks> pGroup = GetGroupDoc(sGroup); if(pGroup) { sRet = pGroup->GetName(); - delete pGroup; } return sRet; } // supplies the group rName's text block document -SwTextBlocks* SwGlossaries::GetGroupDoc(const OUString &rName, +std::unique_ptr<SwTextBlocks> SwGlossaries::GetGroupDoc(const OUString &rName, bool bCreate) { // insert to the list of text blocks if applicable @@ -186,12 +185,11 @@ bool SwGlossaries::NewGroupDoc(OUString& rGroupName, const OUString& rTitle) const OUString sNewFilePath(m_PathArr[nNewPath]); const OUString sNewGroup = lcl_CheckFileName(sNewFilePath, rGroupName.getToken(0, GLOS_DELIM)) + OUStringLiteral1(GLOS_DELIM) + sNewPath; - SwTextBlocks *pBlock = GetGlosDoc( sNewGroup ); + std::unique_ptr<SwTextBlocks> pBlock = GetGlosDoc( sNewGroup ); if(pBlock) { GetNameList().push_back(sNewGroup); pBlock->SetName(rTitle); - delete pBlock; rGroupName = sNewGroup; return true; } @@ -273,10 +271,10 @@ SwGlossaries::~SwGlossaries() } // read a block document -SwTextBlocks* SwGlossaries::GetGlosDoc( const OUString &rName, bool bCreate ) const +std::unique_ptr<SwTextBlocks> SwGlossaries::GetGlosDoc( const OUString &rName, bool bCreate ) const { sal_uInt16 nPath = static_cast<sal_uInt16>(rName.getToken(1, GLOS_DELIM).toInt32()); - SwTextBlocks *pTmp = nullptr; + std::unique_ptr<SwTextBlocks> pTmp; if (static_cast<size_t>(nPath) < m_PathArr.size()) { const OUString sFileURL = @@ -288,7 +286,7 @@ SwTextBlocks* SwGlossaries::GetGlosDoc( const OUString &rName, bool bCreate ) co if (bCreate || bExist) { - pTmp = new SwTextBlocks( sFileURL ); + pTmp.reset(new SwTextBlocks( sFileURL )); bool bOk = true; if( pTmp->GetError() ) { diff --git a/sw/source/uibase/misc/glshell.cxx b/sw/source/uibase/misc/glshell.cxx index 6a0cca3eb80c..fa953c925093 100644 --- a/sw/source/uibase/misc/glshell.cxx +++ b/sw/source/uibase/misc/glshell.cxx @@ -201,7 +201,7 @@ SwDocShellRef SwGlossaries::EditGroupDoc( const OUString& rGroup, const OUString { SwDocShellRef xDocSh; - SwTextBlocks* pGroup = GetGroupDoc( rGroup ); + std::unique_ptr<SwTextBlocks> pGroup = GetGroupDoc( rGroup ); if (pGroup && pGroup->GetCount()) { // query which view is registered. In WebWriter there is no normal view @@ -268,7 +268,6 @@ SwDocShellRef SwGlossaries::EditGroupDoc( const OUString& rGroup, const OUString if ( bShow ) pFrame->GetFrame().Appear(); } - delete pGroup; return xDocSh; } diff --git a/sw/source/uibase/uno/unoatxt.cxx b/sw/source/uibase/uno/unoatxt.cxx index b618f19bf2b4..521ff75f09ac 100644 --- a/sw/source/uibase/uno/unoatxt.cxx +++ b/sw/source/uibase/uno/unoatxt.cxx @@ -340,7 +340,9 @@ uno::Reference< text::XAutoTextEntry > SwXAutoTextGroup::insertNewByName(const if(!xTextRange.is()) throw uno::RuntimeException(); - SwTextBlocks* pGlosGroup = pGlossaries ? pGlossaries->GetGroupDoc(m_sGroupName) : nullptr; + std::unique_ptr<SwTextBlocks> pGlosGroup; + if (pGlossaries) + pGlosGroup = pGlossaries->GetGroupDoc(m_sGroupName); const OUString& sShortName(aName); const OUString& sLongName(aTitle); if (pGlosGroup && !pGlosGroup->GetError()) @@ -398,7 +400,7 @@ uno::Reference< text::XAutoTextEntry > SwXAutoTextGroup::insertNewByName(const throw uno::RuntimeException(); } } - delete pGlosGroup; + pGlosGroup.reset(); uno::Reference< text::XAutoTextEntry > xEntry; diff --git a/sw/source/uibase/utlui/gloslst.cxx b/sw/source/uibase/utlui/gloslst.cxx index 497c002933b8..2827cf3c7a52 100644 --- a/sw/source/uibase/utlui/gloslst.cxx +++ b/sw/source/uibase/utlui/gloslst.cxx @@ -353,7 +353,7 @@ AutoTextGroup* SwGlossaryList::FindGroup(const OUString& rGroupName) void SwGlossaryList::FillGroup(AutoTextGroup* pGroup, SwGlossaries* pGlossaries) { - SwTextBlocks* pBlock = pGlossaries->GetGroupDoc(pGroup->sName); + std::unique_ptr<SwTextBlocks> pBlock = pGlossaries->GetGroupDoc(pGroup->sName); pGroup->nCount = pBlock ? pBlock->GetCount() : 0; pGroup->sLongNames.clear(); pGroup->sShortNames.clear(); @@ -367,7 +367,6 @@ void SwGlossaryList::FillGroup(AutoTextGroup* pGroup, SwGlossaries* pGlossaries) pGroup->sShortNames += pBlock->GetShortName(j) + OUStringLiteral1(STRING_DELIM); } - delete pBlock; } // Give back all (not exceeding FIND_MAX_GLOS) found modules |