From 1726c27e0d633ab04843834d2bf987bc7645807f Mon Sep 17 00:00:00 2001
From: Caolán McNamara <caolanm@redhat.com>
Date: Fri, 16 Dec 2022 12:11:38 +0000
Subject: check SfxObjectShell::Current()
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

SfxObjectShell::Current() can return null, it's based on the equally
vile SfxViewFrame::Current()

Change-Id: Ia5c7783680e9d8e5d3075078f16a2c15cb6f7a47
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/144339
Tested-by: Jenkins
Reviewed-by: Caolán McNamara <caolanm@redhat.com>
---
 sw/source/core/edit/edfcol.cxx                     |  16 ++-
 sw/source/ui/dialog/docstdlg.cxx                   |   4 +-
 sw/source/ui/fldui/fldedt.cxx                      |   4 +-
 sw/source/uibase/app/docsh2.cxx                    |  50 ++++-----
 sw/source/uibase/ribbar/workctrl.cxx               |   8 +-
 sw/source/uibase/shells/annotsh.cxx                |   5 +-
 sw/source/uibase/shells/drwtxtex.cxx               |   3 +-
 sw/source/uibase/sidebar/PageStylesPanel.cxx       | 112 ++++++++++++---------
 sw/source/uibase/sidebar/StylePresetsPanel.cxx     |  15 ++-
 .../uibase/sidebar/WriterInspectorTextPanel.cxx    |  20 ++--
 10 files changed, 133 insertions(+), 104 deletions(-)

(limited to 'sw')

