Value tracking simultaneously assumes enums both can and cannot take any value
In the following snippet I've defined a basic assertion macro and an enum, with an external function that returns a value typed by that enum. I wish to verify that the returned value is a valid enumerated value.
Firstly, if we remove both assertions below then there are no warnings generated, despite the fact that an out of bounds access on the printf
is possible.
If you add both assertions then the warning described inline occur: a warning on the first assertion claims that the result can be predetermined. If we assume that enums can only take their enumerated values then this could be a reasonable claim, albeit unsafe in my opinion.
However, if you remove the first assertion then a warning is generated on the printf
line indicating that a possible out-of-range access at position -2147483648
occurs. In this case, Lint assumes that enums can take any possible value. In my opinion this is the desired result, however it conflicts with the previous paragraph.
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
typedef enum MyEnum_e
{
ENUM_VAL_0 = 0,
ENUM_VAL_1 = 1,
ENUM_VAL_COUNT
} MyEnum_t;
MyEnum_t f(void);
#define MY_ASSERT(cond) if (!(cond)) exit(0)
int main()
{
static int32_t the_array[(uint32_t)ENUM_VAL_COUNT] = { 0, 1 };
MyEnum_t the_value = f(); //some function call, perhaps external. consider f() could return (MyEnum_t)(-1);
MY_ASSERT(the_value >= ENUM_VAL_0); //causes warnings 2650 and 587, incorrectly claims predicate '>=' can be pre-determined and always evaluates to true.
MY_ASSERT(the_value < ENUM_VAL_COUNT);
(void)printf("%i", the_array[the_value]); //If you comment out the first assertion above then this line triggers "warning 676: possibly indexing before the beginning of an allocation, inference yields -2147483648"
return 0;
}
-
Tim commented
This issue is related to: https://support.pclintplus.com/forums/225702-general/suggestions/6262572-resolve-the-correlated-variables-problem-in-valu in that I used the correlated variables problem to demonstrate it, but my issue fundamentally relates to enums