tdf#158801 sw floattable: fix crash with headers and interactive editing

Regression from commit ce2fc5eb29b4e252993b549dee002fa8948c8386
(tdf#158341 sw floattable: fix layout loop when fly is below the body
frame, 2023-11-29), open the bugdoc, add an empty paragraph at the
start, Writer layout crashes. The immediate problem is that
SwTabFrame::MakeAll() assumes that in case HasFollowFlowLine() is true,
then GetFollow()->GetFirstNonHeadlineRow() is always non-nullptr,
similar to the situation in commit
223d2fac61e061478721a7a4a89b1362f5037d8f (sw floattable: fix crash by
trying harder to split tables, 2023-11-22).

The deeper problem is that the bugdoc has a repeated table header row,
the fly frame temporarily gets shifted down, so nominally the header
doesn't fit anymore, and this leads to a modification of the doc model,
which creates inconsistency: the model now says we have no header rows
but the layout still contains table row frames where the header bit is
true. This is problematic in theory, but in practice caused no problem
so far.

Fix the problem by disabling this mechanism for floating tables: trying
to have a table header that doesn't fit the table is asking for trouble
anyway, and this way at least we have a layout that is consistent with
the model, which also avoids the crash, now that nobody violates the
"HasFollowFlowLine -> follow's FirstNonHeadlineRow != nullptr"
invariant.

Also extend the layout dump, so it's easier to see when the master's
flag and the follow's row list gets out of sync.

Change-Id: I52b51f6d86ab4e0bab560f892e9cceb183aecd5f
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/164136
Reviewed-by: Miklos Vajna <[email protected]>
Tested-by: Jenkins
diff --git a/sw/qa/core/layout/data/floattable-header.docx b/sw/qa/core/layout/data/floattable-header.docx
new file mode 100644
index 0000000..baddd36
--- /dev/null
+++ b/sw/qa/core/layout/data/floattable-header.docx
Binary files differ
diff --git a/sw/qa/core/layout/tabfrm.cxx b/sw/qa/core/layout/tabfrm.cxx
index e0d099c..9bfbd1b 100644
--- a/sw/qa/core/layout/tabfrm.cxx
+++ b/sw/qa/core/layout/tabfrm.cxx
@@ -17,6 +17,8 @@
#include <anchoredobject.hxx>
#include <flyfrm.hxx>
#include <flyfrms.hxx>
#include <docsh.hxx>
#include <wrtsh.hxx>

namespace
{
@@ -235,6 +237,25 @@ CPPUNIT_TEST_FIXTURE(Test, testSplitFlyWrappedByTableNested)
    CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), pDoc->GetTableFrameFormats()->GetFormatCount());
    CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), pDoc->GetSpzFrameFormats()->GetFormatCount());
}

CPPUNIT_TEST_FIXTURE(Test, testSplitFlyHeader)
{
    // Given a document with 8 pages: a first page ending in a manual page break, then a multi-page
    // floating table on pages 2..8:
    createSwDoc("floattable-header.docx");
    CPPUNIT_ASSERT_EQUAL(8, getPages());

    // When creating a new paragraph at doc start:
    SwDocShell* pDocShell = getSwDocShell();
    SwWrtShell* pWrtShell = pDocShell->GetWrtShell();
    pWrtShell->SttEndDoc(/*bStt=*/true);
    pWrtShell->SplitNode();
    // Without the accompanying fix in place, this test would have crashed here.
    pWrtShell->CalcLayout();

    // Then make sure we get one more page, since the first page is now 2 pages:
    CPPUNIT_ASSERT_EQUAL(9, getPages());
}
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sw/source/core/layout/tabfrm.cxx b/sw/source/core/layout/tabfrm.cxx
index 9642e85..10d1b16 100644
--- a/sw/source/core/layout/tabfrm.cxx
+++ b/sw/source/core/layout/tabfrm.cxx
@@ -1165,7 +1165,13 @@ bool SwTabFrame::Split(const SwTwips nCutPos, bool bTryToSplit,
        OSL_ENSURE( !GetIndPrev(), "Table is supposed to be at beginning" );
        if ( !IsInSct() )
        {
            m_pTable->SetRowsToRepeat(0);
            // This would mean the layout modifies the doc model, so RowsToRepeat drops to 0 while
            // there are existing row frames with RepeatedHeadline == true. Avoid this at least
            // inside split flys, it would lead to a crash in SwTabFrame::MakeAll().
            if (!pFly || !pFly->IsFlySplitAllowed())
            {
                m_pTable->SetRowsToRepeat(0);
            }
            return false;
        }
        else
@@ -6377,6 +6383,10 @@ void SwTabFrame::dumpAsXml(xmlTextWriterPtr writer) const
{
    (void)xmlTextWriterStartElement(writer, reinterpret_cast<const xmlChar*>("tab"));
    SwFrame::dumpAsXmlAttributes( writer );

    (void)xmlTextWriterWriteAttribute(writer, BAD_CAST("has-follow-flow-line"),
                                      BAD_CAST(OString::boolean(m_bHasFollowFlowLine).getStr()));

    if ( HasFollow() )
        (void)xmlTextWriterWriteFormatAttribute( writer, BAD_CAST( "follow" ), "%" SAL_PRIuUINT32, GetFollow()->GetFrameId() );