vcl/inc/bitmap/Octree.hxx    |    5 ++---
 vcl/source/bitmap/Octree.cxx |   40 ++++++++++++++++------------------------
 2 files changed, 18 insertions(+), 27 deletions(-)

New commits:
commit 6da6e942409d8a9bd4d9a95c43f489443e88346f
Author:     Stephan Bergmann <sberg...@redhat.com>
AuthorDate: Mon Jan 17 21:47:48 2022 +0100
Commit:     Caolán McNamara <caol...@redhat.com>
CommitDate: Sat Feb 5 21:53:55 2022 +0100

    Avoid -Werror=dangling-pointer=
    
    > vcl/source/bitmap/Octree.cxx: In constructor ‘Octree::Octree(const 
BitmapReadAccess&, sal_uLong)’:
    > vcl/source/bitmap/Octree.cxx:69:17: error: storing the address of local 
variable ‘aColor’ in ‘*this.Octree::mpColor’ [-Werror=dangling-pointer=]
    >    69 |         mpColor = &aColor;
    >       |         ~~~~~~~~^~~~~~~~~
    > vcl/source/bitmap/Octree.cxx:67:21: note: ‘aColor’ declared here
    >    67 |         BitmapColor aColor;
    >       |                     ^~~~~~
    > vcl/source/bitmap/Octree.cxx:67:21: note: ‘<unknown>’ declared here
    
    (new with GCC 12)
    
    Change-Id: I5b1ffa15b92f2c41dbe51dfa843eb6bab3a4b449
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/128517
    Tested-by: Jenkins
    Reviewed-by: Stephan Bergmann <sberg...@redhat.com>
    (cherry picked from commit 5db574727f4564238a54159a1a0673eaa2884b69)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/129444
    Reviewed-by: Caolán McNamara <caol...@redhat.com>