diff --git a/sw/source/core/edit/edfcol.cxx b/sw/source/core/edit/edfcol.cxx
index 6b7852997d60..dbd9d5bb30dd 100644
--- a/sw/source/core/edit/edfcol.cxx
+++ b/sw/source/core/edit/edfcol.cxx
@@ -777,7 +777,11 @@ static void equaliseNumberOfParagraph(std::vector<svx::ClassificationResult> con
 void SwEditShell::ApplyAdvancedClassification(std::vector<svx::ClassificationResult> const & rResults)
 {
     SwDocShell* pDocShell = GetDoc()->GetDocShell();
-    if (!pDocShell || !SfxObjectShell::Current())
+    if (!pDocShell)
+        return;
+
+    const SfxObjectShell* pObjSh = SfxObjectShell::Current();
+    if (!pObjSh)
         return;
 
     uno::Reference<frame::XModel> xModel = pDocShell->GetBaseModel();
@@ -787,7 +791,7 @@ void SwEditShell::ApplyAdvancedClassification(std::vector<svx::ClassificationRes
 
     uno::Reference<lang::XMultiServiceFactory> xMultiServiceFactory(xModel, uno::UNO_QUERY);
 
-    uno::Reference<document::XDocumentProperties> xDocumentProperties = SfxObjectShell::Current()->getDocProperties();
+    uno::Reference<document::XDocumentProperties> xDocumentProperties = pObjSh->getDocProperties();
 
     const OUString sPolicy = SfxClassificationHelper::policyTypeToString(SfxClassificationHelper::getPolicyType());
     const std::vector<OUString> aUsedPageStyles = lcl_getUsedPageStyles(this);
@@ -949,12 +953,16 @@ std::vector<svx::ClassificationResult> SwEditShell::CollectAdvancedClassificatio
     std::vector<svx::ClassificationResult> aResult;
 
     SwDocShell* pDocShell = GetDoc()->GetDocShell();
-    if (!pDocShell || !SfxObjectShell::Current())
+    if (!pDocShell)
+        return aResult;
+
+    const SfxObjectShell* pObjSh = SfxObjectShell::Current();
+    if (!pObjSh)
         return aResult;
 
     const OUString sBlank;
 
-    uno::Reference<document::XDocumentProperties> xDocumentProperties = SfxObjectShell::Current()->getDocProperties();
+    uno::Reference<document::XDocumentProperties> xDocumentProperties = pObjSh->getDocProperties();
     uno::Reference<beans::XPropertyContainer> xPropertyContainer = xDocumentProperties->getUserDefinedProperties();
     sfx::ClassificationKeyCreator aCreator(SfxClassificationHelper::getPolicyType());
 
diff --git a/sw/source/ui/dialog/docstdlg.cxx b/sw/source/ui/dialog/docstdlg.cxx
index 2701ec015e6c..48e4642281ea 100644
--- a/sw/source/ui/dialog/docstdlg.cxx
+++ b/sw/source/ui/dialog/docstdlg.cxx
@@ -54,7 +54,7 @@ SwDocStatPage::SwDocStatPage(weld::Container* pPage, weld::DialogController* pCo
     m_xUpdatePB->connect_clicked(LINK(this, SwDocStatPage, UpdateHdl));
     //#111684# is the current view a page preview no SwFEShell can be found -> hide the update button
     SwDocShell* pDocShell = static_cast<SwDocShell*>( SfxObjectShell::Current() );
-    SwFEShell* pFEShell = pDocShell->GetFEShell();
+    SwFEShell* pFEShell = pDocShell ? pDocShell->GetFEShell() : nullptr;
     if(!pFEShell)
     {
         m_xUpdatePB->hide();
@@ -118,7 +118,7 @@ IMPL_LINK_NOARG(SwDocStatPage, UpdateHdl, weld::Button&, void)
 {
     Update();
     SwDocShell* pDocShell = static_cast<SwDocShell*>( SfxObjectShell::Current());
-    SwFEShell* pFEShell = pDocShell->GetFEShell();
+    SwFEShell* pFEShell = pDocShell ? pDocShell->GetFEShell() : nullptr;
     if (pFEShell)
         m_xLineNo->set_label(OUString::number(pFEShell->GetLineCount()));
 }
diff --git a/sw/source/ui/fldui/fldedt.cxx b/sw/source/ui/fldui/fldedt.cxx
index 6e0ca5caf07e..e4f5824f9ad9 100644
--- a/sw/source/ui/fldui/fldedt.cxx
+++ b/sw/source/ui/fldui/fldedt.cxx
@@ -187,8 +187,8 @@ SfxTabPage* SwFieldEditDlg::CreatePage(sal_uInt16 nGroup)
             xTabPage = SwFieldRefPage::Create(get_content_area(), this, nullptr);
             break;
         case GRP_REG:
+            if (SfxObjectShell* pDocSh = SfxObjectShell::Current())
             {
-                SfxObjectShell* pDocSh = SfxObjectShell::Current();
                 auto pSet = new SfxItemSetFixed<FN_FIELD_DIALOG_DOC_PROPS, FN_FIELD_DIALOG_DOC_PROPS>( pDocSh->GetPool() );
                 using namespace ::com::sun::star;
                 uno::Reference<document::XDocumentPropertiesSupplier> xDPS(
@@ -200,8 +200,8 @@ SfxTabPage* SwFieldEditDlg::CreatePage(sal_uInt16 nGroup)
                     uno::UNO_QUERY_THROW);
                 pSet->Put( SfxUnoAnyItem( FN_FIELD_DIALOG_DOC_PROPS, uno::Any(xUDProps) ) );
                 xTabPage = SwFieldDokInfPage::Create(get_content_area(), this, pSet);
-                break;
             }
+            break;
 #if HAVE_FEATURE_DBCONNECTIVITY && !ENABLE_FUZZERS
         case GRP_DB:
             xTabPage = SwFieldDBPage::Create(get_content_area(), this, nullptr);
diff --git a/sw/source/uibase/app/docsh2.cxx b/sw/source/uibase/app/docsh2.cxx
index ef3c607444e5..bc9647d77dd7 100644
--- a/sw/source/uibase/app/docsh2.cxx
+++ b/sw/source/uibase/app/docsh2.cxx
@@ -1223,36 +1223,38 @@ void SwDocShell::Execute(SfxRequest& rReq)
         }
         break;
         case SID_CLASSIFICATION_DIALOG:
-        {
-            auto xDialog = std::make_shared<svx::ClassificationDialog>(GetView()->GetFrameWeld(), false);
+            if (SfxObjectShell* pObjSh = SfxObjectShell::Current())
+            {
+                auto xDialog = std::make_shared<svx::ClassificationDialog>(GetView()->GetFrameWeld(), pObjSh->getDocProperties(), false);
 
-            SwWrtShell* pShell = GetWrtShell();
-            std::vector<svx::ClassificationResult> aInput = pShell->CollectAdvancedClassification();
-            xDialog->setupValues(std::move(aInput));
+                SwWrtShell* pShell = GetWrtShell();
+                std::vector<svx::ClassificationResult> aInput = pShell->CollectAdvancedClassification();
+                xDialog->setupValues(std::move(aInput));
 
-            weld::DialogController::runAsync(xDialog, [xDialog, pShell](sal_Int32 nResult){
-                if (RET_OK == nResult)
-                    pShell->ApplyAdvancedClassification(xDialog->getResult());
-            });
-        }
-        break;
+                weld::DialogController::runAsync(xDialog, [xDialog, pShell](sal_Int32 nResult){
+                    if (RET_OK == nResult)
+                        pShell->ApplyAdvancedClassification(xDialog->getResult());
+                });
+            }
+            break;
         case SID_PARAGRAPH_SIGN_CLASSIFY_DLG:
-        {
-            SwWrtShell* pShell = GetWrtShell();
-            auto xDialog = std::make_shared<svx::ClassificationDialog>(GetView()->GetFrameWeld(), true, [pShell]()
+            if (SfxObjectShell* pObjSh = SfxObjectShell::Current())
             {
-                pShell->SignParagraph();
-            });
+                SwWrtShell* pShell = GetWrtShell();
+                auto xDialog = std::make_shared<svx::ClassificationDialog>(GetView()->GetFrameWeld(), pObjSh->getDocProperties(), true, [pShell]()
+                {
+                    pShell->SignParagraph();
+                });
 
-            std::vector<svx::ClassificationResult> aInput = pShell->CollectParagraphClassification();
-            xDialog->setupValues(std::move(aInput));
+                std::vector<svx::ClassificationResult> aInput = pShell->CollectParagraphClassification();
+                xDialog->setupValues(std::move(aInput));
 
-            weld::DialogController::runAsync(xDialog, [xDialog, pShell](sal_Int32 nResult){
-                if (RET_OK == nResult)
-                    pShell->ApplyParagraphClassification(xDialog->getResult());
-            });
-        }
-        break;
+                weld::DialogController::runAsync(xDialog, [xDialog, pShell](sal_Int32 nResult){
+                    if (RET_OK == nResult)
+                        pShell->ApplyParagraphClassification(xDialog->getResult());
+                });
+            }
+            break;
         case SID_WATERMARK:
         {
             SwWrtShell* pSh = GetWrtShell();
diff --git a/sw/source/uibase/ribbar/workctrl.cxx b/sw/source/uibase/ribbar/workctrl.cxx
index f70662f8c73d..5317da59443e 100644
--- a/sw/source/uibase/ribbar/workctrl.cxx
+++ b/sw/source/uibase/ribbar/workctrl.cxx
@@ -560,9 +560,11 @@ IMPL_LINK_NOARG(SwJumpToSpecificBox_Impl, SelectHdl, weld::Entry&, bool)
     OUString sEntry(m_xWidget->get_text());
     SfxUInt16Item aPageNum(m_nSlotId);
     aPageNum.SetValue(o3tl::narrowing<sal_uInt16>(sEntry.toInt32()));
-    SfxObjectShell* pCurrentShell = SfxObjectShell::Current();
-    pCurrentShell->GetDispatcher()->ExecuteList(m_nSlotId, SfxCallMode::ASYNCHRON,
-            { &aPageNum });
+    if (SfxObjectShell* pCurrentShell = SfxObjectShell::Current())
+    {
+        pCurrentShell->GetDispatcher()->ExecuteList(m_nSlotId, SfxCallMode::ASYNCHRON,
+                { &aPageNum });
+    }
     return true;
 }
 
diff --git a/sw/source/uibase/shells/annotsh.cxx b/sw/source/uibase/shells/annotsh.cxx
index 3dcfc6a4a346..6318c7bf7852 100644
--- a/sw/source/uibase/shells/annotsh.cxx
+++ b/sw/source/uibase/shells/annotsh.cxx
@@ -224,8 +224,9 @@ void SwAnnotationShell::Exec( SfxRequest &rReq )
         case FN_GROW_FONT_SIZE:
         case FN_SHRINK_FONT_SIZE:
         {
-            const SvxFontListItem* pFontListItem = static_cast< const SvxFontListItem* >
-                    ( SfxObjectShell::Current()->GetItem( SID_ATTR_CHAR_FONTLIST ) );
+            const SfxObjectShell* pObjSh = SfxObjectShell::Current();
+            const SvxFontListItem* pFontListItem = static_cast<const SvxFontListItem*>
+                    (pObjSh ? pObjSh->GetItem(SID_ATTR_CHAR_FONTLIST) : nullptr);
             const FontList* pFontList = pFontListItem ? pFontListItem->GetFontList() : nullptr;
             pOLV->GetEditView().ChangeFontSize( nSlot == FN_GROW_FONT_SIZE, pFontList );
         }
diff --git a/sw/source/uibase/shells/drwtxtex.cxx b/sw/source/uibase/shells/drwtxtex.cxx
index 9f23ed7e6b7d..c84ee7bd9af4 100644
--- a/sw/source/uibase/shells/drwtxtex.cxx
+++ b/sw/source/uibase/shells/drwtxtex.cxx
@@ -624,8 +624,9 @@ void SwDrawTextShell::Execute( SfxRequest &rReq )
         case FN_GROW_FONT_SIZE:
         case FN_SHRINK_FONT_SIZE:
         {
+            const SfxObjectShell* pObjSh = SfxObjectShell::Current();
             const SvxFontListItem* pFontListItem = static_cast< const SvxFontListItem* >
-                    ( SfxObjectShell::Current()->GetItem( SID_ATTR_CHAR_FONTLIST ) );
+                    (pObjSh ? pObjSh->GetItem(SID_ATTR_CHAR_FONTLIST) : nullptr);
             const FontList* pFontList = pFontListItem ? pFontListItem->GetFontList() : nullptr;
             pOLV->GetEditView().ChangeFontSize( nSlot == FN_GROW_FONT_SIZE, pFontList );
         }
diff --git a/sw/source/uibase/sidebar/PageStylesPanel.cxx b/sw/source/uibase/sidebar/PageStylesPanel.cxx
index ca481345d581..1956b414a951 100644
--- a/sw/source/uibase/sidebar/PageStylesPanel.cxx
+++ b/sw/source/uibase/sidebar/PageStylesPanel.cxx
@@ -260,11 +260,14 @@ XGradient const & PageStylesPanel::GetGradientSetOrDefault()
 {
     if( !mpBgGradientItem )
     {
-        SfxObjectShell* pSh = SfxObjectShell::Current();
-        const SvxGradientListItem * pGradListItem = pSh->GetItem(SID_GRADIENT_LIST);
-        const XGradient aGradient = pGradListItem->GetGradientList()->GetGradient(0)->GetGradient();
-        const OUString aGradientName = pGradListItem->GetGradientList()->GetGradient(0)->GetName();
-
+        XGradient aGradient;
+        OUString aGradientName;
+        if (SfxObjectShell* pSh = SfxObjectShell::Current())
+        {
+            const SvxGradientListItem* pGradListItem = pSh->GetItem(SID_GRADIENT_LIST);
+            aGradient = pGradListItem->GetGradientList()->GetGradient(0)->GetGradient();
+            aGradientName = pGradListItem->GetGradientList()->GetGradient(0)->GetName();
+        }
         mpBgGradientItem.reset( new XFillGradientItem( aGradientName, aGradient ) );
     }
 
@@ -275,11 +278,14 @@ OUString const & PageStylesPanel::GetHatchingSetOrDefault()
 {
     if( !mpBgHatchItem )
     {
-        SfxObjectShell* pSh = SfxObjectShell::Current();
-        const SvxHatchListItem * pHatchListItem = pSh->GetItem(SID_HATCH_LIST);
-        const XHatch aHatch = pHatchListItem->GetHatchList()->GetHatch(0)->GetHatch();
-        const OUString aHatchName = pHatchListItem->GetHatchList()->GetHatch(0)->GetName();
-
+        XHatch aHatch;
+        OUString aHatchName;
+        if (SfxObjectShell* pSh = SfxObjectShell::Current())
+        {
+            const SvxHatchListItem * pHatchListItem = pSh->GetItem(SID_HATCH_LIST);
+            aHatch = pHatchListItem->GetHatchList()->GetHatch(0)->GetHatch();
+            aHatchName = pHatchListItem->GetHatchList()->GetHatch(0)->GetName();
+        }
         mpBgHatchItem.reset( new XFillHatchItem( aHatchName, aHatch ) );
     }
 
@@ -290,11 +296,14 @@ OUString const & PageStylesPanel::GetBitmapSetOrDefault()
 {
     if( !mpBgBitmapItem || mpBgBitmapItem->isPattern() )
     {
-        SfxObjectShell* pSh = SfxObjectShell::Current();
-        const SvxBitmapListItem * pBmpListItem = pSh->GetItem(SID_BITMAP_LIST);
-        const GraphicObject aGraphObj = pBmpListItem->GetBitmapList()->GetBitmap(0)->GetGraphicObject();
-        const OUString aBmpName = pBmpListItem->GetBitmapList()->GetBitmap(0)->GetName();
-
+        GraphicObject aGraphObj;
+        OUString aBmpName;
+        if (SfxObjectShell* pSh = SfxObjectShell::Current())
+        {
+            const SvxBitmapListItem * pBmpListItem = pSh->GetItem(SID_BITMAP_LIST);
+            aGraphObj = pBmpListItem->GetBitmapList()->GetBitmap(0)->GetGraphicObject();
+            aBmpName = pBmpListItem->GetBitmapList()->GetBitmap(0)->GetName();
+        }
         mpBgBitmapItem.reset( new XFillBitmapItem( aBmpName, aGraphObj ) );
     }
 
@@ -305,11 +314,14 @@ OUString const & PageStylesPanel::GetPatternSetOrDefault()
 {
     if( !mpBgBitmapItem || !mpBgBitmapItem->isPattern() )
     {
-        SfxObjectShell* pSh = SfxObjectShell::Current();
-        const SvxPatternListItem * pPatternListItem = pSh->GetItem(SID_PATTERN_LIST);
-        const GraphicObject aGraphObj = pPatternListItem->GetPatternList()->GetBitmap(0)->GetGraphicObject();
-        const OUString aPatternName = pPatternListItem->GetPatternList()->GetBitmap(0)->GetName();
-
+        GraphicObject aGraphObj;
+        OUString aPatternName;
+        if (SfxObjectShell* pSh = SfxObjectShell::Current())
+        {
+            const SvxPatternListItem * pPatternListItem = pSh->GetItem(SID_PATTERN_LIST);
+            aGraphObj = pPatternListItem->GetPatternList()->GetBitmap(0)->GetGraphicObject();
+            aPatternName = pPatternListItem->GetPatternList()->GetBitmap(0)->GetName();
+        }
         mpBgBitmapItem.reset( new XFillBitmapItem( aPatternName, aGraphObj ) );
     }
 
@@ -547,40 +559,42 @@ void PageStylesPanel::ModifyFillColor()
         }
         break;
         case HATCH:
-        {
-            const SvxHatchListItem * pHatchListItem = pSh->GetItem(SID_HATCH_LIST);
-            sal_uInt16 nPos = mxBgHatchingLB->get_active();
-            XHatch aHatch = pHatchListItem->GetHatchList()->GetHatch(nPos)->GetHatch();
-            const OUString aHatchName = pHatchListItem->GetHatchList()->GetHatch(nPos)->GetName();
+            if (pSh)
+            {
+                const SvxHatchListItem * pHatchListItem = pSh->GetItem(SID_HATCH_LIST);
+                sal_uInt16 nPos = mxBgHatchingLB->get_active();
+                XHatch aHatch = pHatchListItem->GetHatchList()->GetHatch(nPos)->GetHatch();
+                const OUString aHatchName = pHatchListItem->GetHatchList()->GetHatch(nPos)->GetName();
 
-            XFillHatchItem aItem(aHatchName, aHatch);
-            GetBindings()->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_HATCH, SfxCallMode::RECORD, { &aItem });
-        }
-        break;
+                XFillHatchItem aItem(aHatchName, aHatch);
+                GetBindings()->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_HATCH, SfxCallMode::RECORD, { &aItem });
+            }
+            break;
         case BITMAP:
         case PATTERN:
-        {
-            sal_Int16 nPos = mxBgBitmapLB->get_active();
-            GraphicObject aBitmap;
-            OUString aBitmapName;
-
-            if ( eXFS == BITMAP )
-            {
-                SvxBitmapListItem const * pBitmapListItem = pSh->GetItem(SID_BITMAP_LIST);
-                aBitmap = pBitmapListItem->GetBitmapList()->GetBitmap(nPos)->GetGraphicObject();
-                aBitmapName = pBitmapListItem->GetBitmapList()->GetBitmap(nPos)->GetName();
-            }
-            else
+            if (pSh)
             {
-                SvxPatternListItem const * pPatternListItem = pSh->GetItem(SID_PATTERN_LIST);
-                aBitmap = pPatternListItem->GetPatternList()->GetBitmap(nPos)->GetGraphicObject();
-                aBitmapName = pPatternListItem->GetPatternList()->GetBitmap(nPos)->GetName();
-            }
+                sal_Int16 nPos = mxBgBitmapLB->get_active();
+                GraphicObject aBitmap;
+                OUString aBitmapName;
 
-            XFillBitmapItem aItem(aBitmapName, aBitmap);
-            GetBindings()->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_BITMAP, SfxCallMode::RECORD, { &aItem });
-        }
-        break;
+                if ( eXFS == BITMAP )
+                {
+                    SvxBitmapListItem const * pBitmapListItem = pSh->GetItem(SID_BITMAP_LIST);
+                    aBitmap = pBitmapListItem->GetBitmapList()->GetBitmap(nPos)->GetGraphicObject();
+                    aBitmapName = pBitmapListItem->GetBitmapList()->GetBitmap(nPos)->GetName();
+                }
+                else
+                {
+                    SvxPatternListItem const * pPatternListItem = pSh->GetItem(SID_PATTERN_LIST);
+                    aBitmap = pPatternListItem->GetPatternList()->GetBitmap(nPos)->GetGraphicObject();
+                    aBitmapName = pPatternListItem->GetPatternList()->GetBitmap(nPos)->GetName();
+                }
+
+                XFillBitmapItem aItem(aBitmapName, aBitmap);
+                GetBindings()->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_BITMAP, SfxCallMode::RECORD, { &aItem });
+            }
+            break;
         default:
             break;
     }
