[Libreoffice-commits] core.git: sc/inc sc/source

Noel Grandin (via logerrit) logerrit at kemper.freedesktop.org
Sun May 16 18:46:15 UTC 2021


 sc/inc/subtotalparam.hxx              |    5 +++--
 sc/source/core/data/subtotalparam.cxx |   23 ++++++++---------------
 sc/source/core/data/table3.cxx        |    8 ++++----
 sc/source/filter/xml/xmldrani.cxx     |    8 ++++----
 sc/source/ui/dbgui/tpsubt.cxx         |    4 ++--
 sc/source/ui/unoobj/datauno.cxx       |   20 ++++++++++----------
 6 files changed, 31 insertions(+), 37 deletions(-)

New commits:
commit 203288c830041b41268f23b9aed5ad786a8e7ae6
Author:     Noel Grandin <noel.grandin at collabora.co.uk>
AuthorDate: Sun May 16 16:12:24 2021 +0200
Commit:     Noel Grandin <noel.grandin at collabora.co.uk>
CommitDate: Sun May 16 20:45:33 2021 +0200

    fix leak in ScSubTotalParam
    
    Change-Id: If839585931fc90b9910f6b95338d59ba48a1a78f
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/115676
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <noel.grandin at collabora.co.uk>

diff --git a/sc/inc/subtotalparam.hxx b/sc/inc/subtotalparam.hxx
index d9701c5379d4..8e36dad83987 100644
--- a/sc/inc/subtotalparam.hxx
+++ b/sc/inc/subtotalparam.hxx
@@ -10,6 +10,7 @@
 #pragma once
 
 #include "global.hxx"
