C++ 17 project using gcc, clang-tidy and clang-format next to sonarCloud.
In the following example the sonar cpp analyzer crashes.
It crashes due to a virtual function call that’s accessed through the inherited class, example below.
This only became an issue once we moved the ok()
implementation to the baseClass instead of having it pure virtual with implementation in the derived classes.
We can privately share the sonar-cfamily-reproducer.tar.xz
For now we avert the crash by using the casting method, though this should be avoided.
The following may not compile, it is just an illustration:
finalInterface.cpp:
#include "finalInterface.h"
#include "interfaces.h"
#include "interfaceOne.h"
#include "interfaceTwo.h"
void FinalInterface::someFunction() {
// This crashes sonar analysis,
// We require both functions to be called
IFaceOne::ok();
IFaceTwo::ok();
// This does not crash sonar analysis:
dynamic_cast<IFaceOne*>(this)->ok()
dynamic_cast<IFaceTwo*>(this)->ok()
}
interfaces.h:
class IFace {
public:
virtual void ok()
{
// virtual because a specialized vector interface exists
// ok
}
};
interfaceOne.h:
#include "interfaces.h"
class IFaceOne: public IFace {
// this does not override ok
};
interfaceTwo.h:
#include "interfaces.h"
class IFaceTwo: public IFace {
// this does not override ok
};
finalInterface.h:
#include "interfaces.h"
#include "interfaceOne.h"
#include "interfaceTwo.h"
class FinalInterface: public IFaceOne, public IFaceTwo
{
public:
void someFunction();
};