diff --git a/sw/source/uibase/sidebar/StylePresetsPanel.cxx b/sw/source/uibase/sidebar/StylePresetsPanel.cxx
index 981f26b3e839..ef6b740bd7e8 100644
--- a/sw/source/uibase/sidebar/StylePresetsPanel.cxx
+++ b/sw/source/uibase/sidebar/StylePresetsPanel.cxx
@@ -118,13 +118,13 @@ BitmapEx GenerateStylePreview(SfxObjectShell& rSource, OUString const & aName)
 
 BitmapEx CreatePreview(OUString const & aUrl, OUString const & aName)
 {
-    SfxMedium aMedium(aUrl, StreamMode::STD_READWRITE);
-    SfxObjectShell* pObjectShell = SfxObjectShell::Current();
-    SfxObjectShellLock xTemplDoc = SfxObjectShell::CreateObjectByFactoryName(pObjectShell->GetFactory().GetFactoryName(), SfxObjectCreateMode::ORGANIZER);
-    xTemplDoc->DoInitNew();
-    if (xTemplDoc->LoadFrom(aMedium))
+    if (SfxObjectShell* pObjectShell = SfxObjectShell::Current())
     {
-        return GenerateStylePreview(*xTemplDoc, aName);
+        SfxMedium aMedium(aUrl, StreamMode::STD_READWRITE);
+        SfxObjectShellLock xTemplDoc = SfxObjectShell::CreateObjectByFactoryName(pObjectShell->GetFactory().GetFactoryName(), SfxObjectCreateMode::ORGANIZER);
+        xTemplDoc->DoInitNew();
+        if (xTemplDoc->LoadFrom(aMedium))
+            return GenerateStylePreview(*xTemplDoc, aName);
     }
     return BitmapEx();
 }
