tdf#160898: check for nullptr

Regression after commit d81379db730a163c5ff75d4f3a3cddbd7b5eddda
(tdf#154877 sw: generalise ExtendedSelectAll(), 2023-05-09)

Change-Id: I9289171647fca8bd1b696399ff7c43a2ac7b8b30
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/166990
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Tested-by: Jenkins
Signed-off-by: Xisco Fauli <xiscofauli@libreoffice.org>
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/166997
Reviewed-by: Michael Stahl <michael.stahl@allotropia.de>
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/167213
Reviewed-by: Ilmari Lauhakangas <ilmari.lauhakangas@libreoffice.org>
Reviewed-by: Michael Weghorn <m.weghorn@posteo.de>
diff --git a/sw/qa/extras/uiwriter/data/table-in-table.fodt b/sw/qa/extras/uiwriter/data/table-in-table.fodt
new file mode 100644
index 0000000..e055d34
--- /dev/null
+++ b/sw/qa/extras/uiwriter/data/table-in-table.fodt
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>

<office:document xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" office:version="1.3" office:mimetype="application/vnd.oasis.opendocument.text">
 <office:automatic-styles>
  <style:style style:name="border" style:family="table-cell">
   <style:table-cell-properties fo:padding="1mm" fo:border="0.5pt solid #000000"/>
  </style:style>
 </office:automatic-styles>
 <office:body>
  <office:text>
   <text:p/>
   <table:table>
    <table:table-column/>
    <table:table-row>
     <table:table-cell table:style-name="border">
      <table:table>
       <table:table-column/>
       <table:table-row>
        <table:table-cell table:style-name="border"/>
       </table:table-row>
      </table:table>
      <text:p/>
     </table:table-cell>
    </table:table-row>
   </table:table>
   <text:p/>
  </office:text>
 </office:body>
</office:document>
\ No newline at end of file
diff --git a/sw/qa/extras/uiwriter/uiwriter9.cxx b/sw/qa/extras/uiwriter/uiwriter9.cxx
index 9f961b1d..bd69d26 100644
--- a/sw/qa/extras/uiwriter/uiwriter9.cxx
+++ b/sw/qa/extras/uiwriter/uiwriter9.cxx
@@ -109,7 +109,21 @@ CPPUNIT_TEST_FIXTURE(SwUiWriterTest9, testTdf159816)
    xTransfer->PrivateDrop(*pWrtShell, ptTo, /*bMove=*/true, /*bXSelection=*/true);
}

} // end of anonymouse namespace
CPPUNIT_TEST_FIXTURE(SwUiWriterTest9, testTdf160898)
{
    // Given a document with a 1-cell table in another 1-cell table:
    createSwDoc("table-in-table.fodt");
    SwXTextDocument* pXTextDocument = dynamic_cast<SwXTextDocument*>(mxComponent.get());
    SwDocShell* pDocShell = pXTextDocument->GetDocShell();
    SwWrtShell* pWrtShell = pDocShell->GetWrtShell();

    // Move to the normally hidden paragraph inside the outer table cell, following the inner table
    pWrtShell->Down(false, 2);
    // Without the fix, this would crash:
    pWrtShell->SelAll();
}

} // end of anonymous namespace
CPPUNIT_PLUGIN_IMPLEMENT();

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sw/source/core/crsr/crsrsh.cxx b/sw/source/core/crsr/crsrsh.cxx
index 04b263c..b7b7a89 100644
--- a/sw/source/core/crsr/crsrsh.cxx
+++ b/sw/source/core/crsr/crsrsh.cxx
@@ -738,9 +738,15 @@ bool SwCursorShell::MoveStartText()
    SwTableNode const*const pTable(pStartNode->FindTableNode());
    m_pCurrentCursor->GetPoint()->Assign(*pStartNode);
    GetDoc()->GetNodes().GoNext(m_pCurrentCursor->GetPoint());
    while (m_pCurrentCursor->GetPoint()->GetNode().FindTableNode() != pTable
        && (!pTable || pTable->GetIndex() < m_pCurrentCursor->GetPoint()->GetNode().FindTableNode()->GetIndex())
        && MoveOutOfTable());
    while (auto* pFoundTable = m_pCurrentCursor->GetPoint()->GetNode().FindTableNode())
    {
        if (pFoundTable == pTable)
            break;
        if (pTable && pTable->GetIndex() >= pFoundTable->GetIndex())
            break;
        if (!MoveOutOfTable())
            break;
    }
    UpdateCursor(SwCursorShell::SCROLLWIN|SwCursorShell::CHKRANGE|SwCursorShell::READONLY);
    return old != *m_pCurrentCursor->GetPoint();
}