[Libreoffice] [PATCH] cppcheck warning in sw/source/ui/uiview/viewprt.cxx

Harri Pitkänen hatapitk at iki.fi
Fri Oct 1 23:40:50 PDT 2010


Here goes my first patch. It silences a "possible null pointer dereference" of
pPage (at the end of diff). Perhaps not for merging yet since my build has not
yet progressed far enough to see that it builds and works and I'd also like to
know what is the preferred way of handling warnings like this.

The issue here is that both pFact and fnCreatePage are checked for nullness
but if either of them is null then pPage would get dereferenced as null. So we
will crash here anyway if that is the case, the question is what is the best
way to handle it? With my patch the null pointer crash will happen at the
earliest possible location, but should I add ASSERT(pFact) and
ASSERT(fnCreatePage) to document that this is intentional?

Cppcheck also finds lots of places where a null pointer crash is done
intentionally for things like "impossible" switch-case values. Sometimes this
is quite well hidden like in sw/source/ui/fldui/fldedt.cxx:203

  pTabPage = SwFldDokPage::Create(this, *(SfxItemSet*)0);

and somewhere I remember also seeing ASSERT(!this). There should be some
standard way of saying "this should never be reached but if it does, just crash
here" but I couldn't find out what it is. Exceptions perhaps, but are those

This and all my future patches to this list are licensed under LGPLv3+ unless I
note otherwise.


diff --git a/sw/source/ui/uiview/viewprt.cxx b/sw/source/ui/uiview/viewprt.cxx
index 88ad189..0f7aabd 100644
--- a/sw/source/ui/uiview/viewprt.cxx
+++ b/sw/source/ui/uiview/viewprt.cxx
@@ -320,16 +320,11 @@ void __EXPORT SwView::ExecutePrint(SfxRequest& rReq)
 SfxTabPage* CreatePrintOptionsPage( Window *pParent,
                                 const SfxItemSet &rOptions, BOOL bPreview )
-    SfxTabPage* pPage = NULL;
     SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
-    if ( pFact )
-    {
-        ::CreateTabPage fnCreatePage = pFact->GetTabPageCreatorFunc( TP_OPTPRINT_PAGE );
-        if ( fnCreatePage )
-            pPage = (*fnCreatePage)( pParent, rOptions );
-    }
+    ::CreateTabPage fnCreatePage = pFact->GetTabPageCreatorFunc( TP_OPTPRINT_PAGE );
+    SfxTabPage* pPage = (*fnCreatePage)( pParent, rOptions );
     SfxAllItemSet aSet(*(rOptions.GetPool()));
     aSet.Put (SfxBoolItem(SID_PREVIEWFLAG_TYPE, bPreview));
     aSet.Put (SfxBoolItem(SID_FAX_LIST, sal_True));

More information about the LibreOffice mailing list