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.

Reviewed-on: https://gerrit.libreoffice.org/76351
Reviewed-by: Michael Stahl <Michael.Stahl@cib.de>
Tested-by: Jenkins
(cherry picked from commit 20e7632919f4bbefabb4190d10ee79c65b89dca7)
Reviewed-on: https://gerrit.libreoffice.org/76798
Reviewed-by: Xisco FaulĂ­ <xiscofauli@libreoffice.org>
Signed-off-by: Xisco Fauli <xiscofauli@libreoffice.org>

Change-Id: I9a60d6b3311278d7391d676010c31c862189fd08
Reviewed-on: https://gerrit.libreoffice.org/76857
Tested-by: Jenkins
Reviewed-by: Michael Stahl <Michael.Stahl@cib.de>
diff --git a/sw/source/core/inc/UndoCore.hxx b/sw/source/core/inc/UndoCore.hxx
index d5b7c25..1627775 100644
--- a/sw/source/core/inc/UndoCore.hxx
+++ b/sw/source/core/inc/UndoCore.hxx
@@ -189,8 +189,8 @@ public:
class SwUndoSetFlyFormat : public SwUndo, public SwClient
{
    SwFrameFormat* pFrameFormat;                  // saved FlyFormat
    SwFrameFormat* const pOldFormat;
    SwFrameFormat* pNewFormat;
    const OUString m_DerivedFromFormatName;
    const OUString m_NewFormatName;
    std::unique_ptr<SfxItemSet> pItemSet;               // the re-/ set attributes
    sal_uLong nOldNode, nNewNode;
    sal_Int32 nOldContent, nNewContent;
diff --git a/sw/source/core/undo/undobj1.cxx b/sw/source/core/undo/undobj1.cxx
index 763b2ed..eab00fcd 100644
--- a/sw/source/core/undo/undobj1.cxx
+++ b/sw/source/core/undo/undobj1.cxx
@@ -456,7 +456,8 @@ void SwUndoDelLayFormat::RedoForRollback()

SwUndoSetFlyFormat::SwUndoSetFlyFormat( SwFrameFormat& rFlyFormat, SwFrameFormat& rNewFrameFormat )
    : SwUndo( SwUndoId::SETFLYFRMFMT, rFlyFormat.GetDoc() ), SwClient( &rFlyFormat ), pFrameFormat( &rFlyFormat ),
    pOldFormat( static_cast<SwFrameFormat*>(rFlyFormat.DerivedFrom()) ), pNewFormat( &rNewFrameFormat ),
    m_DerivedFromFormatName( rFlyFormat.IsDefault() ? "" : rFlyFormat.DerivedFrom()->GetName() ),
    m_NewFormatName( rNewFrameFormat.GetName() ),
    pItemSet( new SfxItemSet( *rFlyFormat.GetAttrSet().GetPool(),
                                rFlyFormat.GetAttrSet().GetRanges() )),
    nOldNode( 0 ), nNewNode( 0 ),
@@ -469,8 +470,7 @@ SwRewriter SwUndoSetFlyFormat::GetRewriter() const
{
    SwRewriter aRewriter;

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

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

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

        if( pFrameFormat->DerivedFrom() != pOldFormat )
            pFrameFormat->SetDerivedFrom( pOldFormat );
        if( pFrameFormat->DerivedFrom() != pDerivedFromFrameFormat )
            pFrameFormat->SetDerivedFrom( pDerivedFromFrameFormat );

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

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

        if( bAnchorChgd )
        {
            SwFormatAnchor aNewAnchor( nNewAnchorTyp );
            GetAnchor( aNewAnchor, nNewNode, nNewContent );
            SfxItemSet aSet( rDoc.GetAttrPool(), aFrameFormatSetRange );
            aSet.Put( aNewAnchor );
            rDoc.SetFrameFormatToFly( *pFrameFormat, *pNewFormat, &aSet );
            rDoc.SetFrameFormatToFly( *pFrameFormat, *pNewFrameFormat, &aSet );
        }
        else
            rDoc.SetFrameFormatToFly( *pFrameFormat, *pNewFormat );
            rDoc.SetFrameFormatToFly( *pFrameFormat, *pNewFrameFormat );

        rContext.SetSelections(pFrameFormat, nullptr);
    }