sc/source/ui/vba/vbaapplication.cxx |   13 +++-
 sc/source/ui/vba/vbapagesetup.cxx   |    7 +-
 sc/source/ui/vba/vbaworkbook.cxx    |   26 +++++----
 sc/source/ui/vba/vbaworksheet.cxx   |  102 +++++++++++++++++++-----------------
 4 files changed, 85 insertions(+), 63 deletions(-)

New commits:
commit c0e96f302c04597bc49d55e262b069f1eb170e45
Author:     Xisco Fauli <xiscofa...@libreoffice.org>
AuthorDate: Thu May 18 13:31:14 2023 +0200
Commit:     Caolán McNamara <caolan.mcnam...@collabora.com>
CommitDate: Fri May 19 10:06:30 2023 +0200

    sc: check excel::getDocShell
    
    There seems to be some crashes related. See
    
https://crashreport.libreoffice.org/stats/signature/ScDocShell::GetDocument()
    or
    
https://crashreport.libreoffice.org/stats/signature/ScDocShell::RegisterAutomationWorkbookObject(com::sun::star::uno::Reference%3Cooo::vba::excel::XWorkbook%3E%20const%20&)
    
    Change-Id: I9affce5a28cb1198eb472d369debf83fd7dd8c0e
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/151924
    Tested-by: Caolán McNamara <caolan.mcnam...@collabora.com>
    Reviewed-by: Caolán McNamara <caolan.mcnam...@collabora.com>

diff --git a/sc/source/ui/vba/vbaapplication.cxx 
b/sc/source/ui/vba/vbaapplication.cxx
index 98c7cf7708a5..6676b4bb5eaa 100644
--- a/sc/source/ui/vba/vbaapplication.cxx
+++ b/sc/source/ui/vba/vbaapplication.cxx
@@ -1203,7 +1203,8 @@ uno::Reference< excel::XRange > lclCreateVbaRange(
         const ListOfScRange& rList )
 {
     ScDocShell* pDocShell = excel::getDocShell( rxModel );
-    if( !pDocShell ) throw uno::RuntimeException();
+    if( !pDocShell )
+        throw uno::RuntimeException();
 
     ScRangeList aCellRanges;
     for( const auto& rItem : rList )
@@ -1345,8 +1346,11 @@ ScVbaApplication::Volatile( const uno::Any& aVolatile )
     if ( pMeth )
     {
         uno::Reference< frame::XModel > xModel( getCurrentDocument() );
-        ScDocument& rDoc = excel::getDocShell( xModel )->GetDocument();
-        rDoc.GetMacroManager()->SetUserFuncVolatile( pMeth->GetName(), 
bVolatile);
+        if ( ScDocShell* pShell = excel::getDocShell( xModel ))
+        {
+            ScDocument& rDoc = pShell->GetDocument();
+            rDoc.GetMacroManager()->SetUserFuncVolatile( pMeth->GetName(), 
bVolatile);
+        }
     }
 
 // this is bound to break when loading the document
@@ -1446,7 +1450,10 @@ void SAL_CALL 
ScVbaApplication::setScreenUpdating(sal_Bool bUpdate)
     VbaApplicationBase::setScreenUpdating( bUpdate );
 
     uno::Reference< frame::XModel > xModel( getCurrentExcelDoc( mxContext ), 
uno::UNO_SET_THROW );
+
     ScDocShell* pDocShell = excel::getDocShell( xModel );
+    if (!pDocShell)
+        return;
     ScDocument& rDoc = pDocShell->GetDocument();
 
     if( bUpdate )
diff --git a/sc/source/ui/vba/vbapagesetup.cxx 
b/sc/source/ui/vba/vbapagesetup.cxx
index f487b74e0c7b..f945802e1082 100644
--- a/sc/source/ui/vba/vbapagesetup.cxx
+++ b/sc/source/ui/vba/vbapagesetup.cxx
@@ -77,8 +77,11 @@ OUString SAL_CALL ScVbaPageSetup::getPrintArea()
             ScUnoConversion::FillScRange( aRange, rRange );
             aRangeList.push_back( aRange );
         }
-        ScDocument& rDoc = excel::getDocShell( mxModel )->GetDocument();
-        aRangeList.Format( aPrintArea, ScRefFlags::RANGE_ABS, rDoc, 
formula::FormulaGrammar::CONV_XL_A1, ','  );
+        if ( ScDocShell* pShell = excel::getDocShell( mxModel ))
+        {
+            ScDocument& rDoc = pShell->GetDocument();
+            aRangeList.Format( aPrintArea, ScRefFlags::RANGE_ABS, rDoc, 
formula::FormulaGrammar::CONV_XL_A1, ','  );
+        }
     }
 
     return aPrintArea;