@@ -185,8 +185,7 @@ IMPL_LINK_NOARG(StylePresetsPanel, DoubleClickHdl, ValueSet*, void)
     sal_Int32 nItemId = mxValueSet->GetSelectedItemId();
     TemplateEntry* pEntry = static_cast<TemplateEntry*>(mxValueSet->GetItemData(nItemId));
 
-    SwDocShell* pDocSh = static_cast<SwDocShell*>(SfxObjectShell::Current());
-    if (pDocSh)
+    if (SwDocShell* pDocSh = static_cast<SwDocShell*>(SfxObjectShell::Current()))
     {
         SwgReaderOption aOption;
         aOption.SetTextFormats(true);
diff --git a/sw/source/uibase/sidebar/WriterInspectorTextPanel.cxx b/sw/source/uibase/sidebar/WriterInspectorTextPanel.cxx
index d7772596df70..518b61d8f1a1 100644
--- a/sw/source/uibase/sidebar/WriterInspectorTextPanel.cxx
+++ b/sw/source/uibase/sidebar/WriterInspectorTextPanel.cxx
@@ -419,15 +419,17 @@ static void MetadataToTreeNode(const css::uno::Reference<css::uno::XInterface>&
 
     // list associated (predicate, object) pairs of the actual subject
     // under the tree node "Metadata Reference"
-    SwDocShell* pDocSh = static_cast<SwDocShell*>(SfxObjectShell::Current());
-    uno::Reference<rdf::XDocumentMetadataAccess> xDocumentMetadataAccess(pDocSh->GetBaseModel(),
-                                                                         uno::UNO_QUERY);
-    const uno::Reference<rdf::XRepository>& xRepo = xDocumentMetadataAccess->getRDFRepository();
-    const css::uno::Reference<css::rdf::XResource> xSubject(rSource, uno::UNO_QUERY);
-    std::map<OUString, OUString> xStatements
-        = SwRDFHelper::getStatements(pDocSh->GetBaseModel(), xRepo->getGraphNames(), xSubject);
-    for (const auto& pair : xStatements)
-        aCurNode.children.push_back(SimplePropToTreeNode(pair.first, uno::Any(pair.second)));
+    if (SwDocShell* pDocSh = static_cast<SwDocShell*>(SfxObjectShell::Current()))
+    {
+        uno::Reference<rdf::XDocumentMetadataAccess> xDocumentMetadataAccess(pDocSh->GetBaseModel(),
+                                                                             uno::UNO_QUERY);
+        const uno::Reference<rdf::XRepository>& xRepo = xDocumentMetadataAccess->getRDFRepository();
+        const css::uno::Reference<css::rdf::XResource> xSubject(rSource, uno::UNO_QUERY);
+        std::map<OUString, OUString> xStatements
+            = SwRDFHelper::getStatements(pDocSh->GetBaseModel(), xRepo->getGraphNames(), xSubject);
+        for (const auto& pair : xStatements)
+            aCurNode.children.push_back(SimplePropToTreeNode(pair.first, uno::Any(pair.second)));
+    }
 
     rNode.children.push_back(aCurNode);
 }
-- 
cgit