diff --git a/vcl/inc/bitmap/Octree.hxx b/vcl/inc/bitmap/Octree.hxx
index 216f2fdd1875..4db06502256d 100644
--- a/vcl/inc/bitmap/Octree.hxx
+++ b/vcl/inc/bitmap/Octree.hxx
@@ -42,9 +42,9 @@ class VCL_PLUGIN_PUBLIC Octree
 {
 private:
     void CreatePalette(OctreeNode* pNode);
-    void GetPalIndex(const OctreeNode* pNode);
+    void GetPalIndex(const OctreeNode* pNode, BitmapColor const& color);
 
-    SAL_DLLPRIVATE void add(std::unique_ptr<OctreeNode>& rpNode);
+    SAL_DLLPRIVATE void add(std::unique_ptr<OctreeNode>& rpNode, BitmapColor 
const& color);
     SAL_DLLPRIVATE void reduce();
 
     BitmapPalette maPalette;
@@ -52,7 +52,6 @@ private:
     sal_uLong mnLevel;
     std::unique_ptr<OctreeNode> pTree;
     std::vector<OctreeNode*> mpReduce;
-    BitmapColor const* mpColor;
     sal_uInt16 mnPalIndex;
 
 public:
diff --git a/vcl/source/bitmap/Octree.cxx b/vcl/source/bitmap/Octree.cxx
index fab0c8374ca8..98ad8c9fcf5d 100644
--- a/vcl/source/bitmap/Octree.cxx
+++ b/vcl/source/bitmap/Octree.cxx
@@ -34,7 +34,6 @@ Octree::Octree(const BitmapReadAccess& rReadAcc, sal_uLong 
nColors)
     : mnLeafCount(0)
     , mnLevel(0)
     , mpReduce(OCTREE_BITS + 1, nullptr)
-    , mpColor(nullptr)
     , mnPalIndex(0)
 {
     const BitmapReadAccess* pAccess = &rReadAcc;
@@ -53,9 +52,8 @@ Octree::Octree(const BitmapReadAccess& rReadAcc, sal_uLong 
nColors)
             Scanline pScanline = pAccess->GetScanline(nY);
             for (tools::Long nX = 0; nX < nWidth; nX++)
             {
-                mpColor = 
&pAccess->GetPaletteColor(pAccess->GetIndexFromData(pScanline, nX));
                 mnLevel = 0;
-                add(pTree);
+                add(pTree, 
pAccess->GetPaletteColor(pAccess->GetIndexFromData(pScanline, nX)));
 
                 while (mnLeafCount > nMax)
                     reduce();
@@ -64,18 +62,13 @@ Octree::Octree(const BitmapReadAccess& rReadAcc, sal_uLong 
nColors)
     }
     else
     {
-        BitmapColor aColor;
-
-        mpColor = &aColor;
-
         for (tools::Long nY = 0; nY < nHeight; nY++)
         {
             Scanline pScanline = pAccess->GetScanline(nY);
             for (tools::Long nX = 0; nX < nWidth; nX++)
             {
-                aColor = pAccess->GetPixelFromData(pScanline, nX);
                 mnLevel = 0;
-                add(pTree);
+                add(pTree, pAccess->GetPixelFromData(pScanline, nX));
 
                 while (mnLeafCount > nMax)
                     reduce();
@@ -86,7 +79,7 @@ Octree::Octree(const BitmapReadAccess& rReadAcc, sal_uLong 
nColors)
 
 Octree::~Octree() {}
 
-void Octree::add(std::unique_ptr<OctreeNode>& rpNode)
+void Octree::add(std::unique_ptr<OctreeNode>& rpNode, BitmapColor const& color)
 {
     // possibly generate new nodes
     if (!rpNode)
@@ -106,20 +99,20 @@ void Octree::add(std::unique_ptr<OctreeNode>& rpNode)
     if (rpNode->bLeaf)
     {
         rpNode->nCount++;
-        rpNode->nRed += mpColor->GetRed();
-        rpNode->nGreen += mpColor->GetGreen();
-        rpNode->nBlue += mpColor->GetBlue();
+        rpNode->nRed += color.GetRed();
+        rpNode->nGreen += color.GetGreen();
+        rpNode->nBlue += color.GetBlue();
     }
     else
     {
         const sal_uLong nShift = 7 - mnLevel;
         const sal_uInt8 cMask = 0x80 >> mnLevel;
-        const sal_uLong nIndex = (((mpColor->GetRed() & cMask) >> nShift) << 2)
-                                 | (((mpColor->GetGreen() & cMask) >> nShift) 
<< 1)
-                                 | ((mpColor->GetBlue() & cMask) >> nShift);
+        const sal_uLong nIndex = (((color.GetRed() & cMask) >> nShift) << 2)
+                                 | (((color.GetGreen() & cMask) >> nShift) << 
1)
+                                 | ((color.GetBlue() & cMask) >> nShift);
 
         mnLevel++;
-        add(rpNode->pChild[nIndex]);
+        add(rpNode->pChild[nIndex], color);
     }
 }
 
@@ -184,7 +177,7 @@ void Octree::CreatePalette(OctreeNode* pNode)
     }
 }
 
-void Octree::GetPalIndex(const OctreeNode* pNode)
+void Octree::GetPalIndex(const OctreeNode* pNode, BitmapColor const& color)
 {
     if (pNode->bLeaf)
         mnPalIndex = pNode->nPalIndex;
@@ -193,11 +186,11 @@ void Octree::GetPalIndex(const OctreeNode* pNode)
         const sal_uLong nShift = 7 - mnLevel;
         const sal_uInt8 cMask = 0x80 >> mnLevel;
         mnLevel++;
-        const sal_uLong nIndex = (((mpColor->GetRed() & cMask) >> nShift) << 2)
-                                 | (((mpColor->GetGreen() & cMask) >> nShift) 
<< 1)
-                                 | ((mpColor->GetBlue() & cMask) >> nShift);
+        const sal_uLong nIndex = (((color.GetRed() & cMask) >> nShift) << 2)
+                                 | (((color.GetGreen() & cMask) >> nShift) << 
1)
+                                 | ((color.GetBlue() & cMask) >> nShift);
 
-        GetPalIndex(pNode->pChild[nIndex].get());
+        GetPalIndex(pNode->pChild[nIndex].get(), color);
     }
 }
 
@@ -211,10 +204,9 @@ const BitmapPalette& Octree::GetPalette()
 
 sal_uInt16 Octree::GetBestPaletteIndex(const BitmapColor& rColor)
 {
-    mpColor = &rColor;
     mnPalIndex = 65535;
     mnLevel = 0;
-    GetPalIndex(pTree.get());
+    GetPalIndex(pTree.get(), rColor);
     return mnPalIndex;
 }
 

Reply via email to