tdf#149184 DOCX: fix crash removing footer, then saving to doc

When openeing the simplefooter.docx, after removing the footer and
exporting to .doc, LibreOffice crashes. This regression was
introduced with 88e6a1bfeac86e0c89d2ff08c908c2b5ae061177 which is
titled: "DOCX: export hidden (shared) headers/footers".

The current patch fixes this problem by checking to see if the header
or footer text is there or not.

A unit test is added to avoid this problem in the future. One can run
the test with:

    make CPPUNIT_TEST_NAME="testTdf149184" -sr CppunitTest_sw_uiwriter7

Change-Id: I5586561677b3c490e49b4b10bd987aecdf3fc134
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/134684
Tested-by: Jenkins
Reviewed-by: Miklos Vajna <vmiklos@collabora.com>
diff --git a/sw/qa/extras/uiwriter/data/simplefooter.docx b/sw/qa/extras/uiwriter/data/simplefooter.docx
new file mode 100644
index 0000000..006c85a
--- /dev/null
+++ b/sw/qa/extras/uiwriter/data/simplefooter.docx
Binary files differ
diff --git a/sw/qa/extras/uiwriter/uiwriter7.cxx b/sw/qa/extras/uiwriter/uiwriter7.cxx
index 3815713..0cb70e9 100644
--- a/sw/qa/extras/uiwriter/uiwriter7.cxx
+++ b/sw/qa/extras/uiwriter/uiwriter7.cxx
@@ -24,6 +24,7 @@
#include <AnnotationWin.hxx>
#include <com/sun/star/awt/FontUnderline.hpp>

#include <svx/hdft.hxx>
#include <svx/svdpage.hxx>
#include <svx/svdview.hxx>
#include <svx/svxids.hrc>
@@ -2751,6 +2752,29 @@ CPPUNIT_TEST_FIXTURE(SwUiWriterTest7, testTdf117225)
    CPPUNIT_ASSERT_EQUAL(nExpected, nActual);
}

CPPUNIT_TEST_FIXTURE(SwUiWriterTest7, testTdf149184)
{
    SwDoc* pDoc = createSwDoc(DATA_DIRECTORY, "simplefooter.docx");
    SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();

    // Removing the footer for all styles
    pWrtShell->ChangeHeaderOrFooter(u"", false, false, false);

    // export to simplefooter.doc
    uno::Reference<frame::XStorable> xStorable(mxComponent, uno::UNO_QUERY);
    uno::Sequence<beans::PropertyValue> aStoreProps = comphelper::InitPropertySequence({
        { "FilterName", uno::Any(OUString("MS Word 97")) },
    });
    utl::TempFile aTempFile;
    aTempFile.EnableKillingFile();

    // Without the fix in place, the test fails with:
    // [CUT] sw_uiwriter7
    // Segmentation fault (core dumped)
    // [_RUN_____] testTdf149184::TestBody
    xStorable->storeToURL(aTempFile.GetURL(), aStoreProps);
}

CPPUNIT_PLUGIN_IMPLEMENT();

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sw/source/filter/ww8/wrtw8sty.cxx b/sw/source/filter/ww8/wrtw8sty.cxx
index 44d437d..6e90534 100644
--- a/sw/source/filter/ww8/wrtw8sty.cxx
+++ b/sw/source/filter/ww8/wrtw8sty.cxx
@@ -2080,6 +2080,10 @@ void MSWordExportBase::WriteHeaderFooterText( const SwFormat& rFormat, bool bHea
        m_bHasHdr = true;
        const SwFormatHeader& rHd = rFormat.GetHeader();
        OSL_ENSURE( rHd.GetHeaderFormat(), "Header text is not here" );

        if ( !rHd.GetHeaderFormat() )
            return;

        pContent = &rHd.GetHeaderFormat()->GetContent();
    }
    else
@@ -2087,6 +2091,10 @@ void MSWordExportBase::WriteHeaderFooterText( const SwFormat& rFormat, bool bHea
        m_bHasFtr = true;
        const SwFormatFooter& rFt = rFormat.GetFooter();
        OSL_ENSURE( rFt.GetFooterFormat(), "Footer text is not here" );

        if ( !rFt.GetFooterFormat() )
            return;

        pContent = &rFt.GetFooterFormat()->GetContent();
    }