C33010

Warning C33010: Unchecked lower bound for enum 'enum' used as index.

This warning is triggered for an enum that is used as an index into an array, if the upper bound is checked for its value, but not the lower bound.

Example

Code using enumerated types as indexes for arrays will often check for the upper bound in order to ensure the index is not out of range. Because an enum variable is signed by default, it can have a negative value. If it is used as an index into an array of values or an array of function pointers, a negative value can allow arbitrary memory to be read, used, or even executed.

typedef void (*PFN)();

enum class Index
{
    Zero,
    One,
    Two,
    Three,
    Max
};

void foo(Index idx, PFN(&functions)[5])
{
    if (idx > Index::Max)
        return;

    auto pfn = functions[static_cast<int>(idx)];    // C33010
    if (pfn != nullptr)
        (*pfn)();
    // ......
}

These warnings are corrected by checking the index value for lower bound as well:

typedef void (*PFN)();

enum class Index
{
    Zero,
    One,
    Two,
    Three,
    Max
};

void foo(Index idx, PFN(&functions)[5])
{
    if (idx < Index::Zero || idx > Index::Max)
        return;

    auto pfn = functions[static_cast<int>(idx)];    // OK
    if (pfn != nullptr)
        (*pfn)();
    // ......
}

See also

C33011