[Libreoffice-commits] core.git: Branch 'distro/collabora/cp-5.1' - include/vcl vcl/source

Aron Budea aron.budea at collabora.com
Thu Apr 6 18:10:41 UTC 2017


 include/vcl/menu.hxx                     |    4 -
 vcl/source/window/menu.cxx               |    4 +
 vcl/source/window/menufloatingwindow.cxx |   74 ++++++++++++++++++-------------
 vcl/source/window/menuitemlist.cxx       |    7 ++
 vcl/source/window/menuitemlist.hxx       |    1 
 5 files changed, 58 insertions(+), 32 deletions(-)

New commits:
commit c9ba32d13aa3076e43596b3b0ad95b5e4eac4d27
Author: Aron Budea <aron.budea at collabora.com>
Date:   Mon Apr 3 02:21:28 2017 +0200

    tdf#104686: do not crash if Menu has been somehow disposed
    
    The rare crashes in MenuFloatingWindow::ImplGetStartY() and
    MenuFloatingWindow::ImplScroll(bool) likely happen because
    of a disposed Menu.
    
    Let's guard against invalid accesses.
    
    Change-Id: Ie31240abbc48c06edd40d0a95f319725cdb3db16
    Reviewed-on: https://gerrit.libreoffice.org/36026
    Tested-by: Jenkins <ci at libreoffice.org>
    Reviewed-by: Michael Meeks <michael.meeks at collabora.com>
    Reviewed-on: https://gerrit.libreoffice.org/36151
    Reviewed-by: Aron Budea <aron.budea at collabora.com>
    Reviewed-on: https://gerrit.libreoffice.org/36228
    Tested-by: Aron Budea <aron.budea at collabora.com>

diff --git a/include/vcl/menu.hxx b/include/vcl/menu.hxx
index 14c3356f0900..53d35b219761 100644
--- a/include/vcl/menu.hxx
+++ b/include/vcl/menu.hxx
@@ -131,7 +131,7 @@ class VCL_DLLPUBLIC Menu : public Resource
     friend struct ImplMenuDelData;
 private:
     ImplMenuDelData* mpFirstDel;
-    MenuItemList* pItemList; // Liste mit den MenuItems
+    std::unique_ptr<MenuItemList> pItemList; // list with MenuItems
     MenuLogo* pLogo;
     Menu* pStartedFrom;
     VclPtr<vcl::Window> pWindow;
@@ -369,7 +369,7 @@ public:
     // Fuer Menu-'Funktionen'
     MenuItemList* GetItemList() const
     {
-        return pItemList;
+        return pItemList.get();
     }
 
     // returns the system's menu handle if native menus are supported
diff --git a/vcl/source/window/menu.cxx b/vcl/source/window/menu.cxx
index da7213ec108b..3d6bc1aa84a8 100644
--- a/vcl/source/window/menu.cxx
+++ b/vcl/source/window/menu.cxx
@@ -171,9 +171,11 @@ Menu::~Menu()
 
     bKilled = true;
 
-    delete pItemList;
+    pItemList->Clear();
     delete pLogo;
+    pLogo = nullptr;
     delete mpLayoutData;
+    mpLayoutData = nullptr;
 
     // Native-support: destroy SalMenu
     ImplSetSalMenu( nullptr );
diff --git a/vcl/source/window/menufloatingwindow.cxx b/vcl/source/window/menufloatingwindow.cxx
index b6d6c966ded2..cc425346d3e5 100644
--- a/vcl/source/window/menufloatingwindow.cxx
+++ b/vcl/source/window/menufloatingwindow.cxx
@@ -152,6 +152,12 @@ long MenuFloatingWindow::ImplGetStartY() const
     long nY = 0;
     if( pMenu )
     {
+        // avoid crash if somehow menu got disposed, and MenuItemList is empty (workaround for tdf#104686)
+        if ( nFirstEntry > 0 && !pMenu->GetItemList()->GetDataFromPos(nFirstEntry - 1) )
+        {
+            return 0;
+        }
+
         for ( sal_uInt16 n = 0; n < nFirstEntry; n++ )
             nY += pMenu->GetItemList()->GetDataFromPos( n )->aSz.Height();
         nY -= pMenu->GetTitleHeight();
@@ -592,45 +598,55 @@ void MenuFloatingWindow::ImplScroll( bool bUp )
         nFirstEntry = pMenu->ImplGetPrevVisible( nFirstEntry );
         DBG_ASSERT( nFirstEntry != ITEMPOS_INVALID, "Scroll?!" );
 
-        long nScrollEntryHeight = pMenu->GetItemList()->GetDataFromPos( nFirstEntry )->aSz.Height();
-
-        if ( !bScrollDown )
+        // avoid crash if somehow menu got disposed, and MenuItemList is empty (workaround for tdf#104686)
+        const auto pItemData = pMenu->GetItemList()->GetDataFromPos( nFirstEntry );
+        if ( pItemData )
         {
-            bScrollDown = true;
-            Invalidate();
-        }
+            long nScrollEntryHeight = pItemData->aSz.Height();
 
-        if ( pMenu->ImplGetPrevVisible( nFirstEntry ) == ITEMPOS_INVALID )
-        {
-            bScrollUp = false;
-            Invalidate();
-        }
+            if ( !bScrollDown )
+            {
+                bScrollDown = true;
+                Invalidate();
+            }
+
+            if ( pMenu->ImplGetPrevVisible( nFirstEntry ) == ITEMPOS_INVALID )
+            {
+                bScrollUp = false;
+                Invalidate();
+            }
 
-        Scroll( 0, nScrollEntryHeight, ImplCalcClipRegion( false ).GetBoundRect(), ScrollFlags::Clip );
+            Scroll( 0, nScrollEntryHeight, ImplCalcClipRegion( false ).GetBoundRect(), ScrollFlags::Clip );
+        }
     }
     else if ( bScrollDown && !bUp )
     {
-        long nScrollEntryHeight = pMenu->GetItemList()->GetDataFromPos( nFirstEntry )->aSz.Height();
+        // avoid crash if somehow menu got disposed, and MenuItemList is empty (workaround for tdf#104686)
+        const auto pItemData = pMenu->GetItemList()->GetDataFromPos( nFirstEntry );
+        if ( pItemData )
+        {
+            long nScrollEntryHeight = pItemData->aSz.Height();
 
-        nFirstEntry = pMenu->ImplGetNextVisible( nFirstEntry );
-        DBG_ASSERT( nFirstEntry != ITEMPOS_INVALID, "Scroll?!" );
+            nFirstEntry = pMenu->ImplGetNextVisible( nFirstEntry );
+            DBG_ASSERT( nFirstEntry != ITEMPOS_INVALID, "Scroll?!" );
 
-        if ( !bScrollUp )
-        {
-            bScrollUp = true;
-            Invalidate();
-        }
+            if ( !bScrollUp )
+            {
+                bScrollUp = true;
+                Invalidate();
+            }
 
-        long nHeight = GetOutputSizePixel().Height();
-        sal_uInt16 nLastVisible;
-        static_cast<PopupMenu*>(pMenu)->ImplCalcVisEntries( nHeight, nFirstEntry, &nLastVisible );
-        if ( pMenu->ImplGetNextVisible( nLastVisible ) == ITEMPOS_INVALID )
-        {
-            bScrollDown = false;
-            Invalidate();
-        }
+            long nHeight = GetOutputSizePixel().Height();
+            sal_uInt16 nLastVisible;
+            static_cast<PopupMenu*>(pMenu)->ImplCalcVisEntries( nHeight, nFirstEntry, &nLastVisible );
+            if ( pMenu->ImplGetNextVisible( nLastVisible ) == ITEMPOS_INVALID )
+            {
+                bScrollDown = false;
+                Invalidate();
+            }
 
-        Scroll( 0, -nScrollEntryHeight, ImplCalcClipRegion( false ).GetBoundRect(), ScrollFlags::Clip );
+            Scroll( 0, -nScrollEntryHeight, ImplCalcClipRegion( false ).GetBoundRect(), ScrollFlags::Clip );
+        }
     }
 
     Invalidate();
diff --git a/vcl/source/window/menuitemlist.cxx b/vcl/source/window/menuitemlist.cxx
index d1042b4fb30b..41b7159e0610 100644
--- a/vcl/source/window/menuitemlist.cxx
+++ b/vcl/source/window/menuitemlist.cxx
@@ -139,6 +139,13 @@ void MenuItemList::Remove( size_t nPos )
     }
 }
 
+void MenuItemList::Clear()
+{
+    for (MenuItemData* i : maItemList)
+        delete i;
+    maItemList.resize(0);
+}
+
 MenuItemData* MenuItemList::GetData( sal_uInt16 nSVId, size_t& rPos ) const
 {
     for( size_t i = 0, n = maItemList.size(); i < n; ++i )
diff --git a/vcl/source/window/menuitemlist.hxx b/vcl/source/window/menuitemlist.hxx
index afc0bdc602ac..e76b447ce177 100644
--- a/vcl/source/window/menuitemlist.hxx
+++ b/vcl/source/window/menuitemlist.hxx
@@ -124,6 +124,7 @@ public:
                     );
     void            InsertSeparator(const OString &rIdent, size_t nPos);
     void            Remove( size_t nPos );
+    void            Clear();
 
     MenuItemData*   GetData( sal_uInt16 nSVId, size_t& rPos ) const;
     MenuItemData*   GetData( sal_uInt16 nSVId ) const


More information about the Libreoffice-commits mailing list