sw/source/core/inc/txtfrm.hxx    |    3 ++-
 sw/source/core/text/frmcrsr.cxx  |    2 +-
 sw/source/core/text/txtcache.cxx |   24 ++++++++++++++----------
 sw/source/core/text/txtcache.hxx |   19 ++++++++-----------
 sw/source/core/text/txtfrm.cxx   |   21 +++++++++------------
 sw/source/core/text/txtftn.cxx   |    2 +-
 6 files changed, 35 insertions(+), 36 deletions(-)

New commits:
commit 4e469136136aed27df73a77080262b26b85f37f1
Author:     Caolán McNamara <caolan.mcnam...@collabora.com>
AuthorDate: Thu Aug 7 21:49:54 2025 +0100
Commit:     Caolán McNamara <caolan.mcnam...@collabora.com>
CommitDate: Fri Aug 8 09:46:03 2025 +0200

    cid#1660822 Different smart pointers managing same raw pointer
    
    and
    
    cid#1660800 Different smart pointers managing same raw pointer
    
    Change-Id: Ifea613279119ab411414b250a029e0e231b5def8
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/189141
    Tested-by: Jenkins
    Reviewed-by: Caolán McNamara <caolan.mcnam...@collabora.com>

diff --git a/sw/source/core/inc/txtfrm.hxx b/sw/source/core/inc/txtfrm.hxx
index 2190dd0f34ff..bb3a8bc5015c 100644
--- a/sw/source/core/inc/txtfrm.hxx
+++ b/sw/source/core/inc/txtfrm.hxx
@@ -300,7 +300,8 @@ class SW_DLLPUBLIC SwTextFrame final : public SwContentFrame
 
     bool GetDropRect_( SwRect &rRect ) const;
 
-    void SetPara(SwParaPortion *pNew, bool bDelete);
+    // returns the old SwParaPortion, if present
+    std::unique_ptr<SwParaPortion> SetPara(std::unique_ptr<SwParaPortion> 
xNew);
 
     bool IsFootnoteNumFrame_() const;
 
diff --git a/sw/source/core/text/frmcrsr.cxx b/sw/source/core/text/frmcrsr.cxx
index 2324e99daccf..28a5db42b606 100644
--- a/sw/source/core/text/frmcrsr.cxx
+++ b/sw/source/core/text/frmcrsr.cxx
@@ -129,7 +129,7 @@ bool sw_ChangeOffset(SwTextFrame* pFrame, TextFrameIndex 
nNew)
                     nNew = TextFrameIndex(0);
                 }
                 pFrame->SetOffset( nNew );
-                pFrame->SetPara(nullptr, true);
+                pFrame->SetPara(nullptr);
                 pFrame->GetFormatted();
                 if( pFrame->getFrameArea().HasArea() )
                     pFrame->getRootFrame()->GetCurrShell()->InvalidateWindows( 
pFrame->getFrameArea() );
diff --git a/sw/source/core/text/txtcache.cxx b/sw/source/core/text/txtcache.cxx
index 188fa7bec697..8a7470030f29 100644
--- a/sw/source/core/text/txtcache.cxx
+++ b/sw/source/core/text/txtcache.cxx
@@ -25,9 +25,9 @@
 #include <osl/diagnose.h>
 #include <view.hxx>
 
-SwTextLine::SwTextLine( SwTextFrame const *pFrame, 
std::unique_ptr<SwParaPortion> pNew ) :
-    SwCacheObj( static_cast<void const *>(pFrame) ),
-    m_pLine( std::move(pNew) )
+SwTextLine::SwTextLine(SwTextFrame const *pFrame, 
std::unique_ptr<SwParaPortion> xNew)
+    : SwCacheObj(static_cast<void const *>(pFrame))
+    , m_xLine(std::move(xNew) )
 {
 }
 
@@ -58,7 +58,7 @@ SwParaPortion *SwTextLineAccess::GetPara()
         const_cast<SwTextFrame *>(static_cast<SwTextFrame const 
*>(m_pOwner))->SetCacheIdx( pRet->GetCachePos() );
     }
     if ( !pRet->GetPara() )
-        pRet->SetPara( new SwParaPortion, true/*bDelete*/ );
+        pRet->SetPara(std::make_unique<SwParaPortion>());
     return pRet->GetPara();
 }
 
