tdf#144304 Fix crashes from "speedup dynamic_cast to SwTextFrame"

Also preemptively adjust cases where FindNextCnt(true) or
FindPrevCnt() can be null.

Regressions from 426930d0c4bd6f782a04a92e8a36e92cd65e186f.

Change-Id: Idb91464ecd285f6f5cf2e399c00faa2dfbbd5601
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/121662
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
diff --git a/sw/source/core/docnode/ndsect.cxx b/sw/source/core/docnode/ndsect.cxx
index ff7946e..d5c9db0 100644
--- a/sw/source/core/docnode/ndsect.cxx
+++ b/sw/source/core/docnode/ndsect.cxx
@@ -1121,9 +1121,11 @@
                    if ( pViewShell && pViewShell->GetLayout() &&
                         pViewShell->GetLayout()->IsAnyShellAccessible() )
                    {
                        auto pNext = pNew->FindNextCnt( true );
                        auto pPrev = pNew->FindPrevCnt();
                        pViewShell->InvalidateAccessibleParaFlowRelation(
                            pNew->FindNextCnt( true )->DynCastTextFrame(),
                            pNew->FindPrevCnt()->DynCastTextFrame() );
                            pNext ? pNext->DynCastTextFrame() : nullptr,
                            pPrev ? pPrev->DynCastTextFrame() : nullptr );
                    }
                }
                pNew = pSct;
@@ -1147,9 +1149,11 @@
                if ( pViewShell && pViewShell->GetLayout() &&
                     pViewShell->GetLayout()->IsAnyShellAccessible() )
                {
                    auto pNext = pNew->FindNextCnt( true );
                    auto pPrev = pNew->FindPrevCnt();
                    pViewShell->InvalidateAccessibleParaFlowRelation(
                        pNew->FindNextCnt( true )->DynCastTextFrame(),
                        pNew->FindPrevCnt()->DynCastTextFrame() );
                        pNext ? pNext->DynCastTextFrame() : nullptr,
                        pPrev ? pPrev->DynCastTextFrame() : nullptr );
                }
            }
            if ( bInitNewSect )
diff --git a/sw/source/core/docnode/ndtbl.cxx b/sw/source/core/docnode/ndtbl.cxx
index c396765..89834dc 100644
--- a/sw/source/core/docnode/ndtbl.cxx
+++ b/sw/source/core/docnode/ndtbl.cxx
@@ -2432,9 +2432,11 @@
            if ( pViewShell && pViewShell->GetLayout() &&
                 pViewShell->GetLayout()->IsAnyShellAccessible() )
            {
                auto pNext = pNew->FindNextCnt( true );
                auto pPrev = pNew->FindPrevCnt();
                pViewShell->InvalidateAccessibleParaFlowRelation(
                            pNew->FindNextCnt( true )->DynCastTextFrame(),
                            pNew->FindPrevCnt()->DynCastTextFrame() );
                            pNext ? pNext->DynCastTextFrame() : nullptr,
                            pPrev ? pPrev->DynCastTextFrame() : nullptr );
            }
        }
        pNew->RegistFlys();
@@ -2467,9 +2469,11 @@
                    if ( pViewShell && pViewShell->GetLayout() &&
                         pViewShell->GetLayout()->IsAnyShellAccessible() )
                    {
                        auto pNext = pFrame->FindNextCnt( true );
                        auto pPrev = pFrame->FindPrevCnt();
                        pViewShell->InvalidateAccessibleParaFlowRelation(
                            pFrame->FindNextCnt( true )->DynCastTextFrame(),
                            pFrame->FindPrevCnt()->DynCastTextFrame() );
                            pNext ? pNext->DynCastTextFrame() : nullptr,
                            pPrev ? pPrev->DynCastTextFrame() : nullptr );
                    }
                }
                pFrame->Cut();
