tdf#126168: sw undo/redo crash fixed

After undo of style creation we have a dangling pointers to
deleted style in SwUndoSetFlyFormat. So instead of keeping
pointers to SwFrameFormat which can be invalidated, we could
keep style name and find style by name.

Change-Id: I9a60d6b3311278d7391d676010c31c862189fd08
Reviewed-on: https://gerrit.libreoffice.org/76351
Reviewed-by: Michael Stahl <Michael.Stahl@cib.de>
Tested-by: Jenkins
diff --git a/sw/source/core/inc/UndoCore.hxx b/sw/source/core/inc/UndoCore.hxx
index 002f2eb..1699c57 100644
--- a/sw/source/core/inc/UndoCore.hxx
+++ b/sw/source/core/inc/UndoCore.hxx
@@ -188,8 +188,8 @@ public:
class SwUndoSetFlyFormat : public SwUndo, public SwClient
{
    SwFrameFormat* m_pFrameFormat;                  // saved FlyFormat
    SwFrameFormat* const m_pOldFormat;
    SwFrameFormat* m_pNewFormat;
    const OUString m_DerivedFromFormatName;
    const OUString m_NewFormatName;
    std::unique_ptr<SfxItemSet> m_pItemSet;               // the re-/ set attributes
    sal_uLong m_nOldNode, m_nNewNode;
    sal_Int32 m_nOldContent, m_nNewContent;
diff --git a/sw/source/core/undo/undobj1.cxx b/sw/source/core/undo/undobj1.cxx
index f88611b..6a2387f 100644
--- a/sw/source/core/undo/undobj1.cxx
+++ b/sw/source/core/undo/undobj1.cxx
@@ -457,7 +457,8 @@ void SwUndoDelLayFormat::RedoForRollback()

SwUndoSetFlyFormat::SwUndoSetFlyFormat( SwFrameFormat& rFlyFormat, SwFrameFormat& rNewFrameFormat )
    : SwUndo( SwUndoId::SETFLYFRMFMT, rFlyFormat.GetDoc() ), SwClient( &rFlyFormat ), m_pFrameFormat( &rFlyFormat ),
    m_pOldFormat( static_cast<SwFrameFormat*>(rFlyFormat.DerivedFrom()) ), m_pNewFormat( &rNewFrameFormat ),
    m_DerivedFromFormatName( rFlyFormat.IsDefault() ? "" : rFlyFormat.DerivedFrom()->GetName() ),
    m_NewFormatName( rNewFrameFormat.GetName() ),
    m_pItemSet( new SfxItemSet( *rFlyFormat.GetAttrSet().GetPool(),
                                rFlyFormat.GetAttrSet().GetRanges() )),
    m_nOldNode( 0 ), m_nNewNode( 0 ),
@@ -470,8 +471,7 @@ SwRewriter SwUndoSetFlyFormat::GetRewriter() const
{
    SwRewriter aRewriter;

    if (m_pNewFormat)
        aRewriter.AddRule(UndoArg1, m_pNewFormat->GetName());
    aRewriter.AddRule(UndoArg1, m_NewFormatName);

    return aRewriter;
}
@@ -532,13 +532,14 @@ void SwUndoSetFlyFormat::UndoImpl(::sw::UndoRedoContext & rContext)
    SwDoc & rDoc = rContext.GetDoc();

    // Is the new Format still existent?
    if (rDoc.GetFrameFormats()->IsAlive(m_pOldFormat))
    SwFrameFormat* pDerivedFromFrameFormat = rDoc.FindFrameFormatByName(m_DerivedFromFormatName);
    if (pDerivedFromFrameFormat)
    {
        if( m_bAnchorChanged )
            m_pFrameFormat->DelFrames();

        if( m_pFrameFormat->DerivedFrom() != m_pOldFormat )
            m_pFrameFormat->SetDerivedFrom( m_pOldFormat );
        if( m_pFrameFormat->DerivedFrom() != pDerivedFromFrameFormat)
            m_pFrameFormat->SetDerivedFrom(pDerivedFromFrameFormat);

        SfxItemIter aIter( *m_pItemSet );
        const SfxPoolItem* pItem = aIter.GetCurItem();
@@ -606,19 +607,19 @@ void SwUndoSetFlyFormat::RedoImpl(::sw::UndoRedoContext & rContext)
    SwDoc & rDoc = rContext.GetDoc();

    // Is the new Format still existent?
    if (rDoc.GetFrameFormats()->IsAlive(m_pNewFormat))
    SwFrameFormat* pNewFrameFormat = rDoc.FindFrameFormatByName(m_NewFormatName);
    if (pNewFrameFormat)
    {

        if( m_bAnchorChanged )
        {
            SwFormatAnchor aNewAnchor( m_nNewAnchorType );
            GetAnchor( aNewAnchor, m_nNewNode, m_nNewContent );
            SfxItemSet aSet( rDoc.GetAttrPool(), aFrameFormatSetRange );
            aSet.Put( aNewAnchor );
            rDoc.SetFrameFormatToFly( *m_pFrameFormat, *m_pNewFormat, &aSet );
            rDoc.SetFrameFormatToFly( *m_pFrameFormat, *pNewFrameFormat, &aSet );
        }
        else
            rDoc.SetFrameFormatToFly( *m_pFrameFormat, *m_pNewFormat );
            rDoc.SetFrameFormatToFly( *m_pFrameFormat, *pNewFrameFormat);

        rContext.SetSelections(m_pFrameFormat, nullptr);
    }