tdf#132596 writerfilter: fix paste of RTF with header/footer

The problem is that writerfilter inserts the content of header/footer
into the body text, but then DomainMapper_Impl::PopPageHeaderFooter()
calls RemoveLastParagraph() and deletes a body paragraph containing a
fieldmark, and then in Undo some SwHistoryTextFieldmark can't find it,
and since ffb26b81e1c7ff1d64959200247bb2edd5a569da this crashes.

This is because of the borked error handling in
DomainMapper_Impl::PushPageHeaderFooter(); the
m_xBodyText->createTextCursorByRange() call throws an exception that is
swallowed, so the m_aTextAppendStack doesn't have an entry containing
the position in the header.

To fix the error handling, set m_bDiscardHeaderFooter = false only when
everything was successful.

Also fix the call to be xText->createTextCursorByRange instead
(this is a regression from 232ad2f2588beff50cb5c1f3b689c581ba317583).

Then it turns out that Undo still crashes, because sw can't Undo
changes of header/footer content, so just return early unless it's
a new document.

Change-Id: Ie5aeb45447c5fbd4b3ae15c4cffb9800583a6d1d
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/95797
Tested-by: Jenkins
Reviewed-by: Michael Stahl <michael.stahl@cib.de>
(cherry picked from commit 7a5afa492a7233b36165b3aba2774a1b03cdefc6)
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/95823
diff --git a/writerfilter/source/dmapper/DomainMapper_Impl.cxx b/writerfilter/source/dmapper/DomainMapper_Impl.cxx
index 155105c..abde5e06 100644
--- a/writerfilter/source/dmapper/DomainMapper_Impl.cxx
+++ b/writerfilter/source/dmapper/DomainMapper_Impl.cxx
@@ -2370,6 +2370,7 @@ void DomainMapper_Impl::PushPageHeaderFooter(bool bHeader, SectionPropertyMap::P
    const PropertyIds ePropTextLeft = bHeader? PROP_HEADER_TEXT_LEFT: PROP_FOOTER_TEXT_LEFT;
    const PropertyIds ePropText = bHeader? PROP_HEADER_TEXT: PROP_FOOTER_TEXT;

    m_bDiscardHeaderFooter = true;
    m_eInHeaderFooterImport
        = bHeader ? HeaderFooterImportState::header : HeaderFooterImportState::footer;

@@ -2383,6 +2384,11 @@ void DomainMapper_Impl::PushPageHeaderFooter(bool bHeader, SectionPropertyMap::P
        // content is not copied from the previous section
        pSectionContext->ClearHeaderFooterLinkToPrevious(bHeader, eType);

        if (!m_bIsNewDoc)
        {
            return; // TODO sw cannot Undo insert header/footer without crashing
        }

        uno::Reference< beans::XPropertySet > xPageStyle =
            pSectionContext->GetPageStyle(
                *this,
@@ -2410,15 +2416,15 @@ void DomainMapper_Impl::PushPageHeaderFooter(bool bHeader, SectionPropertyMap::P
                xPageStyle->getPropertyValue(getPropertyName(bLeft? ePropTextLeft: ePropText)) >>= xText;

                m_aTextAppendStack.push(TextAppendContext(uno::Reference< text::XTextAppend >(xText, uno::UNO_QUERY_THROW),
                            m_bIsNewDoc? uno::Reference<text::XTextCursor>(): m_xBodyText->createTextCursorByRange(xText->getStart())));
            }
            else
            {
                m_bDiscardHeaderFooter = true;
                    m_bIsNewDoc
                        ? uno::Reference<text::XTextCursor>()
                        : xText->createTextCursorByRange(xText->getStart())));
                m_bDiscardHeaderFooter = false; // set only on success!
            }
        }
        catch( const uno::Exception& )
        {
            DBG_UNHANDLED_EXCEPTION("writerfilter.dmapper");
        }
    }
}