sw/source/filter/ww8/ww8par2.cxx |   45 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

New commits:
commit 73631b1bf52a2e6dd8a85d6fef36d754c15dcf82
Author:     Caolán McNamara <caol...@redhat.com>
AuthorDate: Sun Jan 2 20:22:24 2022 +0000
Commit:     Caolán McNamara <caol...@redhat.com>
CommitDate: Sun Jan 2 22:57:41 2022 +0100

    ofz#43052 crash in WW8TabDesc::MergeCells
    
    Change-Id: I4d7acc006458598ad43033fa8e35b2f422d76ff5
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/127876
    Tested-by: Jenkins
    Reviewed-by: Caolán McNamara <caol...@redhat.com>

diff --git a/sw/source/filter/ww8/ww8par2.cxx b/sw/source/filter/ww8/ww8par2.cxx
index a50e0b1d280c..e5037535a824 100644
--- a/sw/source/filter/ww8/ww8par2.cxx
+++ b/sw/source/filter/ww8/ww8par2.cxx
@@ -2791,6 +2791,46 @@ void WW8TabDesc::MoveOutsideTable()
         *m_pIo->m_pPaM->GetPoint() = *m_xTmpPos->GetPoint();
 }
 
+namespace
+{
+    class SwTableNodeListener final : public SwClient
+    {
+    private:
+        SwModify* m_pModify;
+
+        virtual void SwClientNotify(const SwModify&, const SfxHint& rHint) 
override
+        {
+            if (rHint.GetId() != SfxHintId::SwLegacyModify)
+                return;
+            auto pLegacy = static_cast<const sw::LegacyModifyHint*>(&rHint);
+            if (pLegacy->GetWhich() == RES_OBJECTDYING)
+            {
+                m_pModify->Remove(this);
+                m_pModify = nullptr;
+            }
+        }
+
+    public:
+        SwTableNodeListener(SwModify* pModify)
+            : m_pModify(pModify)
+        {
+            m_pModify->Add(this);
+        }
+
+        bool WasDeleted() const
+        {
+            return !m_pModify;
+        }
+
+        virtual ~SwTableNodeListener() override
+        {
+            if (!m_pModify)
+                return;
+            m_pModify->Remove(this);
+        }
+    };
+}
+
 void WW8TabDesc::FinishSwTable()
 {
     m_pIo->m_xRedlineStack->closeall(*m_pIo->m_pPaM->GetPoint());
@@ -2800,6 +2840,8 @@ void WW8TabDesc::FinishSwTable()
     std::shared_ptr<SwUnoCursor> xLastAnchorCursor(m_pIo->m_pLastAnchorPos ? 
m_pIo->m_rDoc.CreateUnoCursor(*m_pIo->m_pLastAnchorPos) : nullptr);
     m_pIo->m_pLastAnchorPos.reset();
 
+    SwTableNode* pTableNode = m_pTable->GetTableNode();
+    SwTableNodeListener aListener(pTableNode);
     m_pIo->m_xRedlineStack = std::move(mxOldRedlineStack);
 
     if (xLastAnchorCursor)
@@ -2818,6 +2860,9 @@ void WW8TabDesc::FinishSwTable()
 
     m_pIo->m_aInsertedTables.InsertTable(*m_pTableNd, *m_pIo->m_pPaM);
 
+    if (aListener.WasDeleted())
+        return;
+
     MergeCells();
 
     // if needed group cells together that should be merged

Reply via email to