I have the following code
enum class LoggerType
{
ASCII,
BINARY_1,
TEXT,
BINARY_2,
NUM_OF_TYPE
};
Logger* Logger::GetInstance(
LoggerType type)
{
static Logger* pLogger[4] =
{
new Logger(LoggerType::ASCII),
new Logger(LoggerType::BINARY_1),
new Logger(LoggerType::TEXT),
new Logger(LoggerType::BINARY_2)
};
return pLogger[static_cast<UINT8>(type)];
}
And Klockwork tells me
Array 'pLogger' of size 2 may use index value(s) 2
Is there a real problem here? If so kindly let knows how can deal this problem. Should I add some protection for "static_cast(type)"? Thanks a lot
** Following is what I guess the issue ** Because we have 5 LoggerType, KW may think it is possible to access out of bound even we don`t. So we have to add some protection here,
Logger* pReturnLogger = NULL;
if (static_cast<UINT8>(type) < static_cast<UINT8>(LoggerType::NUM_OF_TYPE))
{
pReturnLogger = pLogger[static_cast<UINT8>(type)];
}
return pReturnLogger;