[Libreoffice-commits] core.git: tools/source

Jochen Nitschke j.nitschke+logerrit at ok.de
Thu Nov 24 05:09:30 UTC 2016


 tools/source/inet/inetmime.cxx |   49 +++++++++++++----------------------------
 1 file changed, 16 insertions(+), 33 deletions(-)

New commits:
commit b367c81d06f64e500063ebb814eda5e8d55309ca
Author: Jochen Nitschke <j.nitschke+logerrit at ok.de>
Date:   Wed Nov 23 19:49:21 2016 +0100

    replace custom list with std::forward_list
    
    cppcheck warns with publicAllocationError in
    INetMIMECharsetList_Impl::prepend(...)
    >     { m_pFirst = new Node(rCharset, m_pFirst); }<--- Possible leak
    > in public function. The pointer 'm_pFirst' is not deallocated before
    > it is allocated.
    
    We got a single linked list here which is correctly deallocated
    in dtor.
    
    But with C++11 std::forward_list there is no reason to implement or own
    list.
    
    Change-Id: Ia6582061c1e5f1284bd81fafef88ce475ee891aa
    Reviewed-on: https://gerrit.libreoffice.org/31132
    Tested-by: Jenkins <ci at libreoffice.org>
    Reviewed-by: Noel Grandin <noel.grandin at collabora.co.uk>

diff --git a/tools/source/inet/inetmime.cxx b/tools/source/inet/inetmime.cxx
index 34fb6aa..05ae7cb 100644
--- a/tools/source/inet/inetmime.cxx
+++ b/tools/source/inet/inetmime.cxx
@@ -18,6 +18,7 @@
  */
 
 #include <cstddef>
+#include <forward_list>
 #include <limits>
 #include <memory>
 
@@ -429,20 +430,19 @@ class INetMIMECharsetList_Impl
     {
         Charset m_aCharset;
         bool m_bDisabled;
-        Node * m_pNext;
 
-        inline Node(const Charset & rTheCharset, Node * pTheNext);
+        explicit Node(const Charset & rCharset)
+            :m_aCharset(rCharset), m_bDisabled(false)
+        {}
     };
 
-    Node * m_pFirst;
+    std::forward_list<Node> m_aList;
 
 public:
-    INetMIMECharsetList_Impl(): m_pFirst(nullptr) {}
-
-    ~INetMIMECharsetList_Impl();
-
     void prepend(const Charset & rCharset)
-    { m_pFirst = new Node(rCharset, m_pFirst); }
+    {
+        m_aList.emplace_front(rCharset);
+    }
 
     void includes(sal_uInt32 nChar);
 
@@ -452,13 +452,6 @@ public:
     void reset();
 };
 
-inline INetMIMECharsetList_Impl::Node::Node(const Charset & rTheCharset,
-                                            Node * pTheNext):
-    m_aCharset(rTheCharset),
-    m_bDisabled(false),
-    m_pNext(pTheNext)
-{}
-
 struct Parameter
 {
     Parameter * m_pNext;
@@ -539,36 +532,26 @@ void appendISO88591(OUString & rText, sal_Char const * pBegin,
 
 //  INetMIMECharsetList_Impl
 
-INetMIMECharsetList_Impl::~INetMIMECharsetList_Impl()
-{
-    while (m_pFirst)
-    {
-        Node * pRemove = m_pFirst;
-        m_pFirst = m_pFirst->m_pNext;
-        delete pRemove;
-    }
-}
-
 void INetMIMECharsetList_Impl::includes(sal_uInt32 nChar)
 {
-    for (Node * p = m_pFirst; p; p = p->m_pNext)
-        if (!(p->m_bDisabled || p->m_aCharset.contains(nChar)))
-            p->m_bDisabled = true;
+    for (Node& rNode : m_aList)
+        if (!(rNode.m_bDisabled || rNode.m_aCharset.contains(nChar)))
+            rNode.m_bDisabled = true;
 }
 
 rtl_TextEncoding INetMIMECharsetList_Impl::getPreferredEncoding(rtl_TextEncoding eDefault)
     const
 {
-    for (Node * p = m_pFirst; p; p = p->m_pNext)
-        if (!p->m_bDisabled)
-            return p->m_aCharset.getEncoding();
+    for (const Node& rNode : m_aList)
+        if (!rNode.m_bDisabled)
+            return rNode.m_aCharset.getEncoding();
     return eDefault;
 }
 
 void INetMIMECharsetList_Impl::reset()
 {
-    for (Node * p = m_pFirst; p; p = p->m_pNext)
-        p->m_bDisabled = false;
+    for (Node& rNode : m_aList)
+        rNode.m_bDisabled = false;
 }
 
 //  ParameterList


More information about the Libreoffice-commits mailing list