Unchecked dynamic_cast

Max Kellermann max at duempel.org
Fri Jun 6 04:41:48 PDT 2014


Hi Caolan,

I just happened to read your Libreoffice commits while browsing the
git repository.  For example:

             if ( pFmt->Which() == RES_FLYFRMFMT )
             {
-                GetDoc()->SetFlyFrmTitle( *(dynamic_cast<SwFlyFrmFmt*>(pFmt)),
+                GetDoc()->SetFlyFrmTitle( dynamic_cast<SwFlyFrmFmt&>(*pFmt),
                                           rTitle );

This, however, is still wrong.  It just hides the warning.

We know already at this point that pFmt is not NULL.  But what we
don't know is whether pFmt is a SwFlyFrmFmt instance.  dynamic_cast
can return NULL even if its parameter is not NULL.

So, you have checked that its type is RES_FLYFRMFMT, and thus it must
be a SwFlyFrmFmt instance.  Ok, but then you don't need the overhead
of dynamic_cast.  A static_cast will do the same, with less runtime
overhead.

In any case, your changes do not actually address the Coverity
warnings; they merely hide them.

Max


More information about the LibreOffice mailing list