@@ -110,7 +110,7 @@ void SwTextFrame::ClearPara()
                                         Get( this, GetCacheIdx(), false ));
         if ( pTextLine )
         {
-            pTextLine->SetPara( nullptr, true/*bDelete*/ );
+            pTextLine->SetPara(nullptr);
         }
         else
             mnCacheIndex = USHRT_MAX;
@@ -126,8 +126,10 @@ void SwTextFrame::RemoveFromCache()
     }
 }
 
-void SwTextFrame::SetPara( SwParaPortion *pNew, bool bDelete )
+std::unique_ptr<SwParaPortion> 
SwTextFrame::SetPara(std::unique_ptr<SwParaPortion> xNew)
 {
+    std::unique_ptr<SwParaPortion> xOld;
+
     if ( GetCacheIdx() != USHRT_MAX )
     {
         // Only change the information, the CacheObj stays there
@@ -135,17 +137,17 @@ void SwTextFrame::SetPara( SwParaPortion *pNew, bool 
bDelete )
                                         Get( this, GetCacheIdx(), false ));
         if ( pTextLine )
         {
-            pTextLine->SetPara( pNew, bDelete );
+            xOld = pTextLine->SetPara(std::move(xNew));
         }
         else
         {
-            OSL_ENSURE( !pNew, "+SetPara: Losing SwParaPortion" );
+            OSL_ENSURE( !xNew, "+SetPara: Losing SwParaPortion" );
             mnCacheIndex = USHRT_MAX;
         }
     }
-    else if ( pNew )
+    else if (xNew)
     {   // Insert a new one
-        SwTextLine *pTextLine = new SwTextLine( this, 
std::unique_ptr<SwParaPortion>(pNew) );
+        SwTextLine *pTextLine = new SwTextLine(this, std::move(xNew));
         if (SwTextFrame::GetTextCache()->Insert(pTextLine, false))
             mnCacheIndex = pTextLine->GetCachePos();
         else
@@ -153,6 +155,8 @@ void SwTextFrame::SetPara( SwParaPortion *pNew, bool 
bDelete )
             OSL_FAIL( "+SetPara: InsertCache failed." );
         }
     }
