sw/source/uibase/docvw/edtwin.cxx |  109 +++++++++++++++++++-------------------
 1 file changed, 56 insertions(+), 53 deletions(-)

New commits:
commit 1f820cd30b3014c72f6711d80679faa8488ffcda
Author:     Xisco Fauli <xiscofa...@libreoffice.org>
AuthorDate: Wed Jun 22 12:49:04 2022 +0200
Commit:     Xisco Fauli <xiscofa...@libreoffice.org>
CommitDate: Wed Jun 22 16:45:42 2022 +0200

    sw: fix crash in SwEditWin::MouseButtonDown
    
    Since GetPageAtPos might return nullptr
    
    See 
https://crashreport.libreoffice.org/stats/signature/SwEditWin::MouseButtonDown(MouseEvent%20const%20&)
    
    Change-Id: I4ad3492ef46bcd7b263a4de92efd9439a966fb56
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/136279
    Reviewed-by: Caolán McNamara <caol...@redhat.com>
    Tested-by: Xisco Fauli <xiscofa...@libreoffice.org>
    Reviewed-by: Xisco Fauli <xiscofa...@libreoffice.org>

diff --git a/sw/source/uibase/docvw/edtwin.cxx 
b/sw/source/uibase/docvw/edtwin.cxx
index a0effc7db82a..420da9d0b295 100644
--- a/sw/source/uibase/docvw/edtwin.cxx
+++ b/sw/source/uibase/docvw/edtwin.cxx
@@ -2843,69 +2843,72 @@ void SwEditWin::MouseButtonDown(const MouseEvent& 
_rMEvt)
     {
         const SwPageFrame* pPageFrame = rSh.GetLayout()->GetPageAtPos( aDocPos 
);
 
-        // Is it active?
-        bool bActive = true;
-        const SwPageDesc* pDesc = pPageFrame->GetPageDesc();
-
-        const SwFrameFormat* pFormat = pDesc->GetLeftFormat();
-        if ( pPageFrame->OnRightPage() )
-             pFormat = pDesc->GetRightFormat();
-
-        if ( pFormat )
+        if ( pPageFrame )
         {
-            if ( eControl == FrameControlType::Header )
-                bActive = pFormat->GetHeader().IsActive();
-            else
-                bActive = pFormat->GetFooter().IsActive();
-        }
+            // Is it active?
+            bool bActive = true;
+            const SwPageDesc* pDesc = pPageFrame->GetPageDesc();
 
-        if ( !bActive )
-        {
-            // When in Hide-Whitespace mode, we don't want header
-            // and footer controls.
-            if (!rSh.GetViewOptions()->IsHideWhitespaceMode())
-            {
-                SwPaM aPam(*rSh.GetCurrentShellCursor().GetPoint());
-                const bool bWasInHeader = 
aPam.GetPoint()->nNode.GetNode().FindHeaderStartNode() != nullptr;
-                const bool bWasInFooter = 
aPam.GetPoint()->nNode.GetNode().FindFooterStartNode() != nullptr;
+            const SwFrameFormat* pFormat = pDesc->GetLeftFormat();
+            if ( pPageFrame->OnRightPage() )
+                 pFormat = pDesc->GetRightFormat();
 
-                // Is the cursor in a part like similar to the one we clicked 
on? For example,
-                // if the cursor is in a header and we click on an empty 
header... don't change anything to
-                // keep consistent behaviour due to header edit mode (and the 
same for the footer as well).
+            if ( pFormat )
+            {
+                if ( eControl == FrameControlType::Header )
+                    bActive = pFormat->GetHeader().IsActive();
+                else
+                    bActive = pFormat->GetFooter().IsActive();
+            }
 
-                // Otherwise, we hide the header/footer control if a separator 
is shown, and vice versa.
-                if (!(bWasInHeader && eControl == FrameControlType::Header) &&
-                    !(bWasInFooter && eControl == FrameControlType::Footer))
+            if ( !bActive )
+            {
+                // When in Hide-Whitespace mode, we don't want header
+                // and footer controls.
+                if (!rSh.GetViewOptions()->IsHideWhitespaceMode())
                 {
-                    const bool bSeparatorWasVisible = 
rSh.IsShowHeaderFooterSeparator(eControl);
-                    rSh.SetShowHeaderFooterSeparator(eControl, 
!bSeparatorWasVisible);
+                    SwPaM aPam(*rSh.GetCurrentShellCursor().GetPoint());
+                    const bool bWasInHeader = 
aPam.GetPoint()->nNode.GetNode().FindHeaderStartNode() != nullptr;
+                    const bool bWasInFooter = 
aPam.GetPoint()->nNode.GetNode().FindFooterStartNode() != nullptr;
 
-                    // Repaint everything
-                    Invalidate();
-
-                    // tdf#84929. If the footer control had not been showing, 
do not change the cursor position,
-                    // because the user may have scrolled to turn on the 
separator control and
-                    // if the cursor cannot be positioned on-screen, then the 
user would need to scroll back again to use the control.
-                    // This should only be done for the footer. The cursor can 
always be re-positioned near the header. tdf#134023.
-                    if ( eControl == FrameControlType::Footer && 
!bSeparatorWasVisible
-                         && rSh.GetViewOptions()->IsUseHeaderFooterMenu() && 
!Application::IsHeadlessModeEnabled() )
-                        return;
+                    // Is the cursor in a part like similar to the one we 
clicked on? For example,
+                    // if the cursor is in a header and we click on an empty 
header... don't change anything to
+                    // keep consistent behaviour due to header edit mode (and 
the same for the footer as well).
+
+                    // Otherwise, we hide the header/footer control if a 
separator is shown, and vice versa.
+                    if (!(bWasInHeader && eControl == 
FrameControlType::Header) &&
+                        !(bWasInFooter && eControl == 
FrameControlType::Footer))
+                    {
+                        const bool bSeparatorWasVisible = 
rSh.IsShowHeaderFooterSeparator(eControl);
+                        rSh.SetShowHeaderFooterSeparator(eControl, 
!bSeparatorWasVisible);
+
+                        // Repaint everything
+                        Invalidate();
+
+                        // tdf#84929. If the footer control had not been 
showing, do not change the cursor position,
+                        // because the user may have scrolled to turn on the 
separator control and
+                        // if the cursor cannot be positioned on-screen, then 
the user would need to scroll back again to use the control.
+                        // This should only be done for the footer. The cursor 
can always be re-positioned near the header. tdf#134023.
+                        if ( eControl == FrameControlType::Footer && 
!bSeparatorWasVisible
+                             && rSh.GetViewOptions()->IsUseHeaderFooterMenu() 
&& !Application::IsHeadlessModeEnabled() )
+                            return;
+                    }
                 }
             }
-        }
-        else
-        {
-            // Make sure we have the proper Header/Footer separators shown
-            // as these may be changed if clicking on an empty Header/Footer
-            rSh.SetShowHeaderFooterSeparator( FrameControlType::Header, 
eControl == FrameControlType::Header );
-            rSh.SetShowHeaderFooterSeparator( FrameControlType::Footer, 
eControl == FrameControlType::Footer );
-
-            if ( !rSh.IsHeaderFooterEdit() )
+            else
             {
-                rSh.ToggleHeaderFooterEdit();
+                // Make sure we have the proper Header/Footer separators shown
+                // as these may be changed if clicking on an empty 
Header/Footer
+                rSh.SetShowHeaderFooterSeparator( FrameControlType::Header, 
eControl == FrameControlType::Header );
+                rSh.SetShowHeaderFooterSeparator( FrameControlType::Footer, 
eControl == FrameControlType::Footer );
+
+                if ( !rSh.IsHeaderFooterEdit() )
+                {
+                    rSh.ToggleHeaderFooterEdit();
 
-                // Repaint everything
-                rSh.GetWin()->Invalidate();
+                    // Repaint everything
+                    rSh.GetWin()->Invalidate();
+                }
             }
         }
     }

Reply via email to