[Libreoffice-commits] core.git: include/xmloff sw/source xmloff/source

Noel Grandin (via logerrit) logerrit at kemper.freedesktop.org
Thu May 20 08:48:26 UTC 2021


 include/xmloff/txtparae.hxx       |    5 +-
 sw/source/filter/xml/xmltexte.cxx |   67 ++++++++++----------------------------
 xmloff/source/text/txtflde.cxx    |    2 -
 xmloff/source/text/txtparae.cxx   |   26 +++-----------
 4 files changed, 30 insertions(+), 70 deletions(-)

New commits:
commit a25385e0d8027d7257ab95a4b787404dfca59913
Author:     Noel Grandin <noel.grandin at collabora.co.uk>
AuthorDate: Thu May 20 10:08:30 2021 +0200
Commit:     Noel Grandin <noel.grandin at collabora.co.uk>
CommitDate: Thu May 20 10:47:35 2021 +0200

    simplify some XMLPropertyState memory management
    
    which fixes a leak if some of the code throws an exception
    
    Change-Id: Ic89060e7951060458ffe7ee5b6fe51cd076ef19b
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/115830
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <noel.grandin at collabora.co.uk>

diff --git a/include/xmloff/txtparae.hxx b/include/xmloff/txtparae.hxx
index 2495f0f504d0..3a7ca544d358 100644
--- a/include/xmloff/txtparae.hxx
+++ b/include/xmloff/txtparae.hxx
@@ -29,6 +29,7 @@
 #include <xmloff/xmltoken.hxx>
 #include <xmloff/SinglePropertySetInfoCache.hxx>
 #include <xmloff/XMLTextListAutoStylePool.hxx>
+#include <o3tl/span.hxx>
 #include <memory>
 #include <vector>
 
@@ -385,14 +386,14 @@ public:
     void Add(
         XmlStyleFamily nFamily,
         const css::uno::Reference< css::beans::XPropertySet > & rPropSet,
-        const XMLPropertyState** pAddState = nullptr, bool bDontSeek = false );
+        const o3tl::span<XMLPropertyState> aAddStates = {}, bool bDontSeek = false );
 
     /// find style name for specified family and parent
     OUString Find(
         XmlStyleFamily nFamily,
         const css::uno::Reference< css::beans::XPropertySet > & rPropSet,
         const OUString& rParent,
-        const XMLPropertyState** pAddState = nullptr ) const;
+        const o3tl::span<XMLPropertyState> aAddStates = {} ) const;
 
     static SvXMLExportPropertyMapper *CreateShapeExtPropMapper(
                                                 SvXMLExport& rExport );