diff --git a/sc/source/ui/vba/vbaworkbook.cxx b/sc/source/ui/vba/vbaworkbook.cxx
index 23cc523a1aa8..0604d9686b61 100644
--- a/sc/source/ui/vba/vbaworkbook.cxx
+++ b/sc/source/ui/vba/vbaworkbook.cxx
@@ -184,9 +184,8 @@ ScVbaWorkbook::init()
 {
     if ( !ColorData.hasElements() )
         ResetColors();
-    uno::Reference< frame::XModel > xModel = getModel();
-    if ( xModel.is() )
-        excel::getDocShell( xModel )->RegisterAutomationWorkbookObject( this );
+    if ( ScDocShell* pShell = excel::getDocShell( getModel() ))
+        pShell->RegisterAutomationWorkbookObject( this );
 }
 
 ScVbaWorkbook::ScVbaWorkbook(   const css::uno::Reference< 
ov::XHelperInterface >& xParent, const css::uno::Reference< 
css::uno::XComponentContext >& xContext, css::uno::Reference< 
css::frame::XModel > const & xModel ) : ScVbaWorkbook_BASE( xParent, xContext, 
xModel )
@@ -264,18 +263,23 @@ ScVbaWorkbook::getProtectStructure()
 
 sal_Bool SAL_CALL ScVbaWorkbook::getPrecisionAsDisplayed()
 {
-    uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_SET_THROW );
-    ScDocument& rDoc = excel::getDocShell( xModel )->GetDocument();
-    return rDoc.GetDocOptions().IsCalcAsShown();
+    if ( ScDocShell* pShell = excel::getDocShell( getModel() ))
+    {
+        ScDocument& rDoc = pShell->GetDocument();
+        return rDoc.GetDocOptions().IsCalcAsShown();
+    }
+    return false;
 }
 
 void SAL_CALL ScVbaWorkbook::setPrecisionAsDisplayed( sal_Bool 
_precisionAsDisplayed )
 {
-    uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_SET_THROW );
-    ScDocument& rDoc = excel::getDocShell( xModel )->GetDocument();
-    ScDocOptions aOpt = rDoc.GetDocOptions();
-    aOpt.SetCalcAsShown( _precisionAsDisplayed );
-    rDoc.SetDocOptions( aOpt );
+    if ( ScDocShell* pShell = excel::getDocShell( getModel() ))
+    {
+        ScDocument& rDoc = pShell->GetDocument();
+        ScDocOptions aOpt = rDoc.GetDocOptions();
+        aOpt.SetCalcAsShown( _precisionAsDisplayed );
+        rDoc.SetDocOptions( aOpt );
+    }
 }
 
 OUString SAL_CALL ScVbaWorkbook::getAuthor()
diff --git a/sc/source/ui/vba/vbaworksheet.cxx 
b/sc/source/ui/vba/vbaworksheet.cxx
index cfc84537da62..abd76c6023c6 100644
--- a/sc/source/ui/vba/vbaworksheet.cxx
+++ b/sc/source/ui/vba/vbaworksheet.cxx
@@ -199,7 +199,8 @@ ScVbaWorksheet::createSheetCopyInNewDoc(const OUString& 
aCurrSheetName)
 
     ScDocShell* pShell = excel::getDocShell( xModel );
     OUString aCodeName;
-    pShell->GetDocument().GetCodeName( 0, aCodeName );
+    if (pShell)
+        pShell->GetDocument().GetCodeName( 0, aCodeName );
     return uno::Reference< excel::XWorksheet >( getUnoDocModule( aCodeName, 
pShell ), uno::UNO_QUERY_THROW );
 }
 
@@ -309,20 +310,22 @@ ScVbaWorksheet::getEnableSelection()
     if ( !ScVbaWorksheets::nameExists(xSpreadDoc, getName(), nTab) )
         throw uno::RuntimeException("Sheet Name does not exist." );
 
-    uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_SET_THROW );
-    ScDocument& rDoc = excel::getDocShell( xModel )->GetDocument();
-    const ScTableProtection* pProtect = rDoc.GetTabProtection(nTab);
-    bool bLockedCells = false;
-    bool bUnlockedCells = false;
-    if( pProtect )
+    if ( ScDocShell* pShell = excel::getDocShell( getModel() ))
     {
-        bLockedCells   = 
pProtect->isOptionEnabled(ScTableProtection::SELECT_LOCKED_CELLS);
-        bUnlockedCells = 
pProtect->isOptionEnabled(ScTableProtection::SELECT_UNLOCKED_CELLS);
+        ScDocument& rDoc = pShell->GetDocument();
+        const ScTableProtection* pProtect = rDoc.GetTabProtection(nTab);
+        bool bLockedCells = false;
+        bool bUnlockedCells = false;
+        if( pProtect )
+        {
+            bLockedCells   = 
pProtect->isOptionEnabled(ScTableProtection::SELECT_LOCKED_CELLS);
+            bUnlockedCells = 
pProtect->isOptionEnabled(ScTableProtection::SELECT_UNLOCKED_CELLS);
+        }
+        if( bLockedCells )
+            return excel::XlEnableSelection::xlNoRestrictions;
+        if( bUnlockedCells )
+            return excel::XlEnableSelection::xlUnlockedCells;
     }