+#include <memory>
 
 struct SC_DLLPUBLIC ScSubTotalParam
 {
@@ -29,8 +30,8 @@ struct SC_DLLPUBLIC ScSubTotalParam
     bool            bGroupActive[MAXSUBTOTAL];  ///< active groups
     SCCOL           nField[MAXSUBTOTAL];        ///< associated field
     SCCOL           nSubTotals[MAXSUBTOTAL];    ///< number of SubTotals
-    SCCOL*          pSubTotals[MAXSUBTOTAL];    ///< array of columns to be calculated
-    ScSubTotalFunc* pFunctions[MAXSUBTOTAL];    ///< array of associated functions
+    std::unique_ptr<SCCOL[]> pSubTotals[MAXSUBTOTAL];    ///< array of columns to be calculated
+    std::unique_ptr<ScSubTotalFunc[]> pFunctions[MAXSUBTOTAL];    ///< array of associated functions
 
     ScSubTotalParam();
     ScSubTotalParam( const ScSubTotalParam& r );
diff --git a/sc/source/core/data/subtotalparam.cxx b/sc/source/core/data/subtotalparam.cxx
index fc9a565f0d8e..e8f32954297c 100644
--- a/sc/source/core/data/subtotalparam.cxx
+++ b/sc/source/core/data/subtotalparam.cxx
@@ -37,8 +37,8 @@ ScSubTotalParam::ScSubTotalParam( const ScSubTotalParam& r ) :
         if ( (r.nSubTotals[i] > 0) && r.pSubTotals[i] && r.pFunctions[i] )
         {
             nSubTotals[i] = r.nSubTotals[i];
-            pSubTotals[i] = new SCCOL   [r.nSubTotals[i]];
-            pFunctions[i] = new ScSubTotalFunc  [r.nSubTotals[i]];
+            pSubTotals[i].reset(new SCCOL   [r.nSubTotals[i]]);
+            pFunctions[i].reset(new ScSubTotalFunc  [r.nSubTotals[i]]);
 
             for (SCCOL j=0; j<r.nSubTotals[i]; j++)
             {
@@ -49,8 +49,6 @@ ScSubTotalParam::ScSubTotalParam( const ScSubTotalParam& r ) :
         else
         {
             nSubTotals[i] = 0;
-            pSubTotals[i] = nullptr;
-            pFunctions[i] = nullptr;
         }
     }
 }
@@ -103,13 +101,13 @@ ScSubTotalParam& ScSubTotalParam::operator=( const ScSubTotalParam& r )
         nField[i]       = r.nField[i];
         nSubTotals[i]   = r.nSubTotals[i];
 
-        if ( pSubTotals[i] ) delete [] pSubTotals[i];
-        if ( pFunctions[i] ) delete [] pFunctions[i];
+        pSubTotals[i].reset();
+        pFunctions[i].reset();
 
         if ( r.nSubTotals[i] > 0 )
         {
-            pSubTotals[i] = new SCCOL   [r.nSubTotals[i]];
-            pFunctions[i] = new ScSubTotalFunc  [r.nSubTotals[i]];
+            pSubTotals[i].reset(new SCCOL   [r.nSubTotals[i]]);
+            pFunctions[i].reset(new ScSubTotalFunc  [r.nSubTotals[i]]);
 
             for (SCCOL j=0; j<r.nSubTotals[i]; j++)
             {
@@ -120,8 +118,6 @@ ScSubTotalParam& ScSubTotalParam::operator=( const ScSubTotalParam& r )
         else
         {
             nSubTotals[i] = 0;
-            pSubTotals[i] = nullptr;
-            pFunctions[i] = nullptr;
         }
     }
 
@@ -189,11 +185,8 @@ void ScSubTotalParam::SetSubTotals( sal_uInt16 nGroup,
     if (nGroup != 0)
         nGroup--;
 
-    delete [] pSubTotals[nGroup];
-    delete [] pFunctions[nGroup];
-
-    pSubTotals[nGroup] = new SCCOL      [nCount];
-    pFunctions[nGroup] = new ScSubTotalFunc [nCount];
+    pSubTotals[nGroup].reset(new SCCOL[nCount]);
+    pFunctions[nGroup].reset(new ScSubTotalFunc[nCount]);
     nSubTotals[nGroup] = static_cast<SCCOL>(nCount);
 
     for ( sal_uInt16 i=0; i<nCount; i++ )
diff --git a/sc/source/core/data/table3.cxx b/sc/source/core/data/table3.cxx
index 552abe5b0370..e8837f6ca95a 100644
--- a/sc/source/core/data/table3.cxx
+++ b/sc/source/core/data/table3.cxx
@@ -1998,7 +1998,7 @@ bool ScTable::DoSubTotals( ScSubTotalParam& rParam )
         // how many results per level
         SCCOL nResCount         = rParam.nSubTotals[aRowEntry.nGroupNo];
         // result functions
-        ScSubTotalFunc* pResFunc = rParam.pFunctions[aRowEntry.nGroupNo];
+        ScSubTotalFunc* pResFunc = rParam.pFunctions[aRowEntry.nGroupNo].get();
 
         if (nResCount > 0)                                      // otherwise only sort
         {
@@ -2113,7 +2113,7 @@ bool ScTable::DoSubTotals( ScSubTotalParam& rParam )
         for (sal_uInt16 nLevel = 0; nLevel<nLevelCount; nLevel++)
         {
             const sal_uInt16 nGroupNo = nLevelCount - nLevel - 1;
-            const ScSubTotalFunc* pResFunc = rParam.pFunctions[nGroupNo];
+            const ScSubTotalFunc* pResFunc = rParam.pFunctions[nGroupNo].get();
             if (!pResFunc)
             {
                 // No subtotal function given for this group => no formula or
@@ -2158,8 +2158,8 @@ bool ScTable::DoSubTotals( ScSubTotalParam& rParam )
     for (const auto& rRowEntry : aRowVector)
     {
         SCCOL nResCount         = rParam.nSubTotals[rRowEntry.nGroupNo];
-        SCCOL* nResCols         = rParam.pSubTotals[rRowEntry.nGroupNo];
-        ScSubTotalFunc* pResFunc = rParam.pFunctions[rRowEntry.nGroupNo];
+        SCCOL* nResCols         = rParam.pSubTotals[rRowEntry.nGroupNo].get();
+        ScSubTotalFunc* pResFunc = rParam.pFunctions[rRowEntry.nGroupNo].get();
         for ( SCCOL nResult=0; nResult < nResCount; ++nResult )
         {
             aRef.Ref1.SetAbsCol(nResCols[nResult]);
diff --git a/sc/source/filter/xml/xmldrani.cxx b/sc/source/filter/xml/xmldrani.cxx
index 756e874f91c7..d1701179d912 100644
--- a/sc/source/filter/xml/xmldrani.cxx
+++ b/sc/source/filter/xml/xmldrani.cxx
@@ -344,8 +344,8 @@ std::unique_ptr<ScDBData> ScXMLDatabaseRangeContext::ConvertToDBData(const OUStr
             aParam.nSubTotals[nPos] = nCount;
             if (nCount != 0)
             {
-                aParam.pSubTotals[nPos] = new SCCOL[nCount];
-                aParam.pFunctions[nPos] = new ScSubTotalFunc[nCount];
+                aParam.pSubTotals[nPos].reset(new SCCOL[nCount]);
+                aParam.pFunctions[nPos].reset(new ScSubTotalFunc[nCount]);
 
                 const sheet::SubTotalColumn* pAry = rColumns.getConstArray();
                 for (SCCOL i = 0; i < nCount; ++i)
@@ -356,8 +356,8 @@ std::unique_ptr<ScDBData> ScXMLDatabaseRangeContext::ConvertToDBData(const OUStr
             }
             else
             {
-                aParam.pSubTotals[nPos] = nullptr;
-                aParam.pFunctions[nPos] = nullptr;
+                aParam.pSubTotals[nPos].reset();
+                aParam.pFunctions[nPos].reset();
             }
             ++nPos;
         }
diff --git a/sc/source/ui/dbgui/tpsubt.cxx b/sc/source/ui/dbgui/tpsubt.cxx
index 4b8c2e1396be..9ece3597bb50 100644
--- a/sc/source/ui/dbgui/tpsubt.cxx
+++ b/sc/source/ui/dbgui/tpsubt.cxx
@@ -133,8 +133,8 @@ bool ScTpSubTotalGroup::DoReset( sal_uInt16             nGroupNo,
     {
         SCCOL           nField      = theSubTotalData.nField[nGroupIdx];
         SCCOL           nSubTotals  = theSubTotalData.nSubTotals[nGroupIdx];
-        SCCOL*          pSubTotals  = theSubTotalData.pSubTotals[nGroupIdx];
-        ScSubTotalFunc* pFunctions  = theSubTotalData.pFunctions[nGroupIdx];
+        SCCOL*          pSubTotals  = theSubTotalData.pSubTotals[nGroupIdx].get();
+        ScSubTotalFunc* pFunctions  = theSubTotalData.pFunctions[nGroupIdx].get();
 
         mxLbGroup->set_active( GetFieldSelPos( nField )+1 );
 
diff --git a/sc/source/ui/unoobj/datauno.cxx b/sc/source/ui/unoobj/datauno.cxx
index 4691b7082326..d1c1f55bc1a1 100644
--- a/sc/source/ui/unoobj/datauno.cxx
+++ b/sc/source/ui/unoobj/datauno.cxx
@@ -507,8 +507,8 @@ void SAL_CALL ScSubTotalFieldObj::setSubTotalColumns(
         aParam.nSubTotals[nPos] = nCount;
         if (nCount != 0)
         {
-            aParam.pSubTotals[nPos] = new SCCOL[nCount];
-            aParam.pFunctions[nPos] = new ScSubTotalFunc[nCount];
+            aParam.pSubTotals[nPos].reset(new SCCOL[nCount]);
+            aParam.pFunctions[nPos].reset(new ScSubTotalFunc[nCount]);
 
             const sheet::SubTotalColumn* pAry = aSubTotalColumns.getConstArray();
             for (SCCOL i=0; i<nCount; i++)
@@ -519,8 +519,8 @@ void SAL_CALL ScSubTotalFieldObj::setSubTotalColumns(
         }
         else
         {
-            aParam.pSubTotals[nPos] = nullptr;
-            aParam.pFunctions[nPos] = nullptr;
+            aParam.pSubTotals[nPos].reset();
+            aParam.pFunctions[nPos].reset();
         }
     }
     //! otherwise exception or so? (too many columns)
@@ -581,15 +581,15 @@ void SAL_CALL ScSubTotalDescriptorBase::addNew(
     aParam.bGroupActive[nPos] = true;
     aParam.nField[nPos] = static_cast<SCCOL>(nGroupColumn);
 
-    delete aParam.pSubTotals[nPos];
-    delete aParam.pFunctions[nPos];
+    aParam.pSubTotals[nPos].reset();
+    aParam.pFunctions[nPos].reset();
 
     SCCOL nCount = static_cast<SCCOL>(nColCount);
     aParam.nSubTotals[nPos] = nCount;
     if (nCount != 0)
     {
-        aParam.pSubTotals[nPos] = new SCCOL[nCount];
-        aParam.pFunctions[nPos] = new ScSubTotalFunc[nCount];
+        aParam.pSubTotals[nPos].reset(new SCCOL[nCount]);
+        aParam.pFunctions[nPos].reset(new ScSubTotalFunc[nCount]);
 
         const sheet::SubTotalColumn* pAry = aSubTotalColumns.getConstArray();
         for (SCCOL i=0; i<nCount; i++)
@@ -600,8 +600,8 @@ void SAL_CALL ScSubTotalDescriptorBase::addNew(
     }
     else
     {
-        aParam.pSubTotals[nPos] = nullptr;
-        aParam.pFunctions[nPos] = nullptr;
+        aParam.pSubTotals[nPos].reset();
+        aParam.pFunctions[nPos].reset();
     }
 
     PutData(aParam);


More information about the Libreoffice-commits mailing list