+
+    return xOld;
 }
 
 /** Prevent the SwParaPortions of the *visible* paragraphs from being deleted;
diff --git a/sw/source/core/text/txtcache.hxx b/sw/source/core/text/txtcache.hxx
index ee18f14c3b0c..68f907de4597 100644
--- a/sw/source/core/text/txtcache.hxx
+++ b/sw/source/core/text/txtcache.hxx
@@ -27,25 +27,22 @@ class SwTextFrame;
 
 class SwTextLine : public SwCacheObj
 {
-    std::unique_ptr<SwParaPortion> m_pLine;
+    std::unique_ptr<SwParaPortion> m_xLine;
 
     virtual void UpdateCachePos() override;
 
 public:
-    SwTextLine(SwTextFrame const* pFrame, std::unique_ptr<SwParaPortion> pNew 
= nullptr);
+    SwTextLine(SwTextFrame const* pFrame, std::unique_ptr<SwParaPortion> xNew 
= nullptr);
     virtual ~SwTextLine() override;
 
-    SwParaPortion* GetPara() { return m_pLine.get(); }
-    const SwParaPortion* GetPara() const { return m_pLine.get(); }
+    SwParaPortion* GetPara() { return m_xLine.get(); }
+    const SwParaPortion* GetPara() const { return m_xLine.get(); }
 
-    void SetPara(SwParaPortion* pNew, bool bDelete)
+    std::unique_ptr<SwParaPortion> SetPara(std::unique_ptr<SwParaPortion> xNew)
     {
-        if (!bDelete)
-        {
-            // coverity[leaked_storage] - intentional, ownership transferred
-            (void)m_pLine.release();
-        }
-        m_pLine.reset(pNew);
+        std::unique_ptr<SwParaPortion> xOld = std::move(m_xLine);
+        m_xLine = std::move(xNew);
+        return xOld;
     }
 };
 
diff --git a/sw/source/core/text/txtfrm.cxx b/sw/source/core/text/txtfrm.cxx
index 6e6687faabc8..d19f8f5a4c38 100644
--- a/sw/source/core/text/txtfrm.cxx
+++ b/sw/source/core/text/txtfrm.cxx
@@ -3387,7 +3387,7 @@ bool SwTextFrame::Prepare( const PrepareHint ePrep, const 
void* pVoid,
 class SwTestFormat
 {
     SwTextFrame *pFrame;
-    SwParaPortion *pOldPara;
+    std::unique_ptr<SwParaPortion> xOldPara;
     SwRect aOldFrame, aOldPrt;
 public:
     SwTestFormat( SwTextFrame* pTextFrame, const SwFrame* pPrv, SwTwips 
nMaxHeight );
@@ -3439,8 +3439,7 @@ SwTestFormat::SwTestFormat( SwTextFrame* pTextFrame, 
const SwFrame* pPre, SwTwip
         aRectFnSet.SetWidth( aPrt, aRectFnSet.GetWidth(pFrame->getFrameArea()) 
- ( rAttrs.CalcLeft( pFrame ) + rAttrs.CalcRight( pFrame ) ) );
     }
 
-    pOldPara = pFrame->HasPara() ? pFrame->GetPara() : nullptr;
-    pFrame->SetPara( new SwParaPortion(), false );
+    xOldPara = pFrame->SetPara(std::make_unique<SwParaPortion>());
     OSL_ENSURE( ! pFrame->IsSwapped(), "A frame is swapped before Format_" );
 
     if ( pFrame->IsVertical() )
@@ -3469,7 +3468,7 @@ SwTestFormat::~SwTestFormat()
         aPrt.setSwRect(aOldPrt);
     }
 
-    pFrame->SetPara(pOldPara, true);
+    pFrame->SetPara(std::move(xOldPara));
 }
 
 bool SwTextFrame::TestFormat( const SwFrame* pPrv, SwTwips &rMaxHeight, bool 
&bSplit )
@@ -3666,9 +3665,8 @@ SwTwips SwTextFrame::CalcFitToContent()
     if ( IsLocked() )
         return getFramePrintArea().Width();
 
-    SwParaPortion* pOldPara = GetPara();
-    SwParaPortion *pDummy = new SwParaPortion();
-    SetPara( pDummy, false );
+    //Swap old para for a dummy
+    std::unique_ptr<SwParaPortion> xOldPara = 
SetPara(std::make_unique<SwParaPortion>());
     const SwPageFrame* pPage = FindPageFrame();
 
     const Point   aOldFramePos   = getFrameArea().Pos();
@@ -3721,7 +3719,8 @@ SwTwips SwTextFrame::CalcFitToContent()
         aPrt.Width( nOldPrtWidth );
     }
 
-    SetPara(pOldPara, true);
+    //restore original para
+    SetPara(std::move(xOldPara));
 
     // tdf#164932 handle numbering list offset
     const SwTextNode* pTextNode( GetTextNodeForParaProps() );
@@ -3772,9 +3771,7 @@ void SwTextFrame::CalcAdditionalFirstLineOffset()
         return;
 
     // keep current paragraph portion and apply dummy paragraph portion
-    SwParaPortion* pOldPara = GetPara();
-    SwParaPortion *pDummy = new SwParaPortion();
-    SetPara( pDummy, false );
+    std::unique_ptr<SwParaPortion> xOldPara = 
SetPara(std::make_unique<SwParaPortion>());
 
     // lock paragraph
     TextFrameLockGuard aLock( this );
@@ -3814,7 +3811,7 @@ void SwTextFrame::CalcAdditionalFirstLineOffset()
     }
 
     // restore paragraph portion
-    SetPara(pOldPara, true);
+    SetPara(std::move(xOldPara));
 }
 
 /**
diff --git a/sw/source/core/text/txtftn.cxx b/sw/source/core/text/txtftn.cxx
index a941e5eca2d8..8a6c1613a774 100644
--- a/sw/source/core/text/txtftn.cxx
+++ b/sw/source/core/text/txtftn.cxx
@@ -166,7 +166,7 @@ bool SwTextFrame::CalcPrepFootnoteAdjust()
             SwTextFormatInfo aInf( getRootFrame()->GetCurrShell()->GetOut(), 
this );
             SwTextFormatter aLine( this, &aInf );
             aLine.TruncLines();
-            SetPara(nullptr, true); // May be deleted!
+            SetPara(nullptr); // May be deleted!
             ResetPreps();
             return false;
         }

Reply via email to