diff --git a/sw/source/core/docnode/node.cxx b/sw/source/core/docnode/node.cxx
index d7a2a6b..1e34abf 100644
--- a/sw/source/core/docnode/node.cxx
+++ b/sw/source/core/docnode/node.cxx
@@ -1487,9 +1487,11 @@
            if ( pViewShell && pViewShell->GetLayout() &&
                 pViewShell->GetLayout()->IsAnyShellAccessible() )
            {
                auto pNext = pFrame->FindNextCnt( true );
                auto pPrev = pFrame->FindPrevCnt();
                pViewShell->InvalidateAccessibleParaFlowRelation(
                            pFrame->FindNextCnt( true )->DynCastTextFrame(),
                            pFrame->FindPrevCnt()->DynCastTextFrame() );
                            pNext ? pNext->DynCastTextFrame() : nullptr,
                            pPrev ? pPrev->DynCastTextFrame() : nullptr );
            }
        }

diff --git a/sw/source/core/layout/frmtool.cxx b/sw/source/core/layout/frmtool.cxx
index 77293c1..7d4eb1f 100644
--- a/sw/source/core/layout/frmtool.cxx
+++ b/sw/source/core/layout/frmtool.cxx
@@ -1588,9 +1588,11 @@
                     pViewShell->GetLayout()->IsAnyShellAccessible() &&
                     pFrame->FindPageFrame() != nullptr)
                {
                    auto pNext = pFrame->FindNextCnt( true );
                    auto pPrev = pFrame->FindPrevCnt();
                    pViewShell->InvalidateAccessibleParaFlowRelation(
                        pFrame->FindNextCnt( true )->DynCastTextFrame(),
                        pFrame->FindPrevCnt()->DynCastTextFrame() );
                        pNext ? pNext->DynCastTextFrame() : nullptr,
                        pPrev ? pPrev->DynCastTextFrame() : nullptr );
                    // #i68958#
                    // The information flags of the text frame are validated
                    // in methods <FindNextCnt(..)> and <FindPrevCnt(..)>.
@@ -1674,9 +1676,11 @@
                     pViewShell->GetLayout()->IsAnyShellAccessible() &&
                     pFrame->FindPageFrame() != nullptr)
                {
                    auto pNext = pFrame->FindNextCnt( true );
                    auto pPrev = pFrame->FindPrevCnt();
                    pViewShell->InvalidateAccessibleParaFlowRelation(
                            pFrame->FindNextCnt( true )->DynCastTextFrame(),
                            pFrame->FindPrevCnt()->DynCastTextFrame() );
                            pNext ? pNext->DynCastTextFrame() : nullptr,
                            pPrev ? pPrev->DynCastTextFrame() : nullptr );
                }
            }
            if ( bObjsDirect && !pTable->empty() )
@@ -1786,9 +1790,11 @@
                         pViewShell->GetLayout()->IsAnyShellAccessible() &&
                         pFrame->FindPageFrame() != nullptr)
                    {
                        auto pNext = pFrame->FindNextCnt( true );
                        auto pPrev = pFrame->FindPrevCnt();
                        pViewShell->InvalidateAccessibleParaFlowRelation(
                            pFrame->FindNextCnt( true )->DynCastTextFrame(),
                            pFrame->FindPrevCnt()->DynCastTextFrame() );
                            pNext ? pNext->DynCastTextFrame() : nullptr,
                            pPrev ? pPrev->DynCastTextFrame() : nullptr );
                    }
                }
                pFrame->CheckDirChange();
diff --git a/sw/source/core/text/frmform.cxx b/sw/source/core/text/frmform.cxx
index b0ded2d..4731a7d 100644
--- a/sw/source/core/text/frmform.cxx
+++ b/sw/source/core/text/frmform.cxx
@@ -668,8 +668,9 @@
        if ( pViewShell && pViewShell->GetLayout() &&
             pViewShell->GetLayout()->IsAnyShellAccessible() )
        {
            auto pNext = pFoll->FindNextCnt( true );
            pViewShell->InvalidateAccessibleParaFlowRelation(
                            pFoll->FindNextCnt( true )->DynCastTextFrame(),
                            pNext ? pNext->DynCastTextFrame() : nullptr,
                            this );
        }
    }
@@ -702,8 +703,9 @@
        if ( pViewShell && pViewShell->GetLayout() &&
             pViewShell->GetLayout()->IsAnyShellAccessible() )
        {
            auto pNext = pNew->FindNextCnt( true );
            pViewShell->InvalidateAccessibleParaFlowRelation(
                            pNew->FindNextCnt( true )->DynCastTextFrame(),
                            pNext ? pNext->DynCastTextFrame() : nullptr,
                            this );
        }
    }