diff --git a/sw/source/filter/xml/xmltexte.cxx b/sw/source/filter/xml/xmltexte.cxx
index c883f4f5528a..b9e3ac9fa03d 100644
--- a/sw/source/filter/xml/xmltexte.cxx
+++ b/sw/source/filter/xml/xmltexte.cxx
@@ -115,19 +115,17 @@ static void lcl_addURL ( SvXMLExport &rExport, const OUString &rURL,
 
 static void lcl_addAspect(
         const svt::EmbeddedObjectRef& rObj,
-        const XMLPropertyState **pStates,
+        std::vector<XMLPropertyState>& rStates,
         const rtl::Reference < XMLPropertySetMapper >& rMapper )
 {
     sal_Int64 nAspect = rObj.GetViewAspect();
     if ( nAspect )
-    {
-        *pStates = new XMLPropertyState( rMapper->FindEntryIndex( CTF_OLE_DRAW_ASPECT ), uno::makeAny( nAspect ) );
-    }
+        rStates.emplace_back( rMapper->FindEntryIndex( CTF_OLE_DRAW_ASPECT ), uno::makeAny( nAspect ) );
 }
 
 static void lcl_addOutplaceProperties(
         const svt::EmbeddedObjectRef& rObj,
-        const XMLPropertyState **pStates,
+        std::vector<XMLPropertyState>& rStates,
         const rtl::Reference < XMLPropertySetMapper >& rMapper )
 {
     MapMode aMode( MapUnit::Map100thMM ); // the API expects this map mode for the embedded objects
@@ -136,21 +134,15 @@ static void lcl_addOutplaceProperties(
     if( !(aSize.Width() && aSize.Height()) )
         return;
 
-    *pStates = new XMLPropertyState( rMapper->FindEntryIndex( CTF_OLE_VIS_AREA_LEFT ), Any(sal_Int32(0)) );
-    pStates++;
-
-    *pStates = new XMLPropertyState( rMapper->FindEntryIndex( CTF_OLE_VIS_AREA_TOP ), Any(sal_Int32(0)) );
-    pStates++;
-
-    *pStates = new XMLPropertyState( rMapper->FindEntryIndex( CTF_OLE_VIS_AREA_WIDTH ), Any(static_cast<sal_Int32>(aSize.Width())) );
-    pStates++;
-
-    *pStates = new XMLPropertyState( rMapper->FindEntryIndex( CTF_OLE_VIS_AREA_HEIGHT ), Any(static_cast<sal_Int32>(aSize.Height())) );
+    rStates.emplace_back( rMapper->FindEntryIndex( CTF_OLE_VIS_AREA_LEFT ), Any(sal_Int32(0)) );
+    rStates.emplace_back( rMapper->FindEntryIndex( CTF_OLE_VIS_AREA_TOP ), Any(sal_Int32(0)) );
+    rStates.emplace_back( rMapper->FindEntryIndex( CTF_OLE_VIS_AREA_WIDTH ), Any(static_cast<sal_Int32>(aSize.Width())) );
+    rStates.emplace_back( rMapper->FindEntryIndex( CTF_OLE_VIS_AREA_HEIGHT ), Any(static_cast<sal_Int32>(aSize.Height())) );
 }
 
 static void lcl_addFrameProperties(
         const uno::Reference < embed::XEmbeddedObject >& xObj,
-        const XMLPropertyState **pStates,
+        std::vector<XMLPropertyState>& rStates,
         const rtl::Reference < XMLPropertySetMapper >& rMapper )
 {
     if ( !::svt::EmbeddedObjectRef::TryRunningState( xObj ) )
@@ -185,24 +177,13 @@ static void lcl_addFrameProperties(
     aAny >>= nHeight;
 
     if( !bIsAutoScroll )
-    {
-        *pStates = new XMLPropertyState( rMapper->FindEntryIndex( CTF_FRAME_DISPLAY_SCROLLBAR ), makeAny(bIsScrollingMode) );
-        pStates++;
-    }
+        rStates.emplace_back( rMapper->FindEntryIndex( CTF_FRAME_DISPLAY_SCROLLBAR ), makeAny(bIsScrollingMode) );
     if( !bIsAutoBorder )
-    {
-        *pStates = new XMLPropertyState( rMapper->FindEntryIndex( CTF_FRAME_DISPLAY_BORDER ), makeAny(bIsBorderSet) );
-        pStates++;
-    }
+        rStates.emplace_back( rMapper->FindEntryIndex( CTF_FRAME_DISPLAY_BORDER ), makeAny(bIsBorderSet) );
     if( SIZE_NOT_SET != nWidth )
-    {
-        *pStates = new XMLPropertyState( rMapper->FindEntryIndex( CTF_FRAME_MARGIN_HORI ), Any(nWidth) );
-        pStates++;
-    }
+        rStates.emplace_back( rMapper->FindEntryIndex( CTF_FRAME_MARGIN_HORI ), Any(nWidth) );
     if( SIZE_NOT_SET != nHeight )
-    {
-        *pStates = new XMLPropertyState( rMapper->FindEntryIndex( CTF_FRAME_MARGIN_VERT ), Any(nHeight) );
-    }
+        rStates.emplace_back( rMapper->FindEntryIndex( CTF_FRAME_MARGIN_VERT ), Any(nHeight) );
 }
 
 void SwXMLTextParagraphExport::_collectTextEmbeddedAutoStyles(
@@ -213,7 +194,8 @@ void SwXMLTextParagraphExport::_collectTextEmbeddedAutoStyles(
     if( !rObjRef.is() )
         return;
 
-    const XMLPropertyState *aStates[8] = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
+    std::vector<XMLPropertyState> aStates;
+    aStates.reserve(8);
     SvGlobalName aClassId( rObjRef->getClassID() );
 
     if( aIFrameClassId == aClassId )
@@ -230,14 +212,7 @@ void SwXMLTextParagraphExport::_collectTextEmbeddedAutoStyles(
     lcl_addAspect( rObjRef, aStates,
            GetAutoFramePropMapper()->getPropertySetMapper() );
 
-    Add( XmlStyleFamily::TEXT_FRAME, rPropSet, aStates );
-
-    const XMLPropertyState **pStates = aStates;
-    while( *pStates )
-    {
-        delete *pStates;
-        pStates++;
-    }
+    Add( XmlStyleFamily::TEXT_FRAME, rPropSet, { aStates.data(), aStates.size() } );
 }
 
 void SwXMLTextParagraphExport::_exportTextEmbedded(
@@ -282,7 +257,8 @@ void SwXMLTextParagraphExport::_exportTextEmbedded(
         aAny >>= sStyle;
     }
 
-    const XMLPropertyState *aStates[8] = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
+    std::vector<XMLPropertyState> aStates;
+    aStates.reserve(8);
     switch( nType )
     {
     case SV_EMBEDDED_FRAME:
@@ -301,13 +277,8 @@ void SwXMLTextParagraphExport::_exportTextEmbedded(
         GetAutoFramePropMapper()->getPropertySetMapper() );
 
     const OUString sAutoStyle = Find( XmlStyleFamily::TEXT_FRAME,
-                                      rPropSet, sStyle, aStates );
-    const XMLPropertyState **pStates = aStates;
-    while( *pStates )
-    {
-        delete *pStates;
-        pStates++;
-    }
+                                      rPropSet, sStyle, { aStates.data(), aStates.size() } );
+    aStates.clear();
 
     if( !sAutoStyle.isEmpty() )
         rXMLExport.AddAttribute( XML_NAMESPACE_DRAW, XML_STYLE_NAME, sAutoStyle );
diff --git a/xmloff/source/text/txtflde.cxx b/xmloff/source/text/txtflde.cxx
index 111df2f0a7bf..962e30e995d4 100644
--- a/xmloff/source/text/txtflde.cxx
+++ b/xmloff/source/text/txtflde.cxx
@@ -896,7 +896,7 @@ void XMLTextFieldExport::ExportFieldAutoStyle(
         // export text style with the addition of the combined characters
         DBG_ASSERT(nullptr != pCombinedCharactersPropertyState,
                    "need proper PropertyState for combined characters");
-        const XMLPropertyState *aStates[] = { pCombinedCharactersPropertyState.get(), nullptr };
+        o3tl::span<XMLPropertyState> aStates( pCombinedCharactersPropertyState.get(), 1 );
         GetExport().GetTextParagraphExport()->Add(
             XmlStyleFamily::TEXT_TEXT, xRangePropSet,
             aStates);
diff --git a/xmloff/source/text/txtparae.cxx b/xmloff/source/text/txtparae.cxx
index b5c5de5b9133..cd715f15e599 100644
--- a/xmloff/source/text/txtparae.cxx
+++ b/xmloff/source/text/txtparae.cxx
@@ -510,7 +510,7 @@ void FieldParamExporter::ExportParameter(const OUString& sKey, const OUString& s
 
 void XMLTextParagraphExport::Add( XmlStyleFamily nFamily,
                                   const Reference < XPropertySet > & rPropSet,
-                                  const XMLPropertyState** ppAddStates, bool bDontSeek )
+                                  const o3tl::span<XMLPropertyState> aAddStates, bool bDontSeek )
 {
     rtl::Reference < SvXMLExportPropertyMapper > xPropMapper;
     switch( nFamily )
@@ -537,14 +537,8 @@ void XMLTextParagraphExport::Add( XmlStyleFamily nFamily,
     vector< XMLPropertyState > aPropStates =
             xPropMapper->Filter(GetExport(), rPropSet);
 
-    if( ppAddStates )
-    {
-        while( *ppAddStates )
-        {
-            aPropStates.push_back( **ppAddStates );
-            ppAddStates++;
-        }
-    }
+    for (const XMLPropertyState& rState : aAddStates)
+        aPropStates.push_back( rState );
 
     if( aPropStates.empty() )
         return;
@@ -741,7 +735,7 @@ OUString XMLTextParagraphExport::Find(
         XmlStyleFamily nFamily,
         const Reference < XPropertySet > & rPropSet,
         const OUString& rParent,
-        const XMLPropertyState** ppAddStates) const
+        const o3tl::span<XMLPropertyState> aAddStates) const
 {
     OUString sName( rParent );
     rtl::Reference < SvXMLExportPropertyMapper > xPropMapper;
@@ -765,14 +759,8 @@ OUString XMLTextParagraphExport::Find(
     if( !xPropMapper.is() )
         return sName;
     vector<XMLPropertyState> aPropStates(xPropMapper->Filter(GetExport(), rPropSet));
-    if( ppAddStates )
-    {
-        while( *ppAddStates )
-        {
-            aPropStates.push_back( **ppAddStates );
-            ++ppAddStates;
-        }
-    }
+    for (const XMLPropertyState& rState : aAddStates)
+        aPropStates.push_back( rState );
     if( std::any_of( aPropStates.begin(), aPropStates.end(), lcl_validPropState ) )
         sName = GetAutoStylePool().Find( nFamily, sName, aPropStates );
 
@@ -1438,7 +1426,7 @@ void XMLTextParagraphExport::collectTextAutoStylesOptimized( bool bIsProgress )
                 aAny = xAutoStylesEnum->nextElement();
                 Reference< XAutoStyle > xAutoStyle = *o3tl::doAccess<Reference<XAutoStyle>>(aAny);
                 Reference < XPropertySet > xPSet( xAutoStyle, uno::UNO_QUERY );
-                Add( nFamily, xPSet, nullptr, true );
+                Add( nFamily, xPSet, {}, true );
             }
         };
         collectFamily("CharacterStyles", XmlStyleFamily::TEXT_TEXT);


More information about the Libreoffice-commits mailing list