-    if( bLockedCells )
-        return excel::XlEnableSelection::xlNoRestrictions;
-    if( bUnlockedCells )
-        return excel::XlEnableSelection::xlUnlockedCells;
     return excel::XlEnableSelection::xlNoSelection;
 
 }
@@ -342,46 +345,49 @@ ScVbaWorksheet::setEnableSelection( sal_Int32 nSelection )
     if ( !ScVbaWorksheets::nameExists(xSpreadDoc, getName(), nTab) )
         throw uno::RuntimeException("Sheet Name does not exist." );
 
-    uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_SET_THROW );
-    ScDocument& rDoc = excel::getDocShell( xModel )->GetDocument();
-    const ScTableProtection* pProtect = rDoc.GetTabProtection(nTab);
-    // default is xlNoSelection
-    bool bLockedCells = false;
-    bool bUnlockedCells = false;
-    if( nSelection == excel::XlEnableSelection::xlNoRestrictions )
+    if ( ScDocShell* pShell = excel::getDocShell( getModel() ))
     {
-        bLockedCells = true;
-        bUnlockedCells = true;
-    }
-    else if( nSelection == excel::XlEnableSelection::xlUnlockedCells )
-    {
-        bUnlockedCells = true;
-    }
-    if( pProtect )
-    {
-        ScTableProtection aNewProtect(*pProtect);
-        aNewProtect.setOption(ScTableProtection::SELECT_LOCKED_CELLS, 
bLockedCells);
-        aNewProtect.setOption(ScTableProtection::SELECT_UNLOCKED_CELLS, 
bUnlockedCells);
-        rDoc.SetTabProtection(nTab, &aNewProtect);
+        ScDocument& rDoc = pShell->GetDocument();
+        const ScTableProtection* pProtect = rDoc.GetTabProtection(nTab);
+        // default is xlNoSelection
+        bool bLockedCells = false;
+        bool bUnlockedCells = false;
+        if( nSelection == excel::XlEnableSelection::xlNoRestrictions )
+        {
+            bLockedCells = true;
+            bUnlockedCells = true;
+        }
+        else if( nSelection == excel::XlEnableSelection::xlUnlockedCells )
+        {
+            bUnlockedCells = true;
+        }
+        if( pProtect )
+        {
+            ScTableProtection aNewProtect(*pProtect);
+            aNewProtect.setOption(ScTableProtection::SELECT_LOCKED_CELLS, 
bLockedCells);
+            aNewProtect.setOption(ScTableProtection::SELECT_UNLOCKED_CELLS, 
bUnlockedCells);
+            rDoc.SetTabProtection(nTab, &aNewProtect);
+        }
     }
-
-
 }
 
 sal_Bool SAL_CALL ScVbaWorksheet::getAutoFilterMode()
 {
-    uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_SET_THROW );
-    ScDocument& rDoc = excel::getDocShell( xModel )->GetDocument();
-    ScDBData* pDBData = rDoc.GetAnonymousDBData(getSheetID());
-    if (pDBData)
-        return pDBData->HasAutoFilter();
+    if ( ScDocShell* pShell = excel::getDocShell( getModel() ))
+    {
+        ScDocument& rDoc = pShell->GetDocument();
+        ScDBData* pDBData = rDoc.GetAnonymousDBData(getSheetID());
+        if (pDBData)
+            return pDBData->HasAutoFilter();
+    }
     return false;
 }
 
 void SAL_CALL ScVbaWorksheet::setAutoFilterMode( sal_Bool bAutoFilterMode )
 {
-    uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_SET_THROW );
-    ScDocShell* pDocShell = excel::getDocShell( xModel );
+    ScDocShell* pDocShell = excel::getDocShell( getModel() );
+    if (!pDocShell)
+        return;
     ScDocument& rDoc = pDocShell->GetDocument();
     ScDBData* pDBData = rDoc.GetAnonymousDBData(getSheetID());
     if (!pDBData)
@@ -481,11 +487,13 @@ ScVbaWorksheet::getProtectDrawingObjects()
     bool bSheetExists = ScVbaWorksheets::nameExists (xSpreadDoc, aSheetName, 
nTab);
     if ( bSheetExists )
     {
-        uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_SET_THROW 
);
-        ScDocument& rDoc = excel::getDocShell( xModel )->GetDocument();
-        const ScTableProtection* pProtect = rDoc.GetTabProtection(nTab);
-        if ( pProtect )
-            return pProtect->isOptionEnabled( ScTableProtection::OBJECTS );
+        if ( ScDocShell* pShell = excel::getDocShell( getModel() ))
+        {
+            ScDocument& rDoc = pShell->GetDocument();
+            const ScTableProtection* pProtect = rDoc.GetTabProtection(nTab);
+            if ( pProtect )
+                return pProtect->isOptionEnabled( ScTableProtection::OBJECTS );
+        }
     }
     return false;
 }